-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCFN-CR-PythonLambdaLayer-SampleUsage.yaml
More file actions
145 lines (135 loc) · 4.36 KB
/
CFN-CR-PythonLambdaLayer-SampleUsage.yaml
File metadata and controls
145 lines (135 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---
AWSTemplateFormatVersion: '2010-09-09'
Description: >-
This stack will demo the Layer Builder CFN CR, with few example Layer
Also used for testing...
Metadata:
Author: KissT
Project: CFN-CR-PythonLambdaLayer
SourceCode: https://github.com/kisst/CFN-CR-PythonLambdaLayer
Resources:
EmptyLambdaLayer:
Properties:
ServiceToken:
Fn::ImportValue: !Sub "cfn:lambdalayer-x86-64:${AWS::Region}:arn"
Name: test-gracefull-exit
Type: Custom::LayerBuilder
Boto3LambdaLayer:
Properties:
ServiceToken:
Fn::ImportValue: !Sub "cfn:lambdalayer-x86-64:${AWS::Region}:arn"
Name: boto3-latest
requirements:
- boto3
Type: Custom::LayerBuilder
RequestsLambdaLayer:
Properties:
ServiceToken:
Fn::ImportValue: !Sub "cfn:lambdalayer-x86-64:${AWS::Region}:arn"
Name: requests-latest
requirements:
- requests
Type: Custom::LayerBuilder
# Pydantic has a compiled pydantic-core dependency; the builder's arch must
# match the consumer function's arch. x86_64 builder → x86_64 consumer.
PydanticLambdaLayer:
Properties:
ServiceToken:
Fn::ImportValue: !Sub "cfn:lambdalayer-x86-64:${AWS::Region}:arn"
Name: pydantic-latest
requirements:
- pydantic
Type: Custom::LayerBuilder
InlineCodeLambdaLayer:
Properties:
ServiceToken:
Fn::ImportValue: !Sub "cfn:lambdalayer-x86-64:${AWS::Region}:arn"
Name: inline-code-only
filename: eratosthenes.py
filecontent:
Fn::Base64: |
def sieve(n):
# https://wikipedia.org/wiki/Sieve_of_Eratosthenes
return sorted(
set(range(2,n+1)).difference(
set((p * f) \
for p in range(2,int(n**0.5) + 2) \
for f in range(2,(n//p)+1))
)
)
Type: Custom::LayerBuilder
InlineCodeWithPipPackageLambdaLayer:
Properties:
ServiceToken:
Fn::ImportValue: !Sub "cfn:lambdalayer-x86-64:${AWS::Region}:arn"
Name: inline-code-and-requirements
requirements:
- python-whois
filename: module.py
filecontent:
Fn::Base64: |
from whois import whois
def examine(domain):
return whois(domain)
Type: Custom::LayerBuilder
# Same requirements, different arch: call the arm64 builder. C extensions
# get built natively for arm64 so Graviton consumer functions work.
Arm64BotoLambdaLayer:
Properties:
ServiceToken:
Fn::ImportValue: !Sub "cfn:lambdalayer-arm64:${AWS::Region}:arn"
Name: boto3-latest-arm64
requirements:
- boto3
Type: Custom::LayerBuilder
LambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: AllowLogs
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: 'arn:aws:logs:*:*:*'
DemoLambdaFuntion:
Type: 'AWS::Lambda::Function'
Properties:
Description: 'Show lib version from the layers'
Code:
ZipFile: |
import boto3
import pydantic
import requests
from lambdalayer.module import examine
from lambdalayer.eratosthenes import sieve
def lambda_handler(event, context):
print(f"boto3 version: {boto3.__version__}")
print(f"pydantic version: {pydantic.VERSION}")
print(f"requests version: {requests.__version__}")
print(f"Sieve of Eratosthenes from 10: {sieve(10)}")
print(f"WhoIs example.com: {examine('example.com')}")
Handler: index.lambda_handler
Runtime: python3.14
Timeout: 60
Role: !GetAtt LambdaExecutionRole.Arn
Layers:
- !GetAtt Boto3LambdaLayer.Arn
- !GetAtt RequestsLambdaLayer.Arn
- !GetAtt PydanticLambdaLayer.Arn
- !GetAtt InlineCodeWithPipPackageLambdaLayer.Arn
- !GetAtt InlineCodeLambdaLayer.Arn