Code Monkey home page Code Monkey logo

ec2-start-stop-lambda's Introduction

EC2 Instance Start/Stop

A Serverless Application that starts and stops your EC2 instance with the help of tags , cron expressions and AWS Lambda.

Usage

You tag your instances with when you whant them stared or stoped. This function then runs every few minutes (default is 15) and looks for instances with those instruction tags (default Scheduled_Start and Scheduled_Stop). For any tag it finds the cron expression gets parsed and checked if it maches any time since the last run of the function. If that is the case the instance gets started or respectively stoped.

As the function only runs every few minutes it is unlikely that your instance gets startet exactly on the second but rather the next time the function runs. If nessesary it is possible to influence the delay with the call rate of the function.

Time is a difficult thing, even in world of global systems administration. Thats why times are always in UTC

Supported cron format

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, optional)

Supports mixed use of ranges and range increments (L, W and # characters are not supported currently). If you prefer a more a visual approach have a look at crontab-generator. Or just go with the the usual suspects:

  • Start 0 0 7 * * 1-5 and stop 0 0 19 * * 1-5 so the instance runs monday to friday 7 to 18 o'clock.
  • Start 0 0 6 * * 1 the instance monday morning at 6 and run until friday at 20 o'clock 0 0 21 * * 5.

Don't forget we are still in UTC

Set me up

sam is the AWS CLI tool for managing Serverless applications. With sam's help we package the code as a zip artifact, upload it to S3 and deploy it to Lambda using AWS CloudFormation.

$ npm install -g aws-sam-local
$ npm install --production
$ sam package --template-file template.yaml --s3-bucket some-bucket --output-template-file packaged.yaml
$ sam deploy --template-file ./packaged.yaml --stack-name ec2startstop --capabilities CAPABILITY_IAM

If want to stick to the basics you can archive the same with the commands aws cloudformation package and aws cloudformation deploy.

Contributing

Pull requests are always welcome. I'm grateful for any help or inspiration.

License and Authors

Author: Jan Sauer <[email protected]> (https://jansauer.de)

Copyright 2018, Jan Sauer <[email protected]> (https://jansauer.de)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

ec2-start-stop-lambda's People

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.