- Newest
- Most votes
- Most comments
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:
-
Set up an S3 event notification to trigger a Lambda function when files are uploaded to your input bucket.
-
This Lambda function will:
- Start your RDS database and EC2 instance
- Initiate your existing process to move files to SFTP
-
Create another Lambda function triggered by S3 events on your output bucket (where processed files return).
-
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:
-
Modify your existing Lambda function that moves files to SFTP to start the RDS and EC2 instances before processing.
-
Create a new Lambda function to stop the instances when processing is complete.
-
Use the AWS SDK within these Lambda functions to start and stop the RDS and EC2 instances.
-
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
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
Relevant content
asked a year ago
- AWS OFFICIALUpdated 10 months ago
