Giter VIP home page Giter VIP logo

atdavidpark / ec2-cost-optimization-using-lambda Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sebinxavi/ec2-cost-optimization-using-lambda

0.0 1.0 0.0 23 KB

Using the lambda function with python code, we will be able to shut down the EC2-instances automatically. The python code that can be executed automatically using the aws cloudwatch rules is provided in this repository..

Python 100.00%

ec2-cost-optimization-using-lambda's Introduction

EC2 Cost Optimization Using AWS Lambda Function

Using the lambda function with python code, we will be able to shut down the EC2-instances automatically. The python code that can be executed automatically using the aws cloudwatch rules has been provided below.

General Information

To save costs on AWS EC2, it may be necessary in some cases to automatically shut down EC2 instances during off-peak hours. For example, if the developers have a few EC2 instances running, we can automatically shut them down during their non-working hours using lambda functions and cloudwatch rules.

Technology Used

  • AWS Lambda
  • AWS Cloudwatch

About the python code

The python codes added below will stop and start all ec2-instances every day at 12:15 AM GMT and 08:15 AM GMT respectively with instance tags key: "env" and value: "testing"

The python code has been written below::

1. To Stop the Instance

import boto3

REGION = "ap-south-1"

def lambda_handler(event, context):
    
  ec2 = boto3.client('ec2',region_name=REGION)

  all_ec2 = ec2.describe_instances(
      
      Filters=[
        {'Name':'tag:env', 'Values':["testing"]}
      ]
  )

  for instance in all_ec2['Reservations'][0]['Instances']:
    
    print("Stopping Ec2 : {} ".format( instance['InstanceId'] ))
    
    ec2.stop_instances(InstanceIds=[ instance['InstanceId'] ])

2. To Start the Instance

import boto3

REGION = "ap-south-1"

def lambda_handler(event, context):
    
  ec2 = boto3.client('ec2',region_name=REGION)

  all_ec2 = ec2.describe_instances(
      
      Filters=[
        {'Name':'tag:env', 'Values':["testing"]}
      ]
  )

  for instance in all_ec2['Reservations'][0]['Instances']:
    
    print("Starting Ec2 : {} ".format( instance['InstanceId'] ))
    
    ec2.start_instances(InstanceIds=[ instance['InstanceId'] ])

Steps to be done::

1. Create an IAM role granting EC2 Full access to Lambda

Create the IAM role by selecting the AWS Service - Lambda

alt text

Grant EC2 Full access to Lambda

alt text

2. Create Lambda Function with both the start and stop EC2 Python Codes

Create the Lambda Function

alt text

Add the python code to stop/start the instance in separate lambda functions

alt text

Check the added lambda functions

alt text

3. Create the cloudwatch rules to automatically run the lambda function

Cloudwatch rule to stop the ec2-instances at 12:15 AM GMT every day

alt text

alt text

Cloudwatch rule to start the ec2-instances at 08:15 AM GMT every day

alt text

alt text

Check the Cloudwatch rules

alt text

Result:

The Cloudwatch rules will execute the python codes added in the Lambda functions and it will stop and start all ec2-instances every day at 12:15 AM GMT and 08:15 AM GMT respectively with the instance tags key: "env" and value: "testing"

Author

Created by @sebinxavi - feel free to contact me and advise as necessary!

ec2-cost-optimization-using-lambda's People

Contributors

sebinxavi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.