Listen to this Post
In this article, Darryl R., a Cloud Solutions Architect at Ciena, discusses how to avoid paying for idle AWS Elastic Container Service (ECS) resources by automating their start/stop scheduling using AWS serverless components. The key tools highlighted are AWS Eventbridge and AWS Lambda. Eventbridge can schedule actions and react to events, while Lambda can trigger business logic. Together, they enable the automation of scaling ECS services based on events from the Simple Notification Service (SNS).
Practical Implementation
Here’s a step-by-step guide with verified commands and code snippets to implement this solution:
1. Create an AWS Lambda Function:
- Use the AWS CLI to create a Lambda function:
aws lambda create-function --function-name StopECS --runtime python3.8 --role arn:aws:iam::123456789012:role/lambda-execution-role --handler lambda_function.lambda_handler --zip-file fileb://function.zip
2. Write the Lambda Function:
- Below is a Python example to stop ECS services:
import boto3</li> </ul> def lambda_handler(event, context): ecs = boto3.client('ecs') cluster_name = 'your-cluster-name' service_name = 'your-service-name' ecs.update_service( cluster=cluster_name, service=service_name, desiredCount=0 ) return { 'statusCode': 200, 'body': 'ECS service stopped successfully' }3. Set Up AWS Eventbridge Rule:
- Create an Eventbridge rule to trigger the Lambda function at a specific time:
aws events put-rule --name "StopECSRule" --schedule-expression "cron(0 22 * * ? *)" --state ENABLED
- Add Lambda as a Target to Eventbridge Rule:
– Link the Lambda function to the Eventbridge rule:
aws events put-targets --rule StopECSRule --targets "Id"="1","Arn"="arn:aws:lambda:us-east-1:123456789012:function:StopECS"
5. Test the Setup:
- Manually trigger the Eventbridge rule to ensure the Lambda function stops the ECS service as expected.
What Undercode Say
Automating the start/stop scheduling of AWS ECS services is a cost-effective way to manage cloud resources. By leveraging AWS Eventbridge and Lambda, you can ensure that your ECS services are only running when needed, reducing unnecessary costs. This approach can be extended to other AWS services like EC2 instances, RDS databases, and more. Here are some additional commands and tips to enhance your automation:
- Check ECS Service Status:
aws ecs describe-services --cluster your-cluster-name --services your-service-name
-
Start ECS Service:
aws ecs update-service --cluster your-cluster-name --service your-service-name --desired-count 1
-
Monitor AWS Costs:
Use AWS Cost Explorer to track your savings:
aws ce get-cost-and-usage --time-period Start=2023-10-01,End=2023-10-31 --granularity MONTHLY --metrics "UnblendedCost"
- Automate EC2 Instances:
Use Lambda to stop/start EC2 instances:
import boto3 def lambda_handler(event, context): ec2 = boto3.client('ec2') instance_id = 'your-instance-id' ec2.stop_instances(InstanceIds=[instance_id])- Schedule RDS Stop/Start:
Use Eventbridge to manage RDS instances:
aws rds stop-db-instance --db-instance-identifier your-db-instance
By implementing these practices, you can optimize your cloud infrastructure, reduce costs, and improve operational efficiency. For further reading, check out the AWS Documentation on Eventbridge and Lambda.
References:
Hackers Feeds, Undercode AI

- Create an Eventbridge rule to trigger the Lambda function at a specific time:


