Skip to content

Commit ce8c08b

Browse files
pubudusjrix0rrr
authored andcommitted
feat: example using S3 trigger for Lambda (#106)
1 parent 3017879 commit ce8c08b

File tree

7 files changed

+53
-0
lines changed

7 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ $ cdk destroy
9292
| [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 |
9393
| [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 |
9494
| [lambda-cron](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/lambda-cron/) | Running a Lambda on a schedule |
95+
| [lambda-s3-trigger](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/lambda-s3-trigger/) | S3 trigger for Lambda |
9596
| [stepfunctions](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/stepfunctions/) | A simple StepFunctions workflow |
9697
| [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 |
9798

python/lambda-s3-trigger/app.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python3
2+
3+
from aws_cdk import core
4+
5+
from s3trigger.s3trigger_stack import S3TriggerStack
6+
7+
app = core.App()
8+
S3TriggerStack(app, "s3trigger")
9+
10+
app.synth()

python/lambda-s3-trigger/cdk.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "python3 app.py"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def main(event, context):
2+
# save event to logs
3+
print(event)
4+
5+
return {
6+
'statusCode': 200,
7+
'body': event
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
aws-cdk.core
2+
3+
aws-cdk.aws-s3
4+
aws-cdk.aws-lambda
5+
aws-cdk.aws_s3_notifications

python/lambda-s3-trigger/s3trigger/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from aws_cdk import (
2+
aws_lambda as _lambda,
3+
aws_s3 as _s3,
4+
aws_s3_notifications,
5+
core
6+
)
7+
8+
9+
class S3TriggerStack(core.Stack):
10+
11+
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
12+
super().__init__(scope, id, **kwargs)
13+
14+
# create lambda function
15+
function = _lambda.Function(self, "lambda_function",
16+
runtime=_lambda.Runtime.PYTHON_3_7,
17+
handler="lambda-handler.main",
18+
code=_lambda.Code.asset("./lambda"))
19+
# create s3 bucket
20+
s3 = _s3.Bucket(self, "s3bucket")
21+
22+
# create s3 notification for lambda function
23+
notification = aws_s3_notifications.LambdaDestination(function)
24+
25+
# assign notification for the s3 event type (ex: OBJECT_CREATED)
26+
s3.add_event_notification(_s3.EventType.OBJECT_CREATED, notification)

0 commit comments

Comments
 (0)