Skip to content

Instantly share code, notes, and snippets.

@aterreno
Last active March 24, 2020 23:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aterreno/940c686c8fc409f39a8ec60b16d35bff to your computer and use it in GitHub Desktop.
Save aterreno/940c686c8fc409f39a8ec60b16d35bff to your computer and use it in GitHub Desktop.
How to automatically create CloudWatch alerts with CloudTrail, Lambda, and Serverless
SELECT DISTINCT eventname
FROM cloudtrail_logs_chargedup_cloudtrail
resource "aws_cloudtrail" "example" {
# ... other configuration ...
event_selector {
read_write_type = "All"
include_management_events = true
data_resource {
type = "AWS::Lambda::Function"
values = ["arn:aws:lambda"]
}
}
}
module.exports.handler = async (event, _context, cb) => {
const {
detail: {
responseElements: { functionArn = 'missing' },
eventName = 'missing',
},
} = event;
const [, functionName = ''] = functionArn.match(/^.*function:(.*)$/) || [];
console.log({ eventName, functionName });
if (functionName.includes(stage)) {
await publishToSns(functionName, eventName, stage);
await createAlarmsForEndpoints(functionName);
}
cb(null, 'ok');
};
cloud-trail-listener:
handler: cloud-trail.handler
events:
- cloudwatchEvent:
event:
source:
- aws.lambda
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- lambda.amazonaws.com
eventName:
- UpdateFunctionCode20150331v2
- CreateFunction20150331
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment