AWS Lambda
Configuring Lambda
Create a Canary Function using Blueprint
Learn how to deploy a Canary Function in AWS Lambda with a ready-made blueprint. In this guide, you’ll use Amazon EventBridge (CloudWatch Events) as a trigger, configure environment variables, set up SNS destinations for success and failure notifications, and monitor your function using logs and metrics.
Prerequisites
• An AWS account with permissions to create Lambda functions, EventBridge rules, and SNS topics
• Basic knowledge of Python and AWS services
Table of Contents
- Open the Lambda Console
- Select the Canary Blueprint
- Configure the Function
- Review the Sample Code
- Set Schedule & Environment Variables
- View Your New Function
- Add SNS Destinations for Success & Failure
- Test the Canary Function
- Monitor Metrics and Logs
1. Open the Lambda Console
- Sign in to the AWS Management Console.
- In the Services menu, search for Lambda and click Lambda.
- Choose Create function.
2. Select the Canary Blueprint
AWS provides several blueprints with boilerplate code for common tasks. In the Use a blueprint tab:
- Search for canary.
- Select Canary Function (runtime: Python 3.7).
This blueprint periodically checks a URL for a specified text string.
3. Configure the Function
- Click Configure.
- Enter a Function name, e.g.,
KodeKloudCanaryDemo
. - Under Permissions, select Create a new role with basic Lambda permissions.
EventBridge is already set as the trigger. Create a new rule:
- Rule name:
CanaryDemoRule
4. Review the Sample Code
Scroll to Function code to inspect the handler:
import os
from datetime import datetime
from urllib.request import Request, urlopen
SITE = os.environ['site'] # URL to check
EXPECTED = os.environ['expected'] # Expected text on the page
def validate(response):
"""
Return True if EXPECTED appears in the content.
Extend this for custom checks.
"""
return EXPECTED in response
def lambda_handler(event, context):
print(f"Checking {SITE} at {event['time']}")
try:
req = Request(SITE, headers={'User-Agent': 'AWS Lambda'})
content = urlopen(req).read().decode('utf-8')
if not validate(content):
raise Exception('Validation failed')
print('Check passed')
except Exception as e:
print('Check failed:', e)
raise
This uses two environment variables:
Variable | Description |
---|---|
site | URL of the site to check |
expected | Text string to search for |
5. Set Schedule & Environment Variables
In the trigger pane, set the Schedule expression to:
rate(1 minute)
Under Variables and secrets, add:
Key Value site
https://kodekloud.com
expected
a string not on the site (to test failure)
When ready, click Create function. AWS will provision the new function.
6. View Your New Function
After creation, the Lambda console displays your function’s details and trigger.
7. Add SNS Destinations for Success & Failure
By default, asynchronous invocations don’t return errors to the trigger. To capture outcomes:
- Click Add destination.
- Under Source type, select Asynchronous invocation.
- Set Condition to On failure.
- For Destination type, choose SNS topic and pick (or create) your topic, e.g.,
Canary Demo Failures
. - Save and allow AWS to create the necessary IAM role.
Repeat to add an On success destination to an SNS topic (e.g., Canary Demo Success
).
Once done, both destinations will appear alongside your EventBridge rule.
8. Test the Canary Function
- In the Lambda console, click Test.
- The first invocation should fail (expected=string not on site).
- To simulate success, go to Configuration > Variables and secrets and update
expected
to a word you know exists (e.g.,learned
). - Save and click Test again.
Now the function should pass the validation.
9. Monitor Metrics and Logs
To review invocation metrics:
- Click the Monitor tab in the Lambda console.
- For detailed logs, select View logs in CloudWatch.
Inspect each log stream to see both failures and successful runs.
Congratulations! You’ve successfully created a Canary Function using an AWS Lambda blueprint, scheduled it with EventBridge, configured SNS destinations, and monitored its behavior. For more AWS Lambda tips and tutorials, explore the AWS Lambda Documentation.
Watch Video
Watch video content
Practice Lab
Practice lab