forked from aws-samples/aws-cdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Python example for Kinesis+Lambda (aws-samples#77)
- Loading branch information
Showing
7 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"app": "python3 app.py" | ||
} |
2 changes: 2 additions & 0 deletions
2
python/lambda-triggered-by-existing-kinesis-stream/lambda-handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def main(event, context): | ||
print("I'm running!") |
4 changes: 4 additions & 0 deletions
4
python/lambda-triggered-by-existing-kinesis-stream/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"app": "python3 app.py" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |