visit
In a we discussed how to auto-subscribe a CloudWatch Log Group to a Lambda function using CloudWatch Events. So that we don’t need a manual process to ensure all Lambda logs would go to our log aggregation service.
Whilst this is useful in its own right, it only scratches the surface of what we can do. CloudTrail and CloudWatch Events makes it easy to automate many day-to-day operational steps. With the help of Lambda of course ;-)
I work with API Gateway and Lambda heavily. Whenever you create a new API, or make changes, there are several things you need to do:
Couple of things to note from the code above:
apigateway:PATCH
permission to enable detailed metricsapigateway:GET
permission to get the API name and REST endpointscloudwatch:PutMetricAlarm
permission to create the alarms
We can find the restApiId
and stageName
inside the detail.requestParameters
attribute. That’s all we need to figure out what endpoints are there, and so what alarms we need to create.
updateStage
call to API GatewaygetResources
call to API GatewaygetRestApi
call to API GatewayAWS/ApiGateway
namespace
Now, every time I create a new API, I will have CloudWatch Alarms to alert me when the 99 percentile latency for an endpoint goes over 1 second, for 5 minutes in a row.
All this, with just a few lines of code :-)You can take this further, and have other Lambda functions to:
And before you even have to ask, yes I’m aware of serverless plugin by the ACloudGuru folks. It looks neat, but it’s ultimately still something the developer has to remember to do.
That requires discipline. My experience tells me that you cannot rely on discipline, ever. Which is why, I prefer to have a platform in place that will generate these alarms instead.
Hi, my name is Yan Cui. I’m an and the author of . I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
You can contact me via , and . Check out my new course, . In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices. Get your copy .
Come learn about operational BEST PRACTICES for AWS Lambda: CI/CD, testing & debugging functions locally, logging, monitoring, distributed tracing, canary deployments, config management, authentication & authorization, VPC, security, error handling, and more.
You can also get 40% off the face price with the code ytcui.
Get your copy .