Skip to content

Commit b84aac5

Browse files
authored
docs: add Layers example for Serverless framework & CDK (#500)
1 parent 7c88d11 commit b84aac5

File tree

1 file changed

+75
-17
lines changed

1 file changed

+75
-17
lines changed

Diff for: docs/index.md

+75-17
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,81 @@ Powertools is also available as a Lambda Layer, and it is distributed via the [A
4747

4848
If using SAM, you can include this SAR App as part of your shared Layers stack, and lock to a specific semantic version. Once deployed, it'll be available across the account this is deployed to.
4949

50-
=== "template.yml"
51-
52-
```yaml hl_lines="5-6 12-13"
53-
AwsLambdaPowertoolsPythonLayer:
54-
Type: AWS::Serverless::Application
55-
Properties:
56-
Location:
57-
ApplicationId: arn:aws:serverlessrepo:eu-west-1:057560766410:applications/aws-lambda-powertools-python-layer
58-
SemanticVersion: 1.10.2 # change to latest semantic version available in SAR
59-
60-
MyLambdaFunction:
61-
Type: AWS::Serverless::Function
62-
Properties:
63-
Layers:
64-
# fetch Layer ARN from SAR App stack output
65-
- !GetAtt AwsLambdaPowertoolsPythonLayer.Outputs.LayerVersionArn
66-
```
50+
=== "SAM"
51+
52+
```yaml hl_lines="5-6 12-13"
53+
AwsLambdaPowertoolsPythonLayer:
54+
Type: AWS::Serverless::Application
55+
Properties:
56+
Location:
57+
ApplicationId: arn:aws:serverlessrepo:eu-west-1:057560766410:applications/aws-lambda-powertools-python-layer
58+
SemanticVersion: 1.17.0 # change to latest semantic version available in SAR
59+
60+
MyLambdaFunction:
61+
Type: AWS::Serverless::Function
62+
Properties:
63+
Layers:
64+
# fetch Layer ARN from SAR App stack output
65+
- !GetAtt AwsLambdaPowertoolsPythonLayer.Outputs.LayerVersionArn
66+
```
67+
68+
=== "Serverless framework"
69+
70+
```yaml hl_lines="5 8 10-11"
71+
functions:
72+
main:
73+
handler: lambda_function.lambda_handler
74+
layers:
75+
- !GetAtt AwsLambdaPowertoolsPythonLayer.Outputs.LayerVersionArn
76+
77+
resources:
78+
Transform: AWS::Serverless-2016-10-31
79+
Resources:
80+
AwsLambdaPowertoolsPythonLayer:
81+
Type: AWS::Serverless::Application
82+
Properties:
83+
Location:
84+
ApplicationId: arn:aws:serverlessrepo:eu-west-1:057560766410:applications/aws-lambda-powertools-python-layer
85+
# Find latest from github.com/awslabs/aws-lambda-powertools-python/releases
86+
SemanticVersion: 1.17.0
87+
```
88+
89+
=== "CDK"
90+
91+
```python hl_lines="14 22-23 31"
92+
from aws_cdk import core, aws_sam as sam, aws_lambda
93+
94+
POWERTOOLS_BASE_NAME = 'AWSLambdaPowertools'
95+
# Find latest from github.com/awslabs/aws-lambda-powertools-python/releases
96+
POWERTOOLS_VER = '1.17.0'
97+
POWERTOOLS_ARN = 'arn:aws:serverlessrepo:eu-west-1:057560766410:applications/aws-lambda-powertools-python-layer'
98+
99+
class SampleApp(core.Construct):
100+
101+
def __init__(self, scope: core.Construct, id_: str) -> None:
102+
super().__init__(scope, id_)
103+
104+
# Launches SAR App as CloudFormation nested stack and return Lambda Layer
105+
powertools_app = sam.CfnApplication(self,
106+
f'{POWERTOOLS_BASE_NAME}Application',
107+
location={
108+
'applicationId': POWERTOOLS_ARN,
109+
'semanticVersion': POWERTOOLS_VER
110+
},
111+
)
112+
113+
powertools_layer_arn = powertools_app.get_att("Outputs.LayerVersionArn").to_string()
114+
powertools_layer_version = aws_lambda.LayerVersion.from_layer_version_arn(self, f'{POWERTOOLS_BASE_NAME}', powertools_layer_arn)
115+
116+
aws_lambda.Function(self,
117+
'sample-app-lambda',
118+
runtime=aws_lambda.Runtime.PYTHON_3_8,
119+
function_name='sample-lambda',
120+
code=aws_lambda.Code.asset('./src'),
121+
handler='app.handler',
122+
layers: [powertools_layer_version]
123+
)
124+
```
67125

68126
??? tip "Example of least-privileged IAM permissions to deploy Layer"
69127

0 commit comments

Comments
 (0)