Blog Details

  • Home-iCTPro
  • 100 Days of Cloud
  • How to Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – 100 days of cloud: Day 7

How to Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – 100 days of cloud: Day 7

+1
0
+1
0
+1
0
+1
0
+1
0

Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – STOPINATOR 2.0 : Day 7

#webdev#python#productivity#devops

DAY 7 – Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – STOPINATOR 2.0

☁️100 days of Cloud- Day Two
Follow Me on Twitter

Image Cover

Tweet This Blog – Read on GitHub – Read On iCTPro.co.nz


Stop and Start EC2 instances at predefined times Using Lambda and EventBridge .

Reduce usage of Amazon Elastic Compute Cloud (Amazon EC2) usage by starting and stopping EC2 automatically.

Step 1 Creating a IAM Poly and execution role for Lambda.

Create IAM Policy

Goto IAM in AWS console and Click Policies
and Click Create Policy
Click on the JSON tab then copy and paste 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": "*"
    }
  ]
}

Name the policy as Stopinator-II and click Create

Create IAM Role

Go to IAM Console and select Roles and click Create Roles

CommentsScreenshots
Select AWS ServiceImage service
Then Choose Lambda and click nextImage lamda2
Search for AWSLambdaBasicExecutionRole and select itImage policy
Add Stopinator Policy alsoImage role2
Name the role and Click create roleImage namerole

Create an Lambda Function

STOP Function

CommentsScreenshots
1. Go to lambda dashboard and click create functionImage function
2. Keep as Author from scratch and Name the functionImage stop
3. Select RuntimePython 3.9
4. Permissions, select the created RoleImage Permissions then click on **create function**

Goto Code and paste

import boto3
region = 'ap-southeast-2'
instances = ['i-xxxxxxxxxxxxxxxxxx,i-xxxxxxxxxxxxxxxxxx']
ec2 = boto3.client('ec2', region_name=region)

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

Save and Deploy
Dont forget to add your instance name & your region

START Function

Repeat the steps 1 to 4 and add below code to

import boto3
region = 'ap-south-east-2'
instances = ['i-xxxxxxxxxxxxxxxxxx,i-xxxxxxxxxxxxxxxxxx']
ec2 = boto3.client('ec2', region_name=region)

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

Dont forget to add your instance name & your region

Save and Deploy

Testing lambda functions

Select your function and click test and see the lambda function is working as indented.

Schedule time to turn ON and OFF EC2 using EventBridge

Goto EventBridge from AWS console and Click Create rule

CommentsScreenshots
Name the RuleImage name
Define Pattern, Select Schedule and enter your CRON time. I am keeping 6PM everyday as my stop time .Use this link to create your cronImage cron
Select Target as your Lambda Function for StopingSelect Stopinator-II-Stop
Now click CreateImage Create
Repeat the steps forStrating Instances

Congratulations you have successfully configured Stopinator 2.0