- Published on
Snippet: Create Custom CloudWatch Metrics with Python
- Authors
- Name
- Ruan Bekker
- @ruanbekker
A quick post on how create custom CloudWatch Metrics using Python on AWS.
After you produced the metrics into CloudWatch, you will be able to see them when navigating to:
CloudWatch
,Metrics
,Custom Namespaces
,statusdash/ec2client
When selecting:
Select Metric: SomeKey1, SomeKey2
Select MetricName HttpResponseTime
And should look like this:
The Script:
The python script that will be using boto3 to talk to AWS:
import boto3
import random
cloudwatch = boto3.Session(region_name='eu-west-1').client('cloudwatch')
response = cloudwatch.put_metric_data(
MetricData = [
{
'MetricName': 'HttpResponseTime',
'Dimensions': [
{
'Name': 'Server',
'Value': 'app.example.com'
},
{
'Name': 'Client',
'Value': 'Client-ABC'
},
],
'Unit': 'Milliseconds',
'Value': random.randint(20, 50)
},
],
Namespace = 'statusdash/ec2client'
)
print(response)
Resources:
- https://stackify.com/custom-metrics-aws-lambda/
- https://www.syntouch.nl/custom-cloudwatch-metrics-in-python-yes-we-can/ (psutil)
- https://aws.amazon.com/blogs/devops/new-how-to-better-monitor-your-custom-application-metrics-using-amazon-cloudwatch-agent/
- https://medium.com/@mrdoro/aws-lambda-as-the-website-monitoring-tool-184b09202ae2
Thank You
Thanks for reading, if you like my content, feel free to check out my website, and subscribe to my newsletter or follow me at @ruanbekker on Twitter.
- Linktree: https://go.ruan.dev/links
- Patreon: https://go.ruan.dev/patreon