Description
What were you initially searching for in the docs?
Using the AWS CDK, I wanted to deploy a Lambda layer from the Powertools Serverless Application Repository ARN for re-use within my CDK application.
The Lambda Layer documentation does not include how to do this.
Is this related to an existing part of the documentation? Please share a link
https://awslabs.github.io/aws-lambda-powertools-python/#lambda-layer
Describe how we could make it clearer
A code snippet and details could be provided to make it easier for a customer to get up and running faster.
If you have a proposed update, please share it here
Using similar language to the existing documentation, but some rewording could also used to provide the overall detail with SAM and CDK examples below it.
If using the AWS CDK, you can create include this SAR App and lock to a specific semantic version. The Layer can be used in the same CDK application. Once deployed, it'll be available across the account this is deployed to.
** insert a TypeScript CDK example **
# Create the AWS Lambda Powertools for Python layer
powertools_arn = 'arn:aws:serverlessrepo:eu-west-1:057560766410:applications/aws-lambda-powertools-python-layer'
powertools_application = aws_sam.CfnApplication(self, 'AWSLambdaPowertoolsApplication',
location={'applicationId': powertools_arn,
'semanticVersion': '1.12.0'})
powertools_resource = cdk.CfnResource(powertools_application, 'AWSLambdaPowertoolsResource',
type='AWS::Serverless::Application',
properties={'Location': {
'ApplicationId': powertools_arn,
'SemanticVersion': '1.12.0'
}})
powertools_layer = aws_lambda.LayerVersion.from_layer_version_arn(
self, 'AWSLambdaPowertoolsLayer', powertools_resource.get_att("Outputs.LayerVersionArn").to_string())
# Reference the Layer in a Lambda definition
my_function = aws_lambda.Function(
self, "MyFunction",
layers=[powertools_layer],
runtime=aws_lambda.Runtime.PYTHON_3_8,
)
Metadata
Metadata
Assignees
Type
Projects
Status
Activity
michaelbrewer commentedon Mar 23, 2021
Possibly a Python CDK example :P
austoonz commentedon Mar 23, 2021
The example I included was Python CDK. 😁
ran-isenberg commentedon Mar 25, 2021
+1 for this, was on my todo list for a long time ;)
ran-isenberg commentedon Mar 25, 2021
is there a way to always get the latest published version?
am29d commentedon Mar 25, 2021
Hi Andrew,
Thanks for opening the issue and providing the sample code. I will find a good spot in the docs to add it.
am29d commentedon Mar 25, 2021
sadly no, you would need to run CLI or some custom resource to fetch it and sort by version
michaelbrewer commentedon Mar 30, 2021
For CDK, I like to load that kind of thing via configuration settings.
ran-isenberg commentedon Mar 31, 2021
I will need to write a function that takes the powertools version from my pipfile.lock file and set the layer version parameter. annoying, but will work.
michaelbrewer commentedon Mar 31, 2021
@risenberg-cyberark for docs i would just use the
cdk.json
or runtime context to configure the version of powertools.michaelbrewer commentedon Apr 3, 2021
@risenberg-cyberark @austoonz here is a full working example using Poetry:
Directory structure like below, with
cdk
folder for the AWS CDK code andhello-world
with the lambda code.Source for:
cdk/pyproject.toml
Source code for
cdk/Makefile
:dev: pip install --upgrade pip poetry poetry install
Source code for the
cdk/cdk.json
:Source code for the main stack
cdk/lambda_stack/lambda_stack.py
:michaelbrewer commentedon Apr 3, 2021
@heitorlessa @am29d ☝🏼 what do you think?
ran-isenberg commentedon Apr 5, 2021
@michaelbrewer i got the same code in my project but we are using pipenv instead. I'm working on adding a code that will extract the powertools version from the pipenv.lock file and use that for the layer version is the cdk. that way when you upgrade your layer locally, it will also be used automatically for uploaded lambdas layer.
19 remaining items