HOW To SAVE OUR CLIENTS MONEY BY AUTOMATING THE START AND STOP OF EC2 INSTANCES ON AWS BY USING LAMBDA

In this article I will show you how to create AWS Lambda using Python to stop EC2 instances in all regions in one go. The purpose of this Lambda function is to reduce a cost of AWS account which is used for development/POC (proof of concept) in organization. The Lambda function will get triggered on scheduled time and stop the running EC2 instances intentionally.

WHAT WE’LL BE DOING:

  1.  Create a custom AWS Identity and Access Management (IAM) policy and execution role for your Lambda function.
  2. Create Lambda functions that stop and start your EC2 instances.
  3. Test your Lambda functions.
  4. Create CloudWatch Events rules that trigger your function on a schedule.

STEP 1: CREATE IAM ROLE FOR LAMBDA EXECUTION

1. Create a policy:
a. Click on create policy

b. Paste the below code
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    },{
      "Effect": "Allow",
      "Action": [
        "ec2:Start*",
        "ec2:Stop*"
      ],
      "Resource": "*"
    }  ] }
c. Type the policy name and click on create policy.
2. Create a IAM Role and attach the above policy that we have created.

a. Click on create role.
b. Select the lambda
c. Select the policy that we have created in previous step. d. Type the role name an click on create role

Step 2: Create Lambda Function for EC2 Stop Instances

  1. Click on create function.
  2. Type the function name.
  3. Select the Runtime —> Python 3.8
  4. Select the existing create role that we have created in the first step.
  5. Paste the below code in lambda_function.py file and change the region and instances id.
import boto3
region = 'us-west-1'
instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))

Step 3: Create Lambda Function for EC2 Start Instances

  1. Click on create function.
  2. Type the function name.
  3. Select the Runtime —> Python 3.8
  4. Select the existing create role that we have created in the first step.
  5. Paste the below code in lambda_function.py file and change the region and instances id.
import boto3
region = 'us-west-1'
instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']
ec2 = boto3.client('ec2', region_name=region)

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

Step 4: The last part is to add trigger to the function.

a. Click on add trigger
b. Select Cloudwatch Events
c. Click on Create New Rule
d. Type Rule Name and it’s description.
e. Select Schedule expression
f. Type the expression as per your need in below mention format.
   example:  CRON(0 17 ? * MON-FRI) 

100% LikesVS
0% Dislikes