I’m working on a project where I need to test AWS Lambda functions that are triggered by AWS EventBridge rules when certain AWS resources (like EC2, RDS, S3) are created or updated. However, I’m facing an issue where events are not getting triggered in LocalStack when I create or update these resources, even though I’ve configured the EventBridge rules correctly.
My Use Case
- I want to simulate the creation and changes of resources like EC2 instances, RDS clusters, and S3 buckets in LocalStack and trigger a Lambda function when those changes happen (e.g., when a new EC2 instance is created).
- The Lambda function should receive the event via an EventBridge rule that I’ve configured to listen for
Create*
,Update*
, orDelete*
events.
What I’ve Done:
- I created EventBridge rules in Terraform to listen for events from AWS services like EC2, RDS, and S3.
- I have a webhook Lambda function set up to receive the events and handle them.
- I manually tested the Lambda function by invoking it using the
awslocal lambda invoke
command, and it works fine when I pass a sample event.
Here’s the relevant part of my Terraform code for setting up the EventBridge rule:
resource "aws_cloudwatch_event_rule" "resource_change_rule" {
name = "resource-change-rule"
description = "Trigger Lambda on resource changes"
event_pattern = jsonencode({
"source": [
"aws.ec2",
"aws.rds",
"aws.s3"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"ec2.amazonaws.com",
"rds.amazonaws.com",
"s3.amazonaws.com"
],
"eventName": [
"Create*",
"Delete*",
"Update*"
]
}
})
}
resource "aws_lambda_function" "webhook_lambda" {
function_name = "my-webhook-lambda"
# Lambda function details here
}
resource "aws_cloudwatch_event_target" "lambda_target" {
rule = aws_cloudwatch_event_rule.resource_change_rule.name
target_id = "lambda-target"
arn = aws_lambda_function.webhook_lambda.arn
}
What’s Happening:
When I manually invoke the Lambda function using the following command, the webhook is called successfully:
awslocal lambda invoke \
--function-name my-webhook-lambda \
--payload test-event.json \
output.json
However, when I try to create or update EC2 instances, RDS clusters, or S3 buckets in LocalStack (via awslocal ec2 run-instances
or awslocal rds create-db-cluster
), no event is triggered and the Lambda function does not get called.
Challenges/Questions:
- Real-Time Event Generation:
- Does LocalStack support the real-time generation of AWS events when resources like EC2, RDS, and S3 are created or updated?
- If so, why are the events not getting triggered in my case?
- EventBridge Configuration:
- Is there something missing in my EventBridge rule configuration to ensure that events are captured and forwarded to the Lambda function?
- Should I be using a different event pattern or event source to capture these events in LocalStack?
- CloudTrail and LocalStack Limitations:
- I understand that CloudTrail support in LocalStack is limited in the free version. Does this prevent the triggering of events like
Create*
,Delete*
, orUpdate*
for resources? - Is there a workaround or method to simulate these types of events without CloudTrail?
I formatted this question with AI hope you can understand better
Any help or insights would be greatly appreciated!
Thanks