Skip to content

How to Trigger RDS and EC2 to automatically start up

0

I am looking for how I can automatically get my RDS and EC2 instance to automatically go from Stopped to Start state when files are uploaded to an S3 bucket. And then to turn both the RDS and EC2 instance back to a Stopped state once everything has processed. The system is setup where files are uploaded to an S3 bucket, which triggers a Lambda function to move the files out of AWS to an SFTP and then, once the files are processed by the SFTP, they are returned to a different S3 bucket. What is the most cost efficient way to setup these triggers? Please note - there is no set schedule I can use. I never know when files will be submitted to the S3 bucket, but it's intermittent enough that leaving the system running 24/7 isn't cost efficient.

asked 2 years ago437 views

2 Answers
0

To automatically start and stop your RDS and EC2 instances based on S3 file uploads, you can implement a serverless solution using AWS Lambda and Amazon EventBridge. Here's a cost-efficient approach:

  1. Set up an S3 event notification to trigger a Lambda function when files are uploaded to your input bucket.

  2. This Lambda function will:

    • Start your RDS database and EC2 instance
    • Initiate your existing process to move files to SFTP
  3. Create another Lambda function triggered by S3 events on your output bucket (where processed files return).

  4. This second Lambda function will:

    • Check if all expected files have been processed
    • If so, stop the RDS database and EC2 instance

To implement this:

  1. Modify your existing Lambda function that moves files to SFTP to start the RDS and EC2 instances before processing.

  2. Create a new Lambda function to stop the instances when processing is complete.

  3. Use the AWS SDK within these Lambda functions to start and stop the RDS and EC2 instances.

  4. Implement a tracking mechanism (e.g., DynamoDB table) to keep count of processed files and determine when all work is complete.

This approach is cost-efficient because:

  • You only pay for Lambda executions and minimal EventBridge usage
  • RDS and EC2 instances run only when needed
  • No need for constantly running schedulers or maintenance windows

Remember to set up appropriate IAM roles and permissions for your Lambda functions to interact with RDS, EC2, and S3. Also, consider implementing error handling and notifications in case of any issues during the start/stop processes.
Sources
Automatically stop and start an Amazon RDS DB instance using AWS Systems Manager Maintenance Windows - AWS Prescriptive Guidance
Automate starting and stopping AWS instances - Instance Scheduler on AWS
Schedule SQS to trigger lambda | AWS re:Post

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

0

Hello.

AWS re:Post Agent's answer will probably work, but I think it will be a hassle to create a mechanism to manage things like starting and stopping EC2 and RDS.
Why not use ECS Fargate standalone tasks and DynamoDB instead of EC2 or RDS?
The EventBridge rule detects that a file is created in S3 and starts processing by running an ECS Fargate standalone task.
ECS Fargate standalone tasks automatically terminate after execution completes, so unlike EC2, no action is required to explicitly stop them.
Additionally, by using DynamoDB, you can avoid incurring charges during times when no access is occurring, so there is no need to start and stop RDS.
https://docs.aws.amazon.com/AmazonS3/latest/userguide/ev-events.html
https://theekshanawj.medium.com/utilizing-amazon-eventbridge-to-trigger-ecs-tasks-from-s3-events-by-using-aws-cdk-43619db2f7f2

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.