You are on page 1of 5

DevOpsCheetah

Ho me Linux Clo ud  Pr o g r a mming  Mo nit o r ing  CI/CD  Ca t eg o r ies 

How to start-stop an EC2 instance via


Lambda function
 May 22, 2021  Atul Rajput  0 Comment

First, we have to create I am role that role has permission to start and stop ec2
instance.

We use cookies on our website to give you the most relevant experience by remembering your preferences
Second, we have to create lambda functions.
and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit Cookie Settings Accept All
"Cookie Settings" to provide a controlled consent.
Third, we have to create CloudWatch event for cron jobs.

Now create two lambda functions, one for start and second for stop to ec2
instance.

How to create lambda functions?

First click on create function

Now define the name of function, runtime(language), select an existing role which
we had created and click on create function.

Now, define the lamda_function_code:-

import bot o3
region = ‘ap-sout h-1’
inst ances = [‘inst ance’]
ec2 = bot o3.client (‘ec2’, region_name=region)
def lambda_handler(event , cont ext ):
    ec2.st art _inst ances(Inst anceIds=inst ances)
   print (‘st art ed your inst ances: ‘ + st r(inst ances))

After this click on deploy and give the name to the event, and test the event.

Now we are going to define the cron job, first go to CloudWatch and select rule
under the event section. Select schedule section and then cron section:-

Now click on add targets and select lambda function in drop down section, define
the details like which lambda function you want to trigger.

How t o Inst all MongoDB on AMAZON Linux, Cent OS7, and Redhat .

T ags: aws lambda function to start and stop an ec2 instance ,

aws lambda function to start and stop ec2 instance , ec2 instance schedule start stop ,

lambda function to start and stop ec2 instance , schedule ec2 instance start stop ,

start stop ec2 instance lambda

Leave a Reply
Your email address will not be published. Required fields are marked *
Comment

Name *

Email *

Websit e

Save my name, email, and websit e in t his browser for t he next t ime I comment .

POST COMMENT

 Previous Post Next Post 

Related Posts
Blogs About Us Contact Us Privacy Policy    
© 2021. DevOpsCheetah.com

PDFmyURL.com - convert URLs, web pages or even full websites to PDF online. Easy API for developers!

You might also like