A Cloud Architect Company
Amazon Web Services

How to schedule ecs stop and start automatically using Lambda

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.

IAM policies

Add scripts in the policy, and click >> next.

IAM policies

Type >> policy name, (I am giving “ECSservicestart-stop”), click >> create policy

IAM policies

we successfully created a policy, now we want to attach the policy to the role

click >> roles, click >> create roles, choose >> aws service

IAM roles

In service or use case, choose >> lambda and click >> next.

IAM roles

In permission, add the created policy and click >> next

IAM roles

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.

Lambda function

Now we successfully created the lambda function, erase the default code.

Lambda function

Add the new lambda code here.

Lambda function

You have to change the cluster_name and service_name with your values. after you added the values, click >> deploy button

Lambda function

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

Eventbridge

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

Cron expression

Select >> AWS service in the target type, select >> lambda function in a target, and select your created lambda function.

lambda in Eventbridge

Click >> additional settings, In configure target input, select >> constant (JSON text)

Eventbridge

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 >> ECSServicestart, In rule type, click >> schedule, click >> continue to create rule

Eventbridge

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

Eventbridge

Select >> AWS service in the target type, select >> lambda function in a target, and select your created lambda function.

Eventbridge

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

Eventbridge

Now we successfully created two rule.

EventBridge

Check lambda function

Go to lambda services, click your created function, click >> triggers and click >> configuration.

The event rules are successfully attached

Lambda function

Check lambda log

Now we want to check if the event was successfully executed or not, Go to Cloudwatch services

Click >> log groups

Cloudwatch

Here it will show the lambda function log group

Cloudwatch

Swipe down, click >> log streams, here we have two log events. the start event was executed at 3.05 pm

Cloudwatch

The stop event was executed at 3.15 pm.

Cloudwatch

 

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.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

back to top
advanced-floating-content-close-btn

Contact Us to save your AWS bill by 40%

X