Skip to content

Instantly share code, notes, and snippets.

@dsudduth
Last active November 9, 2020 22:24
Show Gist options
  • Select an option

  • Save dsudduth/36eb2c3de470feb14c8c9ecef94ef139 to your computer and use it in GitHub Desktop.

Select an option

Save dsudduth/36eb2c3de470feb14c8c9ecef94ef139 to your computer and use it in GitHub Desktop.
AWS CDK example for triggering a Lambda based on CloudTrail API event
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