Skip to content

Commit

Permalink
feat: example using S3 trigger for Lambda (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
pubudusj authored and rix0rrr committed Sep 17, 2019
1 parent 3017879 commit ce8c08b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
10 changes: 10 additions & 0 deletions python/lambda-s3-trigger/app.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions python/lambda-s3-trigger/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "python3 app.py"
}
8 changes: 8 additions & 0 deletions python/lambda-s3-trigger/lambda/lambda-handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def main(event, context):
# save event to logs
print(event)

return {
'statusCode': 200,
'body': event
}
5 changes: 5 additions & 0 deletions python/lambda-s3-trigger/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
aws-cdk.core

aws-cdk.aws-s3
aws-cdk.aws-lambda
aws-cdk.aws_s3_notifications
Empty file.
26 changes: 26 additions & 0 deletions python/lambda-s3-trigger/s3trigger/s3trigger_stack.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ce8c08b

Please sign in to comment.