1. Introduction
Do you need to run a background task every few minutes — like syncing data, sending reports, or hitting an external API? AWS Lambda and EventBridge (formerly CloudWatch Events) offer a simple, serverless way to schedule jobs without managing servers.
In this article, I’ll show you how to spin up a scheduled Lambda function using Terraform — all in under 5 minutes.
2. What We’re Building
We’ll use:
- AWS Lambda to execute your custom logic
- EventBridge to define a time-based schedule
- Terraform to provision everything as code
The result: A serverless scheduler that you can easily deploy, update, and manage using infrastructure as code.
3. Prerequisites
Before we start, make sure you have:
- Terraform installed
- AWS CLI configured (
aws configure) - An IAM user with permissions for Lambda, IAM, and EventBridge
4. Let’s start
Step 1: Write Terraform Code
Create a new folder and add a file named main.tf with the following content:
provider "aws" {
region = "us-east-1"
}
resource "aws_iam_role" "lambda_exec_role" {
name = "lambda_exec_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}]
})
}
resource "aws_iam_role_policy_attachment" "lambda_basic_exec" {
role = aws_iam_role.lambda_exec_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_lambda_function" "scheduler_lambda" {
function_name = "scheduler-lambda"
role = aws_iam_role.lambda_exec_role.arn
handler = "lambda_function.lambda_handler"
runtime = "python3.12"
filename = "lambda.zip"
source_code_hash = filebase64sha256("lambda.zip")
}
resource "aws_cloudwatch_event_rule" "every_5_minutes" {
name = "every-5-minutes"
schedule_expression = "rate(5 minutes)"
}
resource "aws_cloudwatch_event_target" "lambda_target" {
rule = aws_cloudwatch_event_rule.every_5_minutes.name
target_id = "lambda"
arn = aws_lambda_function.scheduler_lambda.arn
}
resource "aws_lambda_permission" "allow_eventbridge" {
statement_id = "AllowExecutionFromEventBridge"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.scheduler_lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.every_5_minutes.arn
}
Step 2: Create Your Lambda Function
In the same folder, create a Python file named lambda_function.py:
def lambda_handler(event, context):
print("Scheduled Lambda triggered!")
Then zip it:
zip lambda.zip lambda_function.py
Step 3: Deploy with Terraform
Run the following commands to provision everything:
terraform init
terraform apply -auto-approve
Terraform will:
- Create an IAM role
- Deploy your Lambda
- Set up an EventBridge rule to trigger it every 5 minutes
- Give EventBridge permission to invoke your Lambda
5. Conclusion
In just a few minutes, you now have a scheduled Lambda running every 5 minutes — and you didn’t have to touch the AWS Console at all:
- No EC2 instances to manage
- No cron job servers to maintain
- Scalable and cost-effective (you only pay when Lambda runs)
- Infrastructure-as-Code makes it version-controlled and replicable
You can verify it by checking CloudWatch Logs in the AWS Console to see the function being invoked. The source code can be found here.