Giter VIP home page Giter VIP logo

terraform-aws-lambda-scheduler-stop-start's Introduction

terraform-aws-lambda-scheduler-stop-start

Build Status CI

Stop and start instance, rds resources and autoscaling groups with lambda function.

Terraform versions

For Terraform 0.12 use version v2.* of this module.

If you are using Terraform 0.11 you can use versions v1.*.

Future version v3.*

  • Multi aws tags support (backward compatibility)
  • Use bool type for scheduler service variable (backward compatibility)
  • Terminate action for autoscaling group instance

Features

  • Aws lambda runtine Python 3.7
  • ec2 instances scheduling
  • spot instances scheduling
  • rds clusters scheduling
  • rds instances scheduling
  • autoscalings scheduling
  • cloudwatch alarm scheduling
  • Aws CloudWatch logs for lambda

Caveats

You can't stop and start an Amazon Spot instance (only the Spot service can stop and start a Spot Instance), but you can reboot or terminate a Spot Instance. That why this module support only scheduler action terminate for spot instance.

Usage

module "stop_ec2_instance" {
  source                         = "diodonfrost/lambda-scheduler-stop-start/aws"
  name                           = "ec2_stop"
  cloudwatch_schedule_expression = "cron(0 0 ? * FRI *)"
  schedule_action                = "stop"
  autoscaling_schedule           = "false"
  spot_schedule                  = "terminate"
  ec2_schedule                   = "true"
  rds_schedule                   = "false"
  resources_tag                  = {
    key   = "tostop"
    value = "true"
  }
}

module "start_ec2_instance" {
  source                         = "diodonfrost/lambda-scheduler-stop-start/aws"
  name                           = "ec2_start"
  cloudwatch_schedule_expression = "cron(0 8 ? * MON *)"
  schedule_action                = "start"
  autoscaling_schedule           = "false"
  spot_schedule                  = "false"
  ec2_schedule                   = "true"
  rds_schedule                   = "false"
  resources_tag                  = {
    key   = "tostop"
    value = "true"
  }
}

Examples

  • Autoscaling scheduler - Create lambda functions to suspend autoscaling group with tag tostop = true and terminate its ec2 instances on Friday at 23:00 Gmt and start them on Monday at 07:00 GMT
  • Spot scheduler - Create lambda functions to stop spot instance with tag tostop = true on Friday at 23:00 Gmt
  • EC2 scheduler - Create lambda functions to stop ec2 with tag tostop = true on Friday at 23:00 Gmt and start them on Monday at 07:00 GMT
  • Rds aurora - mariadb scheduler - Create lambda functions to stop rds mariadb and aurora cluster with tag tostop = true on Friday at 23:00 Gmt and start them on Monday at 07:00 GMT
  • test fixture - Deploy environment for testing module

Inputs

Name Description Type Default Required
name Define name to use for lambda function, cloudwatch event and iam role string n/a yes
custom_iam_role_arn Custom IAM role arn for the scheduling lambda string null no
kms_key_arn The ARN for the KMS encryption key. If this configuration is not provided when environment variables are in use, AWS Lambda uses a default service key string null no
aws_regions A list of one or more aws regions where the lambda will be apply, default use the current region list null no
cloudwatch_schedule_expression The scheduling expression string "cron(0 22 ? * MON-FRI *)" yes
schedule_action Define schedule action to apply on resources string "stop" yes
resources_tag Set the tag use for identify resources to stop or start map { tostop = "true" } yes
autoscaling_schedule Enable scheduling on autoscaling resources string "false" no
spot_schedule Enable scheduling on spot instance resources string "false" no
ec2_schedule Enable scheduling on ec2 instance resources string "false" no
rds_schedule Enable scheduling on rds resources string "false" no
cloudwatch_alarm_schedule Enable scheduleding on cloudwatch alarm resources string "false" no

Outputs

Name Description
lambda_iam_role_arn The ARN of the IAM role used by Lambda function
lambda_iam_role_name The name of the IAM role used by Lambda function
scheduler_lambda_arn The ARN of the Lambda function
scheduler_lambda_name The name of the Lambda function
scheduler_lambda_invoke_arn The ARN to be used for invoking Lambda function from API Gateway
scheduler_lambda_function_last_modified The date Lambda function was last modified
scheduler_lambda_function_version Latest published version of your Lambda function
scheduler_log_group_name The name of the scheduler log group
scheduler_log_group_arn The Amazon Resource Name (ARN) specifying the log group

Tests

Some of these tests create real resources in an AWS account. That means they cost money to run, especially if you don't clean up after yourself. Please be considerate of the resources you create and take extra care to clean everything up when you're done!

In order to run tests that access your AWS account, you will need to configure your AWS CLI credentials. For example, you could set the credentials as the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

Integration tests

Integration tests are realized with python boto3 and pytest modules.

Install Python dependency:

python3 -m pip install boto3 pytest pytest-cov pytest-xdist
# Test python code use by instance scheduler scheduler
python3 -m pytest -n 4 --cov=package tests/integration/test_instance_scheduler.py

# Test python code use by spot scheduler scheduler
python3 -m pytest -n 2 --cov=package tests/integration/test_spot_scheduler.py

# Test python code use by autoscaling scheduler
python3 -m pytest -n 4 --cov=package tests/integration/test_asg_scheduler.py

# Test python code use by rds scheduler
python3 -m pytest -n 8 --cov=package tests/integration/test_rds_scheduler.py

# Test pythn code use by cloudwatch alarm scheduler
python3 -m pytest -n 12 --cov=package tests/integration/test_cloudwatch_alarm_scheduler.py

# Test all python code
python3 -m pytest -n 30 --cov=package tests/integration/

End-to-end tests

This module has been packaged with Terratest to tests this Terraform module.

Install Terratest with depedencies:

# Prerequisite: install Go
go get ./...
# Test instance scheduler
go test -timeout 900s -v tests/end-to-end/instance_scheduler_test.go

# Test spot scheduler
go test -timeout 900s -v tests/end-to-end/spot_scheduler_test.go

# Test autoscaling scheduler
go test -timeout 900s -v tests/end-to-end/autoscaling_scheduler_test.go

Authors

Modules managed by diodonfrost

Licence

Apache 2 Licensed. See LICENSE for full details.

Resources

terraform-aws-lambda-scheduler-stop-start's People

Contributors

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