Code Monkey home page Code Monkey logo

terraform-aws-lambda-scheduler's Introduction

terraform-aws-lambda-scheduler

Stop and start EC2 and RDS instances according to schedule via lambda and terraform.

Overview

The scheduler looks at the schedule tag to see if it needs to stop or start and instance. It works by setting a tag (default name schedule) to a string giving the stop and start time hour for each day.

A schedule tag for an EC2 instance is json and looks like:

{"mon": {"start": 7, "stop": 19},"tue": {"start": 7, "stop": 19},"wed": {"start": [9, 22], "stop": 19},"thu": {"start": 7, "stop": [2,19]}, "fri": {"start": 7, "stop": 19}, "sat": {"start": 22}, "sun": {"stop": 7}}

If you want to handle multiple stop/starts per day, you will need to pass a list in square brackets in the start/stop schedule.

You can also use 'daily' and 'workday'. 'workday' is a placeholder for monday - friday. This allow a much smaller configuration in tags. It is needed for configurations with multiple times for stoppping an instance, because a value of a tag is limitted to 254 characters. The data from daily or workday will be added to any possible existing data of the weekday.

Example:

{"daily": {"stop": [19,23,4]}, "sat": {"stop": 15},"sun": {"stop": [15]}}

A stop is planned for 4,19 and 23 for each day - including the additional value for saturday and sunday.

On a RDS instance the schedule tag is a string of keyword parameters separated by a space. To support multiple stop/start times per day, separate the hours with a /.

"mon_start=7 mon_stop=20 tue_start=7 tue_stop=20 wed_start=7/22 wed_stop=20 thu_start=7 thu_stop=2/20 fri_start=7 fri_stop=20"

NOTE:

  • This is because of restrictions in the characters the tags on RDS instance support.
  • The configuration format from RDS can be used in EC2 as well.

The scheduler can be configured to add a default schedule tag to EC2 and RDS instances it finds without a schedule tag. It ignores instances that are part of autoscaling groups assuming scheduling actions can be used to stop and start these instances.

Requirements

This module requires Terraform version 0.12.x or newer.

Dependencies

This module depends on a correctly configured AWS Provider in your Terraform codebase.

Usage

module "lambda-scheduler" {
  source = "neillturner/lambda-scheduler/aws"
  version = "0.x.0"
  schedule_expression = "cron(5 * * * ? *)"
  tag = "schedule"
  schedule_tag_force = "true"
  ec2_schedule = "true"
  rds_schedule = "true"
  default = "{\"mon\": {\"start\": [7], \"stop\": [19]},\"tue\": {\"start\": [7], \"stop\": [19]},\"wed\": {\"start\": [9, 22], \"stop\": [19]},\"thu\": {\"start\": [7], \"stop\": [2,19]}, \"fri\": {\"start\": [7], \"stop\": [19]}, \"sat\": {\"start\": [22]}, \"sun\": {\"stop\": [7]}}"
  time = "Europe/London"
  permissions_boundary = "arn:aws:iam::AWSACCTID:policy/optional-permissions-boundary-ARN"
}

variables

schedule_expression

The aws cloudwatch event rule schedule expression that specifies when the scheduler runs.

Default = "cron(5 * * * ? *)" i.e. 5 minuts past the hour. for debugging use "rate(5 minutes)" See ScheduledEvents

tag

The tag name used on the EC2 and RDS instance to contain the schedule string for the instance. default is 'schedule'

schedule_tag_force

Whether to force the EC2 and RDS instance to have the default schedule tag if no schedule tag exists for the instance.

Default is false. If set to true it with create a default schedule tag for each instance it finds.

exclude

String containing comma separated list of ECS2 and RDS instance ids to exclude from scheduling.

default

The default schedule tag containing json schedule information to add to EC2 or RDS instance when schedule_tag_force set to true.

NOTE: On a RDS instance the tag is converted to a string of keyword parameters separated by a space. i.e.

"mon_start=7 mon_stop=20"

because of restrictions in the characters the tags on RDS instance support.

To support multiple stop/start times per day, separate the hours with a /. Example shows an instance that starts at 7, turns off at 19, then turns back on again from 22 to 1 the next day for weekly maintenance.

"mon_start=7/22 mon_stop=19 tue_start=7 tue_stop=1/19"

Default for default is:

{"mon": {"start": [7], "stop": [19]},"tue": {"start": [7], "stop": [19]},"wed": {"start": [9, 22], "stop": [19]},"thu": {"start": [7], "stop": [2,19]}, "fri": {"start": [7], "stop": [19]}, "sat": {"start": [22]}, "sun": {"stop": [7]}}

time

Timezone to use for scheduler. Can be 'local', 'gmt' or an Olson timezone from https://gist.github.com/ykessler/3349954. default is 'gmt'. local time is for the AWS region.

permissions_boundary

An optional AWS IAM permissions boundary ARN to be attached to the AWS IAM role. default = "".

ec2_schedule

Whether to do scheduling for EC2 instances. default = "true".

rds_schedule

Whether to do scheduling for RDS instances. default = "true".

security_group_ids

list of the vpc security groups to run lambda scheduler in. Defaults to []. Usually this does not need to be specified.

subnet_ids

list of subnet_ids that the scheduler runs in. Defaults to []. Usually this does not need to be specified.

resource_name_prefix

The prefix to apply to resource names. E.g. setting this to cluster1- will create the Lambda as cluster1-aws-scheduler rather than aws-schedule. default = "".

terraform-aws-lambda-scheduler's People

Contributors

furikake avatar mrinella-fs avatar neillturner avatar rendanic avatar troydieter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

terraform-aws-lambda-scheduler's Issues

Error calling scheduler

[INFO] 2018-11-26T15:27:09.491Z xxxx Evaluating EC2 instance "xxxxxxxxxxxxxxxxxxxx"
'NoneType' object is not iterable: TypeError
Traceback (most recent call last):
File "/var/task/aws-scheduler.py", line 261, in handler
check()
File "/var/task/aws-scheduler.py", line 94, in check
for tag in instance.tags:
TypeError: 'NoneType' object is not iterable

RDS Scheduling not working

Greetings!

Today, I found this nice module and integrated it into my project.

It works perfectly fine for EC2 instances, but not for RDS.

I see one of the commits named "RDS scheduling is broken".

Is it still broken or is that commit actually fixing the issue?

I'll be grateful if you reply shortly as this is urgent for me.

Thanks in advance.

Best Regards.

hello,a question about main.tf

permissions_boundary = var.permissions_boundary != "" ? var.permissions_boundary : ""

Why can't I just value it directly here

Scheduler not working for RDS instances

SUMMARY

Hello team,

I've applied the module using Terraform v0.12.24 and set the values for the EC2 and RDS tags as per instructions.
Scheduler is working fine for the EC2 instances, starting/stopping them at defined intervals. However, it is never triggered for the RDS instances. Could the issue lies in format of the value that we are passing to the RDS tag?

As per instructions the value of the RDS tag should be a string with double quotes at the beggining and the end.

Example of the value: "mon_start=7 mon_stop=20 tue_start=7 tue_stop=20 wed_start=7 wed_stop=20 thu_start=7 thu_stop=20 fri_start=7 fri_stop=20"

RDS tags are not supporting symbols like ", so we can only pass a value without quotes, in this case: mon_start=7 mon_stop=20 tue_start=7 tue_stop=20 wed_start=7 wed_stop=20 thu_start=7 thu_stop=20 fri_start=7 fri_stop=20

This unfotunatelly never triggers scheduler and RDS instances are never stopped/started.
Could you please share if you are facing the same issue?
If the module is working fine with your RDS instances, please share the example of the value that you are passing to RDS tag.

Thank you in advance!

ISSUE TYPE

  • Bug Report

EXPECTED RESULTS

Ec2 and RDS instances to start/stop by scheduler at defined intervals
ACTUAL RESULTS

Scheduler is working only for the EC2 instances

timezone

How is timezone calculated? I'm in CST timezone and deployed in us-east-1, I have the stop set to 18 and time set to local. but the instance was stopped at 12:05 PM CST/18:05 GMT

RDS schedule is broken

I noticed RDS instances were not shutting down or starting. Traced it to line 240&242 of aws-scheduler.py in the zip file. hh is an int and the dictionary contains a string hour. I fixed it by removing the int cast on line 195 & 199

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.