Wednesday, September 15, 2021

How to Automatically Start and Stop EC2 instances

How to Automatically Start and Stop EC2 instances in AWS

In this tutorial we going to configure EC2 instances stop and start service automatically by the help of lambda function and Cloud Watch Event Service.

Before starting the article lets look into the following services, which service will help to complete our task.

  • EC2 instance
  • IAM policy
  • IAM Role
  • LambdaFuntcion
  • CloudWatch Event

Task 1) Launch an EC2 Instance & keep instance id and aws region id.

  • AWS instance ID - i-00c51728cd1861f72
  • AWS Region     - ap-south-1



Task 2) Go to the IAM Policy & Click on Create.


  • Select EC2 in service section.
  • Select Stop_instance and Start_instance.

 


  • Click on Add ARN and write the AWS instance ID & AWS Region

 



  • Leave Default Request Condition
  • Click on Next:Tag
  • Click on Next:Review
  • Write the Policy Name, Description and Click on Create Policy.

 


Task 3) Create IAM New Role for Lambda Function.


 
  • Select EC2 Service, Select Lambda and click on Next:Permission. 



  • Search The Policy name which you have created in Task 2 than, Click on Next:Tags


 

  • Add tags
  • In Review, Just write the Role Name and Description than, click on Create Role


Task 4) Go to the AWS Lambda Service than, Click on create function.


 

  • Select "Author From Scratch"
  • Write the Function Name : Stop_Ec2_Instances
  • Select Runtime python3.9

 


  • Select Execution role ( Which you have created in Task 3 ) than, Click on Create.

 


  • Copy the Below code and paste into lambda_function file.

Note:- Change the region and instances value according your value.
Note:- If you have multiple Instances to run the stop script just add comma between instance id.

Example :-  instances = ['i-00d637d5329909a8b','i-28js7d532990942v']

~~~~~~~~~~~~~   Stop the instances code ~~~~~~~~~~~~~   

import boto3
region = 'ap-south-1'
instances = ['i-00d637d5329909a8b']
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))

~~~~~~~~~~~~~   Stop the instances code ~~~~~~~~~~~~~   

 


 

  • Now, Click on Deploy and than click on Test and write the event name.

 


  • Again Click on the Test Button and check the EC2 is stopped.


Task 5) Go to the cloud-watch and click on Event

 



  • Click on Create Rule >>  Schedule >> Cron expression   


 

Write the crontab, Cron Will work on UTC time (you need convert time UTC to IST)

  • Add Target as a Lambda Function than click on configure.


 

  • Write the Event rule name.



Task 6) Below is Start EC2 instances Script, please do configure as same of Stop process

~~~~~~~~~~~~~   Start the instances:- ~~~~~~~~~~~~~  


import boto3
region = 'ap-south-1'
instances = ['i-00d637d5329909a8b']
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.start_instances(InstanceIds=instances)
    print('started your instances: ' + str(instances))


~~~~~~~~~~~~~   Start the instances:- ~~~~~~~~~~~~~    




 

No comments:

Post a Comment

testing