Skip to content
This repository has been archived by the owner on Nov 21, 2021. It is now read-only.

Start and Stop your EC2 Instance with tags, cron expressions and AWS Lambda.

Notifications You must be signed in to change notification settings

jansauer/ec2-start-stop-lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Timezone

You are able to set the TIMEZONE via the aws lambda Enviornment. The Timezone is implemented with the help of Moment Timezon

Contributing

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

License and Authors

Author: Jan Sauer <jan@jansauer.de> (https://jansauer.de)

Copyright 2018, Jan Sauer <jan@jansauer.de> (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.

About

Start and Stop your EC2 Instance with tags, cron expressions and AWS Lambda.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published