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*, or Delete* 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:

  1. 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?
  1. 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?
  1. 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*, or Update* 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 :slightly_smiling_face:

Any help or insights would be greatly appreciated!

Thanks

Hi, I can provide some quick answers for your questions:

  1. LocalStack does support real-time event generation. We need to evaluate your setup further to understand why they are not being generated.
  2. CloudTrail is only supported in the pro version of LocalStack, which might be a limitation if you’re using the community version alone. Right now, there are no workarounds for this.

We’re deprecating the LocalStack Discuss forum and recommend joining our Slack community for the best support moving forward. For more details, please see our deprecation notice. Thank you for your understanding and continued support!