Skip to content

Instantly share code, notes, and snippets.

@shortjared
Created January 29, 2019 20:29
Show Gist options
  • Save shortjared/84bb114ba9574bde4f98f54c59fb0b89 to your computer and use it in GitHub Desktop.
Save shortjared/84bb114ba9574bde4f98f54c59fb0b89 to your computer and use it in GitHub Desktop.
Function Throttle Alarms Serverless.yml Example
'use strict';
function sleep(ms){
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports.hello = async (event, context) => {
await sleep(5000); // hit api endpoint twice in 5 seconds, get a 429!
return {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
};
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
service: function-throttle-alerting-example
custom:
alerts:
stages:
- dev
dashboards: false
definitions:
functionThrottles:
period: 60
topics:
ok: ${self:service}-${opt:stage}-alerts-ok
alarm:
topic: ${self:service}-${opt:stage}-alerts-alarm
notifications:
- protocol: email
endpoint: [email protected]
insufficientData: ${self:service}-${opt:stage}-alerts-insufficient-data
alarms:
- functionThrottles
provider:
name: aws
runtime: nodejs8.10
functions:
hello:
handler: handler.hello
reservedConcurrency: 1 # make it easy to throttle and get 429 / 500 on api gateway
events:
- http:
path: test
method: get
plugins:
- serverless-plugin-aws-alerts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment