AWS CloudWatch
Cloudwatch Insights X Ray and Service Map
Demo Lambda Insights
In this guide, you’ll learn how to provision multiple AWS Lambda functions with CloudFormation, invoke them to generate metrics, and explore those metrics in CloudWatch using Lambda Insights.
1. CloudFormation Template
Note
Ensure that an IAM role named HelloWorldFunctionRole
exists with permissions for Lambda execution and CloudWatch logging. You can define it in this template or create it beforehand.
Below is a CloudFormation YAML template that defines three Lambda functions:
Resources:
HelloWorldFunction01:
Type: AWS::Lambda::Function
Properties:
FunctionName: HelloWorldFunction01
Runtime: python3.9
Handler: index.handler
Role: !GetAtt HelloWorldFunctionRole.Arn
Timeout: 15
Code:
ZipFile: |
import json
def handler(event, context):
print("HelloWorld from function 01")
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
HelloWorldFunction02:
Type: AWS::Lambda::Function
Properties:
FunctionName: HelloWorldFunction02
Runtime: python3.9
Handler: index.handler
Role: !GetAtt HelloWorldFunctionRole.Arn
Timeout: 15
Code:
ZipFile: |
import json
def handler(event, context):
print("HelloWorld from function 02")
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
HelloWorldFunction03:
Type: AWS::Lambda::Function
Properties:
FunctionName: HelloWorldFunction03
Runtime: python3.9
Handler: index.handler
Role: !GetAtt HelloWorldFunctionRole.Arn
Timeout: 15
Code:
ZipFile: |
import json
import time
def handler(event, context):
print("HelloWorld from function 03")
time.sleep(5)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Function Summary
Function Name | Description | Timeout |
---|---|---|
HelloWorldFunction01 | Prints a greeting message | 15 sec |
HelloWorldFunction02 | Prints a greeting message | 15 sec |
HelloWorldFunction03 | Prints a greeting after a 5-sec delay | 15 sec |
2. Invocation Script
Generate consistent metrics by invoking each function 50 times. Save the following as lambda_call.sh
and make it executable.
#!/usr/bin/env bash
for i in {1..50}; do
aws lambda invoke --function-name HelloWorldFunction01 --payload '{}' output01.txt
aws lambda.invoke --function-name HelloWorldFunction02 --payload '{}' output02.txt
aws lambda.invoke --function-name HelloWorldFunction03 --payload '{}' output03.txt
done
chmod +x lambda_call.sh
3. Deploying the Stack
- Open the AWS Management Console and navigate to CloudFormation.
- Choose Create stack > With new resources (standard).
- Upload the YAML template and click Next.
- Enter a stack name (e.g.,
lambda-insights-demo
), acknowledge IAM capabilities, then click Next and Create stack.
Wait until the status reads CREATE_COMPLETE
.
4. Verify Lambda Functions
In the Lambda console, confirm that all three functions (HelloWorldFunction01
, HelloWorldFunction02
, HelloWorldFunction03
) are present and using Python 3.9.
5. Generate Load Using AWS CloudShell
Open AWS CloudShell from the console toolbar.
Upload or paste the
lambda_call.sh
script into your home directory.Execute:
./lambda_call.sh
You’ll see output similar to:
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
...
Notice that invocations for Function 03 take ~5 seconds due to the sleep.
6. Enabling Lambda Insights
By default, Lambda Insights is off. To enable it:
- In the Lambda console, select each function and go to Configuration > Monitoring and operations.
- Click Edit, enable Enhanced monitoring (Lambda Insights), and save.
Warning
Enabling Lambda Insights produces extra logs and metrics, which may incur additional charges.
Wait a few minutes for data to appear in CloudWatch.
7. Exploring Metrics in CloudWatch
Navigate to CloudWatch > Insights > Lambda Insights to view consolidated metrics across all functions.
To focus on a single function (e.g., HelloWorldFunction03
), select it from the list:
8. Creating a Custom Dashboard
If you manage many functions, a dedicated CloudWatch dashboard helps.
- In CloudWatch, go to Dashboards and click Create dashboard.
- Add Lambda Insights widgets and select your functions.
- Customize widgets for GB-s usage, duration, invocations, concurrent executions, and throttles.
Here’s an example with multiple functions:
Remember to disable Lambda Insights when not troubleshooting to avoid unnecessary costs.
9. Cleanup
- In CloudShell, choose Actions > Restart CloudShell to stop any running processes.
- In CloudFormation, select
lambda-insights-demo
and click Delete. - Confirm that the Lambda functions, logs, and IAM roles have been removed.
That completes this AWS Lambda Insights walkthrough. Happy monitoring!
References
Watch Video
Watch video content