0
0
AwsHow-ToBeginner · 4 min read

How to Trigger AWS Lambda from EventBridge: Simple Guide

To trigger a AWS Lambda function from EventBridge, create an EventBridge rule that matches events and set the Lambda function as the target. When the event occurs, EventBridge automatically invokes the Lambda function.
📐

Syntax

The main parts to trigger Lambda from EventBridge are:

  • EventBridge Rule: Defines which events to listen for.
  • Event Pattern: Filters events by source, detail type, or other attributes.
  • Target: The Lambda function to invoke when the event matches.

You create a rule with an event pattern and add the Lambda function as the target.

bash
aws events put-rule --name MyRule --event-pattern '{"source": ["aws.ec2"]}'

aws lambda add-permission --function-name MyLambdaFunction --statement-id MyRulePermission --action 'lambda:InvokeFunction' --principal events.amazonaws.com --source-arn arn:aws:events:region:account-id:rule/MyRule

aws events put-targets --rule MyRule --targets Id=1,Arn=arn:aws:lambda:region:account-id:function:MyLambdaFunction
💻

Example

This example creates an EventBridge rule that triggers a Lambda function when an EC2 instance state changes.

bash
aws events put-rule --name EC2StateChangeRule --event-pattern '{"source": ["aws.ec2"], "detail-type": ["EC2 Instance State-change Notification"]}'

aws lambda add-permission --function-name MyLambdaFunction --statement-id EC2StateChangePermission --action 'lambda:InvokeFunction' --principal events.amazonaws.com --source-arn arn:aws:events:us-east-1:123456789012:rule/EC2StateChangeRule

aws events put-targets --rule EC2StateChangeRule --targets Id=1,Arn=arn:aws:lambda:us-east-1:123456789012:function:MyLambdaFunction
Output
Rule created: EC2StateChangeRule Permission added: EC2StateChangePermission Target added to rule: EC2StateChangeRule
⚠️

Common Pitfalls

Common mistakes when triggering Lambda from EventBridge include:

  • Not adding permission for EventBridge to invoke the Lambda function.
  • Incorrect event pattern that does not match any events.
  • Using wrong ARN formats for the Lambda function or rule.
  • Not specifying the correct region or account ID.

Always verify the event pattern matches the events you expect and that permissions are set correctly.

bash
Wrong (missing permission):
aws events put-targets --rule MyRule --targets Id=1,Arn=arn:aws:lambda:region:account-id:function:MyLambdaFunction

Right (with permission):
aws lambda add-permission --function-name MyLambdaFunction --statement-id MyRulePermission --action 'lambda:InvokeFunction' --principal events.amazonaws.com --source-arn arn:aws:events:region:account-id:rule/MyRule
aws events put-targets --rule MyRule --targets Id=1,Arn=arn:aws:lambda:region:account-id:function:MyLambdaFunction
📊

Quick Reference

Tips for triggering Lambda from EventBridge:

  • Always create an EventBridge rule with a clear event pattern.
  • Grant EventBridge permission to invoke your Lambda function.
  • Use correct ARNs with region and account ID.
  • Test your event pattern with sample events in the AWS console.

Key Takeaways

Create an EventBridge rule with an event pattern to match events.
Add your Lambda function as the target of the EventBridge rule.
Grant EventBridge permission to invoke your Lambda function using lambda:add-permission.
Verify event patterns and ARNs are correct to avoid invocation failures.
Test your setup with sample events to ensure Lambda triggers as expected.