Interview Prep/AWS/Route 53, CloudFront, API Gateway and Messaging

An EventBridge rule should trigger a Lambda on EC2 state changes, but the Lambda never runs.

AdvancedTroubleshootingRoute 53, CloudFront, API Gateway and Messaging
Answer

Likely causes → The event pattern does not match: a typo, wrong case, or values not wrapped in arrays. The rule is on the wrong event bus or Region. The Lambda resource policy does not allow events.amazonaws.com, which shows up as FailedInvocations. Or the rule is disabled.

Diagnose →

aws events describe-rule --name ec2-state --query '[State,EventBusName,EventPattern]'
aws events test-event-pattern --event-pattern file://pattern.json --event file://sample-event.json
aws cloudwatch get-metric-statistics --namespace AWS/Events --metric-name FailedInvocations \
  --dimensions Name=RuleName,Value=ec2-state --statistics Sum --period 300 \
  --start-time $(date -u -d '-1 day' +%FT%TZ) --end-time $(date -u +%FT%TZ)
aws lambda get-policy --function-name on-ec2-change

Fix →

  1. Correct the pattern, for example {"source":["aws.ec2"],"detail-type":["EC2 Instance State-change Notification"],"detail":{"state":["stopped"]}}
  2. Add the permission: aws lambda add-permission --function-name on-ec2-change --statement-id evb --action lambda:InvokeFunction --principal events.amazonaws.com --source-arn <rule-arn>
  3. Add a DLQ on the target

Prevent → Unit-test patterns with test-event-pattern in CI, and alarm on FailedInvocations.

aws All AWS questions Roadmap: AWS core services

More on Route 53, CloudFront, API Gateway and Messaging