In this lab, we will learn how to periodically invoke a lambda function via Amazon EventBridge.
- Overview
- Source: EventBridge
- Build a custom Rule – Scheduling
- Target: Lambda – Function
- returns the current date time
- Python
1. Lambda – Create a Function
- Function Name: “logDateTime“
- Runtime: Python 3.9
2. Lambda – Function Code (Python)
- Type the following code
- Click “Deploy“
import json
from datetime import datetime
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps(datetime.now().strftime("%m/%d/%Y %H:%M:%S"))
}
3. EventBridge – Create a Rule
- Click the “Rules” menu on the left pane
- Click “Create Rule”
- Name: “datetime-rule“
- Event bus: select “default“
- Rule Type: “Schedule“
- Click “Continue in EventBridge Scheduler“
- Specify schedule detail
- Check “Recurring schedule“
- Schedule Type: “Cron-based schedule“
- Cron expression: every minute
- Flexible time window: “Off“
- Select Target(s)
- Target: “Lambda Invoke“
- Lambda Function: “logDateTime“
- Input: Empty
- Settings
- Enable schedule: “Enable“
- Check “Create new role for this schedule“
5. CouldWatch – Check the Lambda logs
- On the Lambda function page, click the “Monitor” tab
- Check some metrics on the page
- Click the “View logs in the CloudWatch” button
- On the CloudWatch page, click the “Log stream“
- Check the logs