I have a serverless application, which consists of numerous lambda functions. Each lambda function has a tag “STAGE”, which is set either to “production” or “staging”. What I need now is to retrieve the data on overall invocations for each production lambda function for the last calendar month (30 days or 4 weeks), with a period of 1 day. I manage to achieve that in the AWS Console (Cloudwatch > Explorer), like shown on the screenshot below:
But the thing is that I need to retrieve this data programmatically to pass it down an ETL pipeline of sorts, that will generate a report with a specific format. I’ve came up with a following script to retrieve this data:
import datetime
import boto3
client = boto3.client("cloudwatch", region_name="eu-west-1")
from_date = datetime.date.today()
month_ago = from_date - datetime.timedelta(days=30)
response = client.get_metric_data(
MetricDataQueries=[{
"Id": "test",
"MetricStat": {
"Metric": {
"Namespace": "AWS/Lambda",
"MetricName": "Invocations",
"Dimensions": [{
"Name": "STAGE",
"Value": "production"
}]
},
"Period": 86400,
"Stat": "Sum"
},
"ReturnData": True,
}],
StartTime=month_ago.strftime("%Y-%m-%dT00:00:00Z"),
EndTime=from_date.strftime("%Y-%m-%dT00:00:00Z")
)
But all it returns me with these params is an empty dataset: { 'MetricDataResults': [{'Id': 'test', 'Label': 'Invocations', 'Timestamps': [], 'Values': [], 'StatusCode': 'Complete'}] ...}
And trying to run with the following Expression "SELECT SUM(Invocations) FROM SCHEMA("AWS/Lambda") WHERE "STAGE" = 'production' GROUP BY FunctionName"
instead of a MetricStat dict raises a MaxQueryTimeRangeExceed
exception, since we’re allowed to SELECT
the data only on the last 3 hours.
How would I build the query correctly to retrieve the same data with the same filters as I do in the AWS Console in this case? Thanks in advance!
3