From ce8c08bc914f9d60d4d377189216e74b402dce49 Mon Sep 17 00:00:00 2001 From: Pubudu Jayawardana Date: Tue, 17 Sep 2019 11:44:26 +0200 Subject: [PATCH] feat: example using S3 trigger for Lambda (#106) --- README.md | 1 + python/lambda-s3-trigger/app.py | 10 +++++++ python/lambda-s3-trigger/cdk.json | 3 +++ .../lambda/lambda-handler.py | 8 ++++++ python/lambda-s3-trigger/requirements.txt | 5 ++++ .../lambda-s3-trigger/s3trigger/__init__.py | 0 .../s3trigger/s3trigger_stack.py | 26 +++++++++++++++++++ 7 files changed, 53 insertions(+) create mode 100644 python/lambda-s3-trigger/app.py create mode 100644 python/lambda-s3-trigger/cdk.json create mode 100644 python/lambda-s3-trigger/lambda/lambda-handler.py create mode 100644 python/lambda-s3-trigger/requirements.txt create mode 100644 python/lambda-s3-trigger/s3trigger/__init__.py create mode 100644 python/lambda-s3-trigger/s3trigger/s3trigger_stack.py diff --git a/README.md b/README.md index 28cd45055..bd6594478 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ $ cdk destroy | [fargate-load-balanced-service](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/ecs/fargate-load-balanced-service/) | Starting a container fronted by a load balancer on Fargate | | [fargate-service-with-autoscaling](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/ecs/fargate-service-with-autoscaling/) | Starting an ECS service of FARGATE launch type that auto scales based on average CPU Utilization | | [lambda-cron](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/lambda-cron/) | Running a Lambda on a schedule | +| [lambda-s3-trigger](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/lambda-s3-trigger/) | S3 trigger for Lambda | | [stepfunctions](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/stepfunctions/) | A simple StepFunctions workflow | | [url-shortner](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/url-shortener) | Demo from the [Infrastructure ***is*** Code with the AWS CDK](https://youtu.be/ZWCvNFUN-sU) AWS Online Tech Talk | diff --git a/python/lambda-s3-trigger/app.py b/python/lambda-s3-trigger/app.py new file mode 100644 index 000000000..c28c8bf87 --- /dev/null +++ b/python/lambda-s3-trigger/app.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 + +from aws_cdk import core + +from s3trigger.s3trigger_stack import S3TriggerStack + +app = core.App() +S3TriggerStack(app, "s3trigger") + +app.synth() diff --git a/python/lambda-s3-trigger/cdk.json b/python/lambda-s3-trigger/cdk.json new file mode 100644 index 000000000..b4baa1022 --- /dev/null +++ b/python/lambda-s3-trigger/cdk.json @@ -0,0 +1,3 @@ +{ + "app": "python3 app.py" +} diff --git a/python/lambda-s3-trigger/lambda/lambda-handler.py b/python/lambda-s3-trigger/lambda/lambda-handler.py new file mode 100644 index 000000000..767cef268 --- /dev/null +++ b/python/lambda-s3-trigger/lambda/lambda-handler.py @@ -0,0 +1,8 @@ +def main(event, context): + # save event to logs + print(event) + + return { + 'statusCode': 200, + 'body': event + } diff --git a/python/lambda-s3-trigger/requirements.txt b/python/lambda-s3-trigger/requirements.txt new file mode 100644 index 000000000..ef60395d9 --- /dev/null +++ b/python/lambda-s3-trigger/requirements.txt @@ -0,0 +1,5 @@ +aws-cdk.core + +aws-cdk.aws-s3 +aws-cdk.aws-lambda +aws-cdk.aws_s3_notifications diff --git a/python/lambda-s3-trigger/s3trigger/__init__.py b/python/lambda-s3-trigger/s3trigger/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/python/lambda-s3-trigger/s3trigger/s3trigger_stack.py b/python/lambda-s3-trigger/s3trigger/s3trigger_stack.py new file mode 100644 index 000000000..26208890e --- /dev/null +++ b/python/lambda-s3-trigger/s3trigger/s3trigger_stack.py @@ -0,0 +1,26 @@ +from aws_cdk import ( + aws_lambda as _lambda, + aws_s3 as _s3, + aws_s3_notifications, + core +) + + +class S3TriggerStack(core.Stack): + + def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: + super().__init__(scope, id, **kwargs) + + # create lambda function + function = _lambda.Function(self, "lambda_function", + runtime=_lambda.Runtime.PYTHON_3_7, + handler="lambda-handler.main", + code=_lambda.Code.asset("./lambda")) + # create s3 bucket + s3 = _s3.Bucket(self, "s3bucket") + + # create s3 notification for lambda function + notification = aws_s3_notifications.LambdaDestination(function) + + # assign notification for the s3 event type (ex: OBJECT_CREATED) + s3.add_event_notification(_s3.EventType.OBJECT_CREATED, notification)