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

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

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

Stop and Start…


This content originally appeared on DEV Community and was authored by Anuvindh Sankaravilasam

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

Comments Screenshots
Select AWS Service Image service
Then Choose Lambda and click next Image lamda2
Search for AWSLambdaBasicExecutionRole and select it Image policy
Add Stopinator Policy also Image role2
Name the role and Click create role Image namerole

Create an Lambda Function

STOP Function

Comments Screenshots
1. Go to lambda dashboard and click create function Image function
2. Keep as Author from scratch and Name the function Image stop
3. Select Runtime Python 3.9
4. Permissions, select the created Role Image 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

Comments Screenshots
Name the Rule Image 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 cron Image cron
Select Target as your Lambda Function for Stoping Select Stopinator-II-Stop
Now click Create Image Create
Repeat the steps for Strating Instances

Congratulations you have successfully configured Stopinator 2.0


This content originally appeared on DEV Community and was authored by Anuvindh Sankaravilasam


Print Share Comment Cite Upload Translate Updates
APA

Anuvindh Sankaravilasam | Sciencx (2022-02-12T10:19:14+00:00) Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – STOPINATOR 2.0 : Day 7. Retrieved from https://www.scien.cx/2022/02/12/stop-and-start-ec2-instances-at-predefined-times-using-lambda-and-eventbridge-stopinator-2-0-day-7/

MLA
" » Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – STOPINATOR 2.0 : Day 7." Anuvindh Sankaravilasam | Sciencx - Saturday February 12, 2022, https://www.scien.cx/2022/02/12/stop-and-start-ec2-instances-at-predefined-times-using-lambda-and-eventbridge-stopinator-2-0-day-7/
HARVARD
Anuvindh Sankaravilasam | Sciencx Saturday February 12, 2022 » Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – STOPINATOR 2.0 : Day 7., viewed ,<https://www.scien.cx/2022/02/12/stop-and-start-ec2-instances-at-predefined-times-using-lambda-and-eventbridge-stopinator-2-0-day-7/>
VANCOUVER
Anuvindh Sankaravilasam | Sciencx - » Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – STOPINATOR 2.0 : Day 7. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/12/stop-and-start-ec2-instances-at-predefined-times-using-lambda-and-eventbridge-stopinator-2-0-day-7/
CHICAGO
" » Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – STOPINATOR 2.0 : Day 7." Anuvindh Sankaravilasam | Sciencx - Accessed . https://www.scien.cx/2022/02/12/stop-and-start-ec2-instances-at-predefined-times-using-lambda-and-eventbridge-stopinator-2-0-day-7/
IEEE
" » Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – STOPINATOR 2.0 : Day 7." Anuvindh Sankaravilasam | Sciencx [Online]. Available: https://www.scien.cx/2022/02/12/stop-and-start-ec2-instances-at-predefined-times-using-lambda-and-eventbridge-stopinator-2-0-day-7/. [Accessed: ]
rf:citation
» Stop and Start EC2 instances at predefined times Using Lambda and EventBridge – STOPINATOR 2.0 : Day 7 | Anuvindh Sankaravilasam | Sciencx | https://www.scien.cx/2022/02/12/stop-and-start-ec2-instances-at-predefined-times-using-lambda-and-eventbridge-stopinator-2-0-day-7/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.