Last active
November 9, 2020 22:24
-
-
Save dsudduth/36eb2c3de470feb14c8c9ecef94ef139 to your computer and use it in GitHub Desktop.
AWS CDK example for triggering a Lambda based on CloudTrail API event
This file contains hidden or 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
| from aws_cdk import aws_cloudtrail as cloudtrail | |
| from aws_cdk import aws_events_targets as event_targets | |
| from aws_cdk import aws_lambda as lambda_ | |
| from aws_cdk import core | |
| class AlchemyStack(core.Stack): | |
| def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: | |
| super().__init__(scope, id, **kwargs) | |
| handler = lambda_.Function( | |
| self, | |
| 'AlchemyRepoManager', | |
| code=lambda_.Code.from_asset('functions/function_a'), | |
| runtime=lambda_.Runtime.PYTHON_3_8, | |
| handler='main.lambda_handler' | |
| ) | |
| event_rule = cloudtrail.Trail.on_event( | |
| self, | |
| 'RepoCreatedCWEvent', | |
| target=event_targets.LambdaFunction(handler) | |
| ) | |
| event_rule.add_event_pattern( | |
| source=['aws.codecommit'], | |
| detail={ | |
| 'eventSource': ['codecommit.amazonaws.com'], | |
| 'eventName': [ | |
| 'CreateRepository', | |
| 'DeleteRepository' | |
| ], | |
| } | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment