In this article, we are going to cover a schedule ECS stop and start automatically using Lambda.
ECS – ECS is a fully managed container orchestration service provided by Amazon Web Services. It enables you to run, stop, and manage Docker containers on a cluster of virtual machines that are managed by AWS. ECS simplifies the process of deploying, managing, and scaling containerized applications.
Eventbridge – Amazon EventBridge is a serverless service that helps you connect different parts of your applications and services together using events. In other words, it’s an event bus that lets various components communicate by emitting (sending) and responding to events.
Lambda – AWS Lambda is a serverless computing service offered by AWS that allows you to run code without provisioning or managing servers. That enables developers to build and deploy applications and services quickly and efficiently by focusing on writing code, without the need to manage infrastructure.
If you would like to follow a video tutorial please use our YouTube video.
The IAM policy and the lambda code are available in the https://github.com/easydeploy-cloud/ecs-auto-start-stop repository.
Create policy
Go to >> IAM services, click >> policies, click >> create policy.
Choose >> lambda in service, click >> json.
Add scripts in the policy, and click >> next.
Type >> policy name, (I am giving “ECSservicestart-stop”), click >> create policy
we successfully created a policy, now we want to attach the policy to the role
click >> roles, click >> create roles, choose >> aws service
In service or use case, choose >> lambda and click >> next.
In permission, add the created policy and click >> next
Give a name to the role, I am giving “ECS“, click >> create role
Create lambda
Go to lambda service, click >> create function, and give a function name (i am giving “ECSservicestart-stop“).
In language , choose >> python, click >> change default execution role, click >> use an existing role, choose your role that we already created.
Click >> create function.
Now we successfully created the lambda function, erase the default code.
Add the new lambda code here.
You have to change the cluster_name and service_name with your values. after you added the values, click >> deploy button
Create event bridge
Navigate to Amazon EventBridge section and Click >> Rules under the Buses section and click Create >> Rule.
In name,type >> ECSService-stop, In rule type, click >> schedule, click >> continue to create rule
Provide the Cron expression for when to trigger to lambda for stop the services. For example (45 9 * * ? *) → It refers to ‘every day at 3.15pm’ in our time(IST). Click >> next
Select >> AWS service in the target type, select >> lambda function in a target, and select your created lambda function.
Click >> additional settings, In configure target input, select >> constant (JSON text)
Add these lines in the constant JSON, and click >> next and click >> create rule
we successfully created a role for the stop event, now we want to create another rule for the start event
Click >> create rule, In name,type >> ECSService–start, In rule type, click >> schedule, click >> continue to create rule
Provide the Cron expression for when to trigger to lambda for stop the services. For example (35 9 * * ? *) → It refers to ‘every day at 3.05 pm’ in our time(IST). Click >> next
Select >> AWS service in the target type, select >> lambda function in a target, and select your created lambda function.
Click >> additional settings, In configure target input, select >> constant (JSON text)
Add these lines in the constant JSON, and click >> next and click >> create rule
Now we successfully created two rule.
Check lambda function
Go to lambda services, click your created function, click >> triggers and click >> configuration.
The event rules are successfully attached
Check lambda log
Now we want to check if the event was successfully executed or not, Go to Cloudwatch services
Click >> log groups
Here it will show the lambda function log group
Swipe down, click >> log streams, here we have two log events. the start event was executed at 3.05 pm
The stop event was executed at 3.15 pm.
Automating the start and stop of Amazon ECS tasks using AWS Lambda and Events is an effective way to optimize resource usage and reduce costs. By scheduling these operations, businesses can ensure their ECS clusters run only during necessary periods, leading to significant savings. This automation not only streamlines operations but also allows for more efficient resource management, aligning cloud expenses with actual usage patterns.