Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add s3 trigger for lambda #106

Merged
merged 3 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |

## JavaScript examples <a name="JavaScript"></a>
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)