Skip to content

uridium/terraform-aws-lambda-scheduler

Repository files navigation

Terraform AWS Lambda module: scheduler/cron

GitHub Actions GitHub Actions Latest tag

Terraform AWS module which creates Lambda resource with attached CloudWatch scheduler.

It integrates Lambda function with CloudWatch cron expression using EventBridge.

Simply put, thanks to this module, you can schedule your code to run at a specific time.

Usage

module "cron" {
  source = "git@github.com:uridium/terraform-aws-lambda-scheduler.git"

  function_name  = "plower"
  handler        = "lambda.handler"
  runtime        = "python3.9"
  layer_enabled  = true
  code_directory = "/opt/lambda"
  input          = "{\"firstName\": \"John\", \"lastName\": \"Doe\"}"

  vars = {
    foo = "bar"
  }

  tags = {
    Env = "Stage"
  }

  cron = "cron(01 06 ? * MON-FRI *)"
}

Development

This module uses pre-commit hook to identify simple issues before pushing code to the remote repository, and relies on .pre-commit-config.yaml file.

To use it simply run:

pip install pre-commit
pre-commit install

Now pre-commit will run automatically on every git commit.

Notes

  • code_directory specifies a directory where you keep your entire Lambda code
  • If you want to create a layer, simply put it into layer/<runtime> directory and set layer_enabled to true

Examples

Requirements

Name Version
terraform >= 1.2
archive >= 2.3.0
aws >= 4.0

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_attachment.lambda resource
aws_iam_role_policy_attachment.xray resource
aws_lambda_function.this resource
aws_lambda_layer_version.this resource
aws_lambda_permission.this resource
archive_file.function_zip data source
archive_file.layer_zip data source
aws_iam_policy_document.this data source

Inputs

Name Description Type Default Required
code_directory A relative or full path to the directory with your Lambda Function (and Layer if you use it) code. For example: /opt/lambda string null no
cron The scheduling expression. For example: cron(0 20 * * ? *) or rate(5 minutes) string n/a yes
description Description of what your Lambda Function does string "Generated by Terraform" no
function_name A unique name for your Lambda Function string n/a yes
handler The function entrypoint in your code string null no
image_uri ECR image URI containing the function's deployment package string false no
input Valid JSON text passed to the target string null no
layer_enabled Create a Lambda Layer Version resource bool false no
memory_size The amount of memory in MB your Lambda Function can use at runtime number 128 no
package_type Lambda deployment package type string "Zip" no
runtime The identifier of the function's runtime string null no
security_group_ids A list of security group IDs associated with the Lambda function list(string) [] no
subnet_ids A list of subnet IDs associated with the Lambda function list(string) [] no
tags A map of tags to assign to the object map(string) null no
timeout The amount of time your Lambda Function has to run in seconds number 3 no
tracing_mode Define X-Ray tracing mode to record timing and error information for a subset of invocations string "PassThrough" no
vars The Lambda environment's configuration settings map(string) null no

Outputs

Name Description
lambda_function_arn The ARN of the Lambda Function
lambda_function_filename The filename of the function zip archive
lambda_function_handler The filename.handler-method value in your function
lambda_function_image_uri A list of subnets and security groups associated tiwht the Lambda Function
lambda_function_last_modified The time when the Lambda Function was modified
lambda_function_name The name of the Lambda Function
lambda_function_role IAM role attached to the Lambda Function
lambda_function_runtime The identifier of the Lambda Function's runtime
lambda_function_source_code_hash A base64-encoded SHA256 hash of the package file
lambda_function_source_code_size The size in bytes of the function .zip file
lambda_function_version Latest published version of your Lambda Function
lambda_function_vpc_config A list of subnets and security groups associated tiwht the Lambda Function