Skip to content

Commit

Permalink
feat: add Python example for Kinesis+Lambda (aws-samples#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
wulfmann authored and rix0rrr committed Sep 17, 2019
1 parent 49d8b87 commit 98d5bd1
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
53 changes: 53 additions & 0 deletions python/lambda-triggered-by-existing-kinesis-stream/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from aws_cdk import (
aws_lambda as lambda_,
aws_kinesis as kinesis,
aws_lambda_event_sources as event_sources,
core,
)


class LambdaWithKinesisTrigger(core.Stack):
def __init__(self, app: core.App, id: str) -> None:
super().__init__(app, id)

with open("lambda-handler.py", encoding="utf8") as fp:
handler_code = fp.read()

# Creates reference to already existing kinesis stream
kinesis_stream = kinesis.Stream.from_stream_arn(
self, 'KinesisStream',
core.Arn.format(
core.ArnComponents(
resource='stream',
service='kinesis',
resource_name='my-stream'
),
self
)
)

lambdaFn = lambda_.Function(
self, 'Singleton',
handler='index.main',
code=lambda_.InlineCode(handler_code),
runtime=lambda_.Runtime.PYTHON_3_7,
timeout=core.Duration.seconds(300)
)

# Update Lambda Permissions To Use Stream
kinesis_stream.grant_read(lambdaFn)

# Create New Kinesis Event Source
kinesis_event_source = event_sources.KinesisEventSource(
stream=kinesis_stream,
starting_position=lambda_.StartingPosition.LATEST,
batch_size=1
)

# Attach New Event Source To Lambda
lambdaFn.add_event_source(kinesis_event_source)


app = core.App()
LambdaWithKinesisTrigger(app, "LambdaWithKinesisTrigger")
app.synth()
3 changes: 3 additions & 0 deletions python/lambda-triggered-by-existing-kinesis-stream/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "python3 app.py"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def main(event, context):
print("I'm running!")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
aws-cdk.aws-kinesis
aws-cdk.aws-lambda
aws-cdk-aws-lambda-event-sources
aws-cdk.core
32 changes: 32 additions & 0 deletions python/lambda-with-existing-s3-code/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from aws_cdk import (
aws_lambda as lambda_,
aws_s3 as s3,
core,
)

# Creates reference to already existing s3 bucket and lambda code

class LambdaS3Code(core.Stack):
def __init__(self, app: core.App, id: str) -> None:
super().__init__(app, id)

lambda_code_bucket = s3.Bucket.from_bucket_attributes(
self, 'LambdaCodeBucket',
bucket_name='my-lambda-code-bucket'
)

lambdaFn = lambda_.Function(
self, 'Singleton',
handler='index.main',
code=lambda_.S3Code(
bucket=lambda_code_bucket,
key='my-lambda.py'
),
runtime=lambda_.Runtime.PYTHON_3_7,
timeout=core.Duration.seconds(300)
)


app = core.App()
LambdaS3Code(app, "LambdaS3CodeExample")
app.synth()
3 changes: 3 additions & 0 deletions python/lambda-with-existing-s3-code/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "python3 app.py"
}
3 changes: 3 additions & 0 deletions python/lambda-with-existing-s3-code/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
aws-cdk.aws-lambda
aws-cdk.aws-s3
aws-cdk.core

0 comments on commit 98d5bd1

Please sign in to comment.