When we talk about monitoring serverless applications , we do not need to worry about monitoring CPU usage or memory usage, and we don't need to update our system package, as all these are managed by AWS. But the Lambda function still needs to be monitored for execution failures, because in a production environment one single function failure could be a disaster.
CloudWatch, by default, provides metrics for Lambda functionsand these metrics are:
- Invocation: Number of times the functions were invoked
- Errors: Number of times the functions failed due to various errors, timeouts, unhandled exceptions, memory problems, and other issues
- Throttles: Number of times functions throttled, as AWS limits the number of concurrent executions per function and if we exceed the limit the function is throttled
- Duration: Invocation time of the function
And errors and throttles are to be monitored 24/7 and we cannot be watching CloudWatch all the time, but we can set an alerti for all the errors and throttles. If we are using the serverless framework, then this can be managed through a plugin named serverless-plugin-aws-alerts.It makes it easy to set up alerts for the services.
To set up alerting, we need to install the plugin within our serverless framework service:
$ npm install serverless-plugin-aws-alerts --save-dev
Then we need to add it to the plugins sections and set the alerts details in the custom section within serverless.yml. So, this setup adds alerts to all the functions in our service when deployed to the production stage. Then we configure subscriptions for our SNS topics and each subscription has to have a protocol, which, in our case, is the email and endpoint, which is email address:
# serverless.yml
plugins:
- serverless-plugin-aws-alerts
custom:
alerts:
stages:
- production
topics:
alarm:
topic: ${self:service}-${opt:stage}-alerts-alarm
notifications:
- protocol: email
endpoint: myemail@domain.com
alarms:
- functionErrors
- functionThrottles