Learn how to automate serverless function deployments and configure scheduled event triggers using Terraform for efficient and scalable workflows.
This guide demonstrates how to schedule the execution of an AWS Lambda function using Terraform and AWS CloudWatch Events (Amazon EventBridge). You will learn how to define your Lambda function, create a CloudWatch Event Rule for scheduling, link the two together, and set up the necessary permissions for seamless execution.
To schedule the execution of an AWS Lambda function using Terraform, you'll need to utilize the power of AWS CloudWatch Events (now known as Amazon EventBridge). Here's a breakdown of the process:
Define your Lambda function: Start by defining your Lambda function using the aws_lambda_function resource in your Terraform configuration. This involves specifying the function's name, runtime environment, handler, and other relevant parameters.
Establish a CloudWatch Event Rule: Create a CloudWatch Event Rule using the aws_cloudwatch_event_rule resource. This rule will dictate the schedule on which your Lambda function should be triggered. You can define the schedule using cron expressions or rate expressions.
Link the Lambda function to the Event Rule: Connect your Lambda function to the CloudWatch Event Rule using the aws_cloudwatch_event_target resource. This resource acts as a bridge, ensuring that when the Event Rule is triggered according to the defined schedule, your Lambda function is invoked.
Grant necessary permissions: For your CloudWatch Event Rule to successfully trigger your Lambda function, you need to grant the necessary permissions. This is typically achieved by adding an IAM policy to your Lambda function's execution role, allowing it to be invoked by the CloudWatch Events service.
By following these steps, you can effectively schedule the execution of your Lambda function using Terraform and CloudWatch Events. This approach provides a robust and scalable solution for automating tasks within your AWS environment.
This Terraform code defines an AWS Lambda function and configures it to be triggered by a CloudWatch Event Rule every 5 minutes. It creates the necessary IAM role and policy for the Lambda function, defines a CloudWatch Event Rule with a schedule, and sets up a target to invoke the Lambda function when the rule is triggered. Finally, it grants the CloudWatch Events service permission to invoke the Lambda function.
# Define the Lambda function
resource "aws_lambda_function" "example" {
function_name = "example_function"
runtime = "nodejs16.x"
handler = "index.handler"
# Replace with your actual Lambda function code
filename = "lambda_function.zip"
source_code_hash = filebase64sha256("lambda_function.zip")
# Define the IAM role for the Lambda function
role = aws_iam_role.lambda_role.arn
}
# Create an IAM role for the Lambda function
resource "aws_iam_role" "lambda_role" {
name = "example_lambda_role"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
POLICY
}
# Attach the AWSLambdaBasicExecutionRole policy to the Lambda role
resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" {
role = aws_iam_role.lambda_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
# Create a CloudWatch Event Rule to trigger the Lambda function
resource "aws_cloudwatch_event_rule" "example" {
name = "example_rule"
schedule_expression = "rate(5 minutes)" # Run every 5 minutes
}
# Create a CloudWatch Event Target to invoke the Lambda function
resource "aws_cloudwatch_event_target" "example" {
rule = aws_cloudwatch_event_rule.example.name
target_id = "lambda_target"
arn = aws_lambda_function.example.arn
}
# Grant permission to the CloudWatch Event Rule to invoke the Lambda function
resource "aws_lambda_permission" "example" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.example.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.example.arn
}Explanation:
Lambda Function Definition:
aws_lambda_function resource defines your Lambda function, including its name, runtime, handler, and code location.role attribute specifies the IAM role that the Lambda function will assume.IAM Role and Policy:
aws_iam_role resource creates an IAM role for the Lambda function.assume_role_policy allows the Lambda service to assume this role.aws_iam_role_policy_attachment resource attaches the AWSLambdaBasicExecutionRole policy to the role, granting the function basic execution permissions.CloudWatch Event Rule:
aws_cloudwatch_event_rule resource defines a CloudWatch Event Rule named "example_rule."schedule_expression attribute sets the schedule for triggering the rule. In this case, it's set to run every 5 minutes using the rate expression.CloudWatch Event Target:
aws_cloudwatch_event_target resource connects the CloudWatch Event Rule to the Lambda function.rule attribute specifies the rule to trigger the target.target_id is a unique identifier for the target.arn attribute specifies the ARN of the Lambda function to invoke.Lambda Permission:
aws_lambda_permission resource grants the CloudWatch Events service permission to invoke the Lambda function.statement_id is a unique identifier for the permission statement.action specifies the allowed action, which is lambda:InvokeFunction.function_name identifies the Lambda function.principal specifies the service that is allowed to invoke the function, which is events.amazonaws.com for CloudWatch Events.source_arn specifies the ARN of the CloudWatch Event Rule that is allowed to invoke the function.Remember to replace the placeholder values with your actual Lambda function code, desired schedule, and other relevant details.
Scheduling:
Permissions:
Lambda Function Considerations:
Terraform Best Practices:
Alternatives and Extensions:
Troubleshooting:
This table summarizes the process of scheduling AWS Lambda function execution using Terraform and CloudWatch Events:
| Step | Description | Terraform Resource | Key Point |
|---|---|---|---|
| 1. Define Lambda Function | Define your Lambda function's configuration (name, runtime, handler, etc.). | aws_lambda_function |
This sets up the function itself. |
| 2. Create CloudWatch Event Rule | Define the schedule (using cron or rate expressions) for triggering your Lambda function. | aws_cloudwatch_event_rule |
This determines when the function will run. |
| 3. Connect Lambda to Event Rule | Link your Lambda function to the CloudWatch Event Rule. | aws_cloudwatch_event_target |
This ensures the function is invoked upon the schedule defined in the rule. |
| 4. Grant Permissions | Allow CloudWatch Events to invoke your Lambda function by adding an IAM policy to the function's execution role. | This is crucial for the entire setup to work. |
In essence: You define your function, set a schedule with CloudWatch Events, connect the two, and ensure proper permissions for a seamless automated workflow.
In conclusion, scheduling the execution of AWS Lambda functions using Terraform and CloudWatch Events (Amazon EventBridge) offers a robust and automated approach for managing tasks within your AWS environment. By defining your Lambda function, establishing a CloudWatch Event Rule with a specific schedule, linking the two, and ensuring appropriate IAM permissions, you can achieve seamless and scalable automation. This guide provided a comprehensive overview of the process, along with code examples and explanations, empowering you to implement scheduled Lambda function executions effectively. Remember to leverage Terraform best practices, consider Lambda function limitations, and utilize monitoring tools for a smooth and reliable automation experience.
How to schedule your Lambda with Terraform? - DEV Community | There are many cases where you might need to run your Lambda regularly without having it triggered by...
Adding trigger between lambda function and Cloudwatch rule ... | I am struggling to create a schedule to run a lambda function through terraform⦠With the code below resource "aws_lambda_function" "cleansing_coding_lambda" { ... } resource "aws_cloudwatch_event_rule" "cleansing_coding_rule" { name = "cleansing-coding-rule" schedule_expression = "cron(5 * * * ? *)" # every hour, 5 mns passed the hour } resource "aws_cloudwatch_event_target" "cleansing_coding_target" { rule = aws_cloudwatch_event_rule.cleansing_coding_rule.name arn ...
How To Setup AWS Lambda Scheduled Events with Terraform | Need to know how to trigger a Lambda on a CloudWatch event? We show you how to Terraform Lambda Scheduled Event's by adding a CloudWatch Event, Trigger and Lambda permission.
Invoke a Lambda function on a schedule - AWS Lambda | With EventBridge Scheduler, you can create schedules using cron and rate expressions for recurring patterns, or configure one-time invocations. You can set upĀ ...
Lambda Function Event Bridge Terraform | How to invoke an AWS Lambda Function at scheduled intervals with AWS EventBridge Rule using Terraform