Skip to content

Start & Stop of EC2 instance using tags in Lambda function

0

Is there is any way to start & stop an EC2 instance using the tags in lambda function. Currently i am using a lambda function to start & stop an EC2 instance by the instance id. if the instance got replaced, the lambda function is not working. So inorder to fix this issue, i am curious on setting up a lambda function to start & stop the instance by using the Tags instead of instance id.

asked 4 years ago2.5K views
3 Answers
2
Accepted Answer

Not directly with the API. You will need to update your Lambda function to DescribeInstances and filter on the tag key/value combination you desire and pass the returned instance ID to the relevant StartInstances/StopInstances method.

AWS
answered 4 years ago
1

if you want to make it easier for yourself, look at this:

https://aws.amazon.com/solutions/implementations/instance-scheduler/?nc1=h_ls

answered 4 years ago
0

This is an old question but a good one, because it gets at a problem with a lot of the examples found online for stopping and starting EC2 instances.

if the instance got replaced, the lambda function is not working.

If you see a solution that requires the Instance ID, then the premise is faulty. In the cattle-versus-pets dichotomy, any EC2 instance is replaceable. It might also be replaced by AWS, as in the case of failing hardware, which generates a notification of pending maintenance.

In addition to modifying your own Lambda function to derive a list of Instance IDs from filter criteria such as tags, or installing AWS's Instance Scheduler tool, consider github.com/sqlxpert/lights-off-aws , an open-source tool I wrote to stop and start EC2 instances and RDS/Aurora databases based on cron schedules in resource tags. It's different from hard-coding schedules in EventBridge Scheduler rules that trigger a Lambda function, and different from tagging instances with schedule profiles, such as "weekday". Instead, you put the schedule you want in the intance's sched-start and sched-stop tags. Assuming that you are creating your instances using an infrastructure-as-code system, specify the tags there so that new instances in a given category (suitable for stopping and starting, i.e., not production) will be tagged.

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.