Skip to content

Commit 96c5cbc

Browse files
wulfmannrix0rrr
authored andcommitted
feat: add Python example for Kinesis+Lambda (#77)
1 parent 167df65 commit 96c5cbc

File tree

7 files changed

+100
-0
lines changed

7 files changed

+100
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from aws_cdk import (
2+
aws_lambda as lambda_,
3+
aws_kinesis as kinesis,
4+
aws_lambda_event_sources as event_sources,
5+
core,
6+
)
7+
8+
9+
class LambdaWithKinesisTrigger(core.Stack):
10+
def __init__(self, app: core.App, id: str) -> None:
11+
super().__init__(app, id)
12+
13+
with open("lambda-handler.py", encoding="utf8") as fp:
14+
handler_code = fp.read()
15+
16+
# Creates reference to already existing kinesis stream
17+
kinesis_stream = kinesis.Stream.from_stream_arn(
18+
self, 'KinesisStream',
19+
core.Arn.format(
20+
core.ArnComponents(
21+
resource='stream',
22+
service='kinesis',
23+
resource_name='my-stream'
24+
),
25+
self
26+
)
27+
)
28+
29+
lambdaFn = lambda_.Function(
30+
self, 'Singleton',
31+
handler='index.main',
32+
code=lambda_.InlineCode(handler_code),
33+
runtime=lambda_.Runtime.PYTHON_3_7,
34+
timeout=core.Duration.seconds(300)
35+
)
36+
37+
# Update Lambda Permissions To Use Stream
38+
kinesis_stream.grant_read(lambdaFn)
39+
40+
# Create New Kinesis Event Source
41+
kinesis_event_source = event_sources.KinesisEventSource(
42+
stream=kinesis_stream,
43+
starting_position=lambda_.StartingPosition.LATEST,
44+
batch_size=1
45+
)
46+
47+
# Attach New Event Source To Lambda
48+
lambdaFn.add_event_source(kinesis_event_source)
49+
50+
51+
app = core.App()
52+
LambdaWithKinesisTrigger(app, "LambdaWithKinesisTrigger")
53+
app.synth()
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def main(event, context):
2+
print("I'm running!")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
aws-cdk.aws-kinesis
2+
aws-cdk.aws-lambda
3+
aws-cdk-aws-lambda-event-sources
4+
aws-cdk.core
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from aws_cdk import (
2+
aws_lambda as lambda_,
3+
aws_s3 as s3,
4+
core,
5+
)
6+
7+
# Creates reference to already existing s3 bucket and lambda code
8+
9+
class LambdaS3Code(core.Stack):
10+
def __init__(self, app: core.App, id: str) -> None:
11+
super().__init__(app, id)
12+
13+
lambda_code_bucket = s3.Bucket.from_bucket_attributes(
14+
self, 'LambdaCodeBucket',
15+
bucket_name='my-lambda-code-bucket'
16+
)
17+
18+
lambdaFn = lambda_.Function(
19+
self, 'Singleton',
20+
handler='index.main',
21+
code=lambda_.S3Code(
22+
bucket=lambda_code_bucket,
23+
key='my-lambda.py'
24+
),
25+
runtime=lambda_.Runtime.PYTHON_3_7,
26+
timeout=core.Duration.seconds(300)
27+
)
28+
29+
30+
app = core.App()
31+
LambdaS3Code(app, "LambdaS3CodeExample")
32+
app.synth()
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
aws-cdk.aws-lambda
2+
aws-cdk.aws-s3
3+
aws-cdk.core

0 commit comments

Comments
 (0)