Building a Visual Insight: Creating Lambda CloudWatch Dashboards with CloudFormation
OverView :-
The AWS::CloudWatch::Dashboard resource specifies an #AmazonCloudWatch dashboard. A dashboard is a customizable home page in the #CloudWatch console that you can use to monitor your AWS resources in a single view.
Add a custom widget to a #CloudWatch dashboard :-
A custom widget is a #CloudWatch dashboard widget that can call any #AWSLambda function with custom parameters. It then displays the returned #HTML or #JSON. Custom widgets are a simple way to build a custom data view on a dashboard. If you can write #Lambda code and create HTML, you can create a useful custom widget. Additionally, #Amazon provides several prebuilt custom widgets that you can create without any code.
When you create a #Lambda function to use as a custom widget, we strongly recommend that you include the prefix custom Widget in the function name. This helps you know which of your Lambda functions are safe to use when you add custom widgets to your dashboard.
- Below is the template for #CloudFormation stack.
AWSTemplateFormatVersion: '2010–09–09'
Transform: AWS::Serverless-2016–10–31
Resources:
MyLambda:
Type: AWS::Serverless::Function
…
MyStateMachine:
Type: "AWS::StepFunctions::StateMachine"
…
MyDashboard:
Type: AWS::CloudWatch::Dashboard
Properties:
DashboardName: MyDashboard
DashboardBody:
Fn::Sub: '{
"widgets": [
{
"type": "metric",
"x": 0,
"y": 3,
"width": 24,
"height": 3,
"properties": {
"view": "singleValue",
"metrics": [
[ "AWS/Lambda", "Invocations", "FunctionName", "${MyLambda}", { "stat": "Sum", "period": 86400 } ],
[ ".", "Duration", ".", ".", { "stat": "Average", "period": 86400, "color": "#2ca02c" } ],
[ ".", "Errors", ".", ".", { "stat": "Sum", "period": 86400, "color": "#d62728" } ],
[ ".", "Throttles", ".", ".", { "stat": "Sum", "period": 86400, "color": "#ff7f0e" } ]
],
"region": "us-west-2",
"title": "MyLambda",
"stacked": true
}
}
]
}'
- Create a yaml file in your home directory and copy the above template and login to your aws account and navigate to cloud formation & create a stack
- Click on choose file and upload your yaml file, After uploading the file give a name to your stack and submit the stack.
- Once the stack creation is completed, check whether the resources are #deployed or not in the stack info.
Also you can open the cloud watch dashboard and check whether the dashboard has been created or not.
conclusion :-
Through this journey, you’ve learned how to define dashboard widgets, metric queries, and layout structures within the #CloudFormation template. This declarative approach not only simplifies the #deployment but also ensures that your dashboard configurations are #versioned, reproducible, and can be seamlessly #integrated into your #CI/CD pipelines.