This project demonstrates how to create a serverless application using AWS SAM. It includes an AWS Lambda function triggered by an EventBridge rule. The Lambda function logs incoming events to AWS CloudWatch Logs.
Before you begin, ensure you have the following tools installed:
-
AWS SAM CLI: Install AWS SAM CLI
-
AWS CLI: Install AWS CLI and configure it with your AWS credentials:
aws configure
-
Python: Ensure Python 3.13 or a compatible runtime is installed. Add it to your PATH if not already.
-
Docker (Optional): For container-based builds, install Docker Desktop.
- Event Source: Amazon EventBridge listens for events matching a specific pattern.
- Lambda Function: A Lambda function is triggered by EventBridge and logs the event details to CloudWatch Logs.
- CloudWatch Logs: Captures and stores the logs from the Lambda function.
git clone https://github.com/insighture/eventbridge-lambda.git
cd eventbridge-lambda
sam build
Deploy the application to your AWS account:
sam deploy --guided
During deployment, you will be prompted for:
- Stack Name: Enter a name for your stack (e.g.,
eventbridge-lambda-app
). - AWS Region: Choose your preferred AWS region.
- Other Configurations: Accept the defaults or customize as needed.
The deployment will create an EventBridge rule, Lambda function, and necessary permissions.
Send a test event to EventBridge using the AWS CLI:
aws events put-events --entries '[{
"Source": "custom.myapp",
"DetailType": "MyApp Event",
"Detail": "{\"status\": \"trigger\"}"
}]'
- Go to the CloudWatch Logs console in the AWS Management Console.
- Find the log group for your Lambda function (e.g.,
/aws/lambda/HelloWorldFunction
). - View the logs to see the received event details.
To avoid incurring charges, delete the stack:
sam delete
This will remove all resources created by the application.
The Lambda function logs the incoming event details:
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
logger.info("Event received from EventBridge!")
logger.info(f"Event details: {event}")
return {
"statusCode": 200,
"body": "Hello, EventBridge!"
}
The template.yaml
defines the Lambda function and EventBridge rule:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SAM Template for EventBridge Hello World
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
Runtime: python3.13
CodeUri: hello_world/
Policies:
- CloudWatchLogsFullAccess
Events:
EventBridgeRule:
Type: CloudWatchEvent
Properties:
Pattern:
source:
- "custom.myapp"
detail-type:
- "MyApp Event"
detail:
status:
- "trigger"
EventBridgeRule:
Type: AWS::Events::Rule
Properties:
Name: MyEventBridgeRule
EventPattern:
source:
- "custom.myapp"
detail-type:
- "MyApp Event"
detail:
status:
- "trigger"
Targets:
- Arn: !GetAtt HelloWorldFunction.Arn
Id: "HelloWorldFunctionTarget"
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt HelloWorldFunction.Arn
Principal: events.amazonaws.com
SourceArn: !GetAtt EventBridgeRule.Arn
Outputs:
HelloWorldFunction:
Description: Lambda Function ARN
Value: !GetAtt HelloWorldFunction.Arn
N/A