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: Reorganize python lambda section and add new example #77

Merged
merged 6 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
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-lambdaa.py'
wulfmann marked this conversation as resolved.
Show resolved Hide resolved
),
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