Listen to this Post

Paying for idle AWS Elastic Container Service (ECS) resources is a waste of money. Fortunately, you can automate the shutdown of unused services using AWS serverless components like EventBridge and Lambda.
How It Works
AWS EventBridge Scheduler triggers actions on a schedule, while Lambda executes business logic to stop or start ECS services. By combining these services, you can automate resource management efficiently.
You Should Know: AWS Automation Steps
1. Set Up EventBridge Rule
Create an EventBridge rule to trigger Lambda on a schedule (e.g., non-working hours).
aws events put-rule --name "StopECSServicesAtNight" --schedule-expression "cron(0 20 ? )"
2. Create Lambda Function
Write a Lambda function (Python) to stop ECS services:
import boto3
def lambda_handler(event, context):
ecs = boto3.client('ecs')
response = ecs.update_service(
cluster='your-cluster-name',
service='your-service-name',
desiredCount=0 Stops all tasks
)
return response
3. Add Permissions
Ensure Lambda has the required IAM permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecs:UpdateService",
"ecs:DescribeServices"
],
"Resource": ""
}
]
}
4. Trigger Lambda via EventBridge
Link the EventBridge rule to Lambda:
aws events put-targets --rule "StopECSServicesAtNight" --targets "Id"="1","Arn"="your-lambda-arn"
5. (Optional) Use SNS for Notifications
Configure SNS to alert when scaling actions occur:
aws sns create-topic --name "ECS-Scaling-Alerts" aws sns subscribe --topic-arn "your-sns-topic-arn" --protocol "email" --notification-endpoint "[email protected]"
What Undercode Say
Automating AWS ECS scaling with EventBridge and Lambda ensures cost efficiency. Below are additional useful commands for managing AWS resources:
Linux/Cloud Commands
- List running ECS tasks:
aws ecs list-tasks --cluster your-cluster --service your-service
- Force-stop a task:
aws ecs stop-task --cluster your-cluster --task your-task-id
- Check CloudWatch logs for Lambda:
aws logs get-log-events --log-group-name "/aws/lambda/your-lambda" --log-stream-name "latest"
Windows AWS CLI Setup
- Install AWS CLI on Windows:
msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi
- Configure AWS credentials:
aws configure
Prediction
As cloud costs rise, more organizations will adopt automated scaling solutions. Serverless architectures will dominate cost optimization strategies in AWS, Azure, and GCP.
Expected Output:
- Idle ECS services stopped automatically.
- Cost savings reflected in AWS billing reports.
- Notifications via SNS for scaling events.
References:
Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅


