Skip to content

Terraform module which creates lambda scheduler for stop and start resources on AWS

License

Notifications You must be signed in to change notification settings

tmill15/terraform-aws-lambda-scheduler-stop-start

 
 

Repository files navigation

terraform-aws-lambda-scheduler-stop-start

CI

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

Terraform versions

For Terraform 0.15 use version v3.* of this module.

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

Features

  • Aws lambda runtine Python 3.7
  • ec2 instances scheduling
  • ecs service scheduling
  • rds clusters scheduling
  • rds instances scheduling
  • redshift clusters scheduling
  • documentdb clusters scheduling
  • autoscalings scheduling
  • cloudwatch alarm scheduling
  • Aws CloudWatch logs for lambda

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"
  documendb_schedule             = "false"
  ec2_schedule                   = "true"
  ecs_schedule                   = "false"
  rds_schedule                   = "false"
  redshift_schedule              = "false"
  cloudwatch_alarm_schedule      = "false"
  scheduler_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"
  documendb_schedule             = "false"
  ec2_schedule                   = "true"
  ecs_schedule                   = "false"
  rds_schedule                   = "false"
  redshift_schedule              = "false"
  cloudwatch_alarm_schedule      = "false"
  scheduler_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
  • Documentdb scheduler - Create lambda functions to stop documentdb cluster with tag tostop = true on Friday at 23:00 Gmt and start them on Monday at 07:00 GMT
  • Instance 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
tags Custom tags on aws resources map 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
autoscaling_schedule Enable scheduling on autoscaling resources bool "false" no
autoscaling_terminate_instances Terminate instances when autoscaling group is scheduled to stop bool "false" no
documendb_schedule Enable scheduling on documentdb resources bool "false" no
ec2_schedule Enable scheduling on ec2 instance resources bool "false" no
ecs_schedule Enable scheduling on ecs services resources bool "false" no
rds_schedule Enable scheduling on rds resources bool "false" no
redshift_schedule Enable scheduling on redshift resources bool "false" no
cloudwatch_alarm_schedule Enable scheduleding on cloudwatch alarm resources bool "false" no
schedule_action Define schedule action to apply on resources string "stop" yes
scheduler_tag Set the tag to use for identify aws resources to stop or start map {"key" = "tostop", "value" = "true"} yes

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 -r requirements-dev.txt
# 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 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
cd tests/end-to-end/ && go get ./...

# Test instance scheduler
go test -timeout 30m -v instance_scheduler_test.go aws_invoke_lambda.go

# Test autoscaling scheduler
go test -timeout 30m -v autoscaling_scheduler_test.go aws_invoke_lambda.go

Authors

Modules managed by diodonfrost

Licence

Apache 2 Licensed. See LICENSE for full details.

Resources

About

Terraform module which creates lambda scheduler for stop and start resources on AWS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 74.4%
  • HCL 13.8%
  • Go 10.1%
  • Dockerfile 1.1%
  • Shell 0.6%