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

diodonfrost/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

Requirements

Name Version
terraform >= 1.0
archive 2.3.0
aws >= 5.94.1

Providers

Name Version
archive 2.3.0
aws >= 5.94.1

Modules

No modules.

Resources

Name Type
aws_cloudwatch_event_rule.this resource
aws_cloudwatch_event_target.this resource
aws_cloudwatch_log_group.this resource
aws_iam_role.this resource
aws_iam_role_policy.autoscaling_group_scheduler resource
aws_iam_role_policy.cloudwatch_alarm_scheduler resource
aws_iam_role_policy.ecs_scheduler resource
aws_iam_role_policy.instance_scheduler resource
aws_iam_role_policy.lambda_logging resource
aws_iam_role_policy.rds_scheduler resource
aws_iam_role_policy.redshift_scheduler resource
aws_iam_role_policy.resource_groups_tagging_api resource
aws_iam_role_policy.spot_instance_scheduler resource
aws_lambda_function.this resource
aws_lambda_permission.this resource
archive_file.this data source
aws_iam_policy_document.autoscaling_group_scheduler data source
aws_iam_policy_document.cloudwatch_alarm_scheduler data source
aws_iam_policy_document.ecs_scheduler data source
aws_iam_policy_document.instance_scheduler data source
aws_iam_policy_document.rds_scheduler data source
aws_iam_policy_document.redshift_scheduler data source
aws_iam_policy_document.resource_groups_tagging_api data source
aws_iam_policy_document.spot_instance_scheduler data source
aws_iam_policy_document.this data source
aws_region.current data source

Inputs

Name Description Type Default Required
autoscaling_schedule Enable scheduling on autoscaling resources any false no
autoscaling_terminate_instances Terminate instances when autoscaling group is scheduled to stop bool false no
aws_regions A list of one or more aws regions where the lambda will be apply, default use the current region list(string) null no
cloudwatch_alarm_schedule Enable scheduleding on cloudwatch alarm resources any false no
cloudwatch_schedule_expression Define the aws cloudwatch event rule schedule expression string "cron(0 22 ? * MON-FRI *)" no
custom_iam_role_arn Custom IAM role arn for the scheduling lambda string null no
documentdb_schedule Enable scheduling on documentdb resources bool false no
ec2_schedule Enable scheduling on ec2 resources any false no
ecs_schedule Enable scheduling on ecs services bool false 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
name Define name to use for lambda function, cloudwatch event and iam role string n/a yes
rds_schedule Enable scheduling on rds resources any false no
redshift_schedule Enable scheduling on redshift resources any false no
resources_tag DEPRECATED, use scheduler_tag variable instead map(string) null no
runtime The runtime environment for the Lambda function that you are uploading string "python3.13" no
schedule_action Define schedule action to apply on resources, accepted value are 'stop or 'start string "stop" no
scheduler_tag Set the tag to use for identify aws resources to stop or start map(string)
{
"key": "tostop",
"value": "true"
}
no
tags Custom tags on aws resources map(any) null 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_function_last_modified The date Lambda function was last modified
scheduler_lambda_function_version Latest published version of your Lambda function
scheduler_lambda_invoke_arn The ARN to be used for invoking Lambda function from API Gateway
scheduler_lambda_name The name of the Lambda function
scheduler_log_group_arn The Amazon Resource Name (ARN) specifying the log group
scheduler_log_group_name The name of the scheduler 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.

End-to-end tests

# Test basic terraform deployment
cd examples/instance-scheduler
terraform test -verbose

# Test rds scheduler
cd examples/rds-scheduler
terraform test -verbose

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