Last active
March 24, 2020 23:31
-
-
Save aterreno/940c686c8fc409f39a8ec60b16d35bff to your computer and use it in GitHub Desktop.
How to automatically create CloudWatch alerts with CloudTrail, Lambda, and Serverless
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT DISTINCT eventname | |
FROM cloudtrail_logs_chargedup_cloudtrail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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