Giter Club home page Giter Club logo

ec2-autostopstart's Introduction

EC2-AutoStopAndStart

πŸ’²πŸ’² SAVE YOUR MONEY πŸ’²πŸ’²

You can save your money by starting EC2 instances only when you really need it. This for a negligible amount, less than $1 a year, needed for the small AWS Lambda. This Node.js application give you the ability to auto stop and start EC2 instances which are tagged with auto:stop and auto:start.

Inspired by existing python and nodejs applications. In particular this one : https://schen1628.wordpress.com/2014/02/04/auto-start-and-stop-your-ec2-instances/

Main dependencies

Examples:

  • Auto-stop instance every day at 7pm : 0 19 * * *
  • Auto-start instance every day at 8:30am : 0 30 8 * * *

With a default margin of 15 minutes, EC2 instances will be stop between 7pm and 7:15pm and start between 8:15am and 8:30am. The margin time can be set with a variable in the source code. It must be linked to the lambda schedule settings.

Values of tags are CRON expressions : http://www.cronmaker.com/. Seconds are optionals.

❗ Never forget that AWS Lambda works with UTC Time ❗

AWS Lambda Configuration

create a lambda with the AWS console (https://eu-west-1.console.aws.amazon.com/lambda/):

  • Choose "Blank function" blueprint
  • Configure a trigger:
    • choose CloudWatch Events
    • create a new rule, give it a significant name and description
    • choose rule type: "Schedule expression"
    • insert a schedule expression: rate(5 minutes) or what you need...
    • click "Enable trigger" now or later if you need test

schedule_1

  • Click "Next"
  • Configure function: give a name and a description for the lambda
  • Runtime: choose Node.js 6.10
  • Lambda function code: let "Edit code inline"

config

  • Lambda function handler and role:

    • Handler: let "index.handler"
    • Role: choose "create a custom role":
    • In the opened page, choose "Create a new IAM role"
    • Give it a name
    • Click View policy document
    • Click on the Edit button, confirm, and paste this policy:
    {
     "Version": "2012-10-17",
     "Statement": [
       {
         "Effect": "Allow",
          "Action": [
                      "ec2:DescribeRegions",
                      "ec2:DescribeInstances",
                      "ec2:StartInstances",
                      "ec2:StopInstances",
                      "ec2:CreateNetworkInterface",
                      "ec2:DeleteNetworkInterface",
                      "ec2:DescribeNetworkInterfaces",
                      "logs:CreateLogGroup",
                      "logs:CreateLogStream",
                      "logs:PutLogEvents"
                  ],
         "Resource": "*"
       }
     ]
    }
    • Don't forget to click on the Allow

    image

  • Advanced settings:

    • let the default memory configuration (128Mo)
    • Adjust the time-out to 1 minute

    image_4

    • If you have a specific VPC configuration, you can set it:

    image_5

  • Click on Next

  • Click on Create function

image_6

Lambda deployment

  1. Clone this repository

  2. Run npm install

  3. Add a file: lambda-config.js

    and fill the necessary informations

    module.exports = {
        region: 'YOUR_REGION',
        functionName: 'YOUR_LAMBDA_NAME'
    };

    You can create the lambda "on the fly" with more parameters. If you need it, go to https://github.com/ThoughtWorksStudios/node-aws-lambda

    Thanks to ThoughtWorksStudios for this great module !

  4. If you have an AWS profile with the necessary rights installed, you can deploy your lambda with the deploy gulp task.

    image_1

    It's possible to fill the credentials directly in the lambda-config.js file .

  5. Back to the AWS Lambda console

    Now, you can view the uploaded code (a refresh can be needed):

    lambda_config

    And you can test the Lambda:

    image_2

    If all is OK, after some seconds you can see this output:

    image_3

    Click on logs to view details with CloudWatch

That's all ☺️

ec2-autostopstart's People

Stargazers

 avatar  avatar

Watchers

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