Code Monkey home page Code Monkey logo

lambda-fuction-to-stop-start-an-instance's Introduction

Lambda-fuction-to-stop-start-an-instance

AWS Lambda is Amazon’s serverless compute service. You can run your code on it without having to manage servers or even containers. It will automatically scale depending on how much work you feed into it. And it build event-driven functions for easy communication between decoupled services. Reduce costs by running applications during times of peak demand i.e you need to pay only for the time taken to execute the code. The key features of Lambda function are :

  • Run code without provisioning or managing infrastructure. Simply write and upload code as a .zip file or container image
  • Automatically respond to code execution requests at any scale, from a dozen events per day to hundreds of thousands per second.
  • Save costs by paying only for the compute time you use by per-millisecond instead of provisioning infrastructure upfront for peak capacity.
  • Optimize code execution time and performance with the right function memory size.

Synopsis

First we will be creating a lambda function to stop/start testing instance in order to reduce cost. Then we will create a python code to stop, start instance and upload it to Lambda. Now we will apply a trigger on lambda function using AWS cloudwatch events. We have crontab facility in AWS cloudwatch, so we schedule a cronjob to apply the trigger on lambda at the demanding time.

Requirements

  • Boto module

    # pip3 install boto3
    
    • Python code to stop/start instance (stop.py, start.py)

Provisioning

1. Create an IAM Role

An IAM role with "AmazonEC2FullAccess" premission is created to allow Lambda functions to call AWS services.

role1 role2 role3

2. Create a Lambda function

For that navigate to Lambda >> Functions >> create function - provide the Basic information, select the pre-created IAM role "ec2-stop-start" >> Create Function

lambda10

The created lambda function interface will be as below :

lambda11

Now replace the default code with our python code to stop the instance and click on the "Deploy" button to update/upload the code to the lambda fuction.

lambda13

3. Apply Trigger

Now we will apply a trigger on Lambda function using the AWS Cloudwatch to run the code on our time demanded. We have crontab feature in AWS Cloudwatch to schedule a cronjob.

Navigate to AWS Cloudwatch >> Events >> Rule >> Create Rule

lambda14

Select Cron Expression under Schedule and set a cronjob. Now set up Trigger by Adding Target for lambda function and select the precreated Lambda function "test-instance-stop"

lambda15

On selecting the created rule we can see next 10 Trigger Dates listed

Screenshot from 2022-02-12 16-20-05

NOTE :: Follow the same proceedure to create a lambda function to start an instance by replacing a single line in the code "ec2.stop_instances(InstanceIds=[ instance_Id ])" to "ec2.start_instances(InstanceIds=[ instance_Id ])".

Result

A lambda function is created to stop and start a testing instance during times of peak demand by reducing the cost.

lambda18

start

lambda-fuction-to-stop-start-an-instance's People

Contributors

swathikarun 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.