Need suggestion on an automation to load data to RDS

Hi there,

I am working on an automation to load data to an postgresql database hosted on RDS. My plan is as follows:

  1. Set up event notification on an S3 bucket which triggers a lambda every time a CSV file uploaded to the bucket.
  2. The lambda spins up an ephemeral EC2 instance.
  3. EC2 instance downloads the file from s3 bucket using AWS CLI commands in its userdata and loads the csv data in RDS using pssql utility.
  4. Once loading is completed, EC2 instance is terminated.

I am looking for some suggestion to make this better or if this automation can be done in any other more efficient setup?

Thanks

Edit: I am using EC2 instance to load the data because data loading is taking more than 15 minutes.

Why don’t you do the csv load with the lambda instead? Is it going to take longer than 15 minutes?

Edit:

With the aws Database Migration Service you could detect changes in an S3 with the CDC and replicate them to RDS. However I dont know if this is going to be cheaper than the lambda.

I believe you may be looking for this: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PostgreSQL.S3Import.html

I work on a product that does large loads of data into our Aurora database and we use a similar method to the above with a Lambda. We massage data with the Lambda before importing.

That’s right, csv load is taking more than 15 minutes to load. I should mention this in my original post.

We are using postgresql v9 so in that case I dont think we can use this :confused:

Can each line in the csv be loaded independently? If so you could split the load up into smaller chunks and process each chunk in a lambda, eg using step functions or just pushing chunks onto an sqs queue.

If you want to stay more with ec2, I would build a container and use fargate to run the container on demand.

You could start a Fargate task instead of spinning up an EC2? It would be simpler to trigger than spinning up an EC2, you’d still need the lambda though.

totally agree with the step function route. You could also use a parallel step, spawn multiple fargate task and import things in parallel.

Do you need the csv to be loaded into rds immediately after S3 object creation? Otherwise you could just send the events to sqs and once per day empty that queue with one or two scheduled ec2 instances.

Isn’t postgres 9 already deprecated?

Smells around…

Lazy dev

No, I mean, the RDS docs say they’ll force upgrade you on the next maintenance window after march. How are they even using 9 in RDS?

Use a Glue job.

You should consider AWS batch instead of the EC2 approach. You might be able to trigger the job directly from EventBridge, but a small Lambda might be needed to set up the right params.

What about using DMS ? https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Source.S3.html

Yeah I think ideally the data could get into SQS so SQS can trigger a lambda to ingest data in more reasonable blocks. But I guess the problem would be getting the stuff into SQS if OP is restricted to ingesting via a fat ass CSV

Im not sure but the SQS message shouldn’t have the whole csv but just the S3 path to it.

You can setup a CloudFormation template that runs when the lambda is triggered and self-terminate after the data is added.

You might also setup an ECS Cluster/Sevice. Just set the desired count to 1 when you need to process data, then set back to zero when the data is handled. The infrastructure will take care of tearing down tasks and instances.

Update: Fargate might be a better alternative using ECS. Avoids the need to handle EC2.

Look at using Fargate tasks instead of spinning up EC2. I gave a talk about using this approach for exporting RDS MySQL databases, you can use the same approach for loading data to Postgres

https://slides.sathyasays.com/aws-fargate-ec2/

Batch and Fargate are similar items, in fact Fargate can be run in Batch or ECS. Ideally, I would use Fargate for event based triggers that could happen at any moment and Batch for cron based items that occur at set intervals. Cool, thing about Batch is you can run it on an EC2 and only get charged for however long it runs, so you could have a beefier EC2 and use spot pricing to to lower your costs.

Just my $0.02