Skip to content

Commit 68812d0

Browse files
committed
feat: Add SnapStart support
1 parent 5007437 commit 68812d0

12 files changed

+707
-7
lines changed

samtranslator/model/lambda_.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class LambdaFunction(Resource):
2727
"CodeSigningConfigArn": PropertyType(False, is_str()),
2828
"ImageConfig": PropertyType(False, is_type(dict)),
2929
"Architectures": PropertyType(False, list_of(one_of(is_str(), is_type(dict)))),
30+
"SnapStart": PropertyType(False, is_type(dict)),
3031
"EphemeralStorage": PropertyType(False, is_type(dict)),
3132
}
3233

samtranslator/model/sam_resources.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class SamFunction(SamResourceMacro):
112112
"ImageConfig": PropertyType(False, is_type(dict)),
113113
"CodeSigningConfigArn": PropertyType(False, is_str()),
114114
"Architectures": PropertyType(False, list_of(one_of(is_str(), is_type(dict)))),
115+
"SnapStart": PropertyType(False, is_type(dict)),
115116
"FunctionUrlConfig": PropertyType(False, is_type(dict)),
116117
}
117118
event_resolver = ResourceTypeResolver( # type: ignore[no-untyped-call]
@@ -458,6 +459,7 @@ def _construct_lambda_function(self): # type: ignore[no-untyped-def]
458459
lambda_function.ImageConfig = self.ImageConfig # type: ignore[attr-defined]
459460
lambda_function.PackageType = self.PackageType # type: ignore[attr-defined]
460461
lambda_function.Architectures = self.Architectures # type: ignore[attr-defined]
462+
lambda_function.SnapStart = self.SnapStart # type: ignore[attr-defined]
461463
lambda_function.EphemeralStorage = self.EphemeralStorage # type: ignore[attr-defined]
462464

463465
if self.Tracing: # type: ignore[attr-defined]
@@ -810,6 +812,9 @@ def _construct_version(self, function, intrinsics_resolver, code_sha256=None):
810812
logical_dict.update(function.Environment)
811813
if function.MemorySize:
812814
logical_dict.update({"MemorySize": function.MemorySize})
815+
# If SnapStart is enabled we want to publish a new version, to have the corresponding snapshot
816+
if function.SnapStart and function.SnapStart.get("ApplyOn", "None") != "None":
817+
logical_dict.update({"SnapStart": function.SnapStart})
813818
logical_id = logical_id_generator.LogicalIdGenerator(prefix, logical_dict, code_sha256).gen()
814819

815820
attributes = self.get_passthrough_resource_attributes() # type: ignore[no-untyped-call]

samtranslator/plugins/globals/globals.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Globals(object):
4545
"FileSystemConfigs",
4646
"CodeSigningConfigArn",
4747
"Architectures",
48+
"SnapStart",
4849
"EphemeralStorage",
4950
"FunctionUrlConfig",
5051
],
@@ -85,7 +86,9 @@ class Globals(object):
8586
SamResourceType.SimpleTable.value: ["SSESpecification"],
8687
}
8788
# unreleased_properties *must be* part of supported_properties too
88-
unreleased_properties: Dict[str, List[str]] = {}
89+
unreleased_properties: Dict[str, List[str]] = {
90+
SamResourceType.Function.value: ["SnapStart"],
91+
}
8992

9093
def __init__(self, template): # type: ignore[no-untyped-def]
9194
"""
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
%YAML 1.1
2+
---
3+
Parameters:
4+
SnapStartParam:
5+
Type: String
6+
Default: None
7+
8+
Resources:
9+
SnapStartFunction:
10+
Type: AWS::Serverless::Function
11+
Properties:
12+
CodeUri: s3://sam-demo-bucket/hello.zip
13+
Handler: hello.handler
14+
Runtime: python3.9
15+
SnapStart:
16+
ApplyOn: PublishedVersions
17+
18+
SnapStartParameterFunction:
19+
Type: AWS::Serverless::Function
20+
Properties:
21+
CodeUri: s3://sam-demo-bucket/hello.zip
22+
Handler: hello.handler
23+
Runtime: python3.9
24+
SnapStart:
25+
ApplyOn: !Ref SnapStartParam
26+
27+
SnapStartFunctionWithAlias:
28+
Type: AWS::Serverless::Function
29+
Properties:
30+
CodeUri: s3://sam-demo-bucket/hello.zip
31+
Handler: hello.handler
32+
Runtime: python3.9
33+
AutoPublishAlias: live
34+
SnapStart:
35+
ApplyOn: PublishedVersions

tests/translator/input/globals_for_function.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Globals:
2626
ReservedConcurrentExecutions: 50
2727
Architectures:
2828
- x86_64
29+
SnapStart:
30+
ApplyOn: PublishedVersions
2931
EphemeralStorage:
3032
Size: 1024
3133

@@ -54,4 +56,6 @@ Resources:
5456
PermissionsBoundary: arn:aws:1234:iam:boundary/OverridePermissionsBoundary
5557
Layers:
5658
- !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:layer:MyLayer2:2
59+
SnapStart:
60+
ApplyOn: None
5761
ReservedConcurrentExecutions: 100
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
{
2+
"Parameters": {
3+
"SnapStartParam": {
4+
"Default": "None",
5+
"Type": "String"
6+
}
7+
},
8+
"Resources": {
9+
"SnapStartFunction": {
10+
"Properties": {
11+
"Code": {
12+
"S3Bucket": "sam-demo-bucket",
13+
"S3Key": "hello.zip"
14+
},
15+
"Handler": "hello.handler",
16+
"Role": {
17+
"Fn::GetAtt": [
18+
"SnapStartFunctionRole",
19+
"Arn"
20+
]
21+
},
22+
"Runtime": "python3.9",
23+
"SnapStart": {
24+
"ApplyOn": "PublishedVersions"
25+
},
26+
"Tags": [
27+
{
28+
"Key": "lambda:createdBy",
29+
"Value": "SAM"
30+
}
31+
]
32+
},
33+
"Type": "AWS::Lambda::Function"
34+
},
35+
"SnapStartFunctionRole": {
36+
"Properties": {
37+
"AssumeRolePolicyDocument": {
38+
"Statement": [
39+
{
40+
"Action": [
41+
"sts:AssumeRole"
42+
],
43+
"Effect": "Allow",
44+
"Principal": {
45+
"Service": [
46+
"lambda.amazonaws.com"
47+
]
48+
}
49+
}
50+
],
51+
"Version": "2012-10-17"
52+
},
53+
"ManagedPolicyArns": [
54+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
55+
],
56+
"Tags": [
57+
{
58+
"Key": "lambda:createdBy",
59+
"Value": "SAM"
60+
}
61+
]
62+
},
63+
"Type": "AWS::IAM::Role"
64+
},
65+
"SnapStartFunctionWithAlias": {
66+
"Properties": {
67+
"Code": {
68+
"S3Bucket": "sam-demo-bucket",
69+
"S3Key": "hello.zip"
70+
},
71+
"Handler": "hello.handler",
72+
"Role": {
73+
"Fn::GetAtt": [
74+
"SnapStartFunctionWithAliasRole",
75+
"Arn"
76+
]
77+
},
78+
"Runtime": "python3.9",
79+
"SnapStart": {
80+
"ApplyOn": "PublishedVersions"
81+
},
82+
"Tags": [
83+
{
84+
"Key": "lambda:createdBy",
85+
"Value": "SAM"
86+
}
87+
]
88+
},
89+
"Type": "AWS::Lambda::Function"
90+
},
91+
"SnapStartFunctionWithAliasAliaslive": {
92+
"Properties": {
93+
"FunctionName": {
94+
"Ref": "SnapStartFunctionWithAlias"
95+
},
96+
"FunctionVersion": {
97+
"Fn::GetAtt": [
98+
"SnapStartFunctionWithAliasVersion0abd29242e",
99+
"Version"
100+
]
101+
},
102+
"Name": "live"
103+
},
104+
"Type": "AWS::Lambda::Alias"
105+
},
106+
"SnapStartFunctionWithAliasRole": {
107+
"Properties": {
108+
"AssumeRolePolicyDocument": {
109+
"Statement": [
110+
{
111+
"Action": [
112+
"sts:AssumeRole"
113+
],
114+
"Effect": "Allow",
115+
"Principal": {
116+
"Service": [
117+
"lambda.amazonaws.com"
118+
]
119+
}
120+
}
121+
],
122+
"Version": "2012-10-17"
123+
},
124+
"ManagedPolicyArns": [
125+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
126+
],
127+
"Tags": [
128+
{
129+
"Key": "lambda:createdBy",
130+
"Value": "SAM"
131+
}
132+
]
133+
},
134+
"Type": "AWS::IAM::Role"
135+
},
136+
"SnapStartFunctionWithAliasVersion0abd29242e": {
137+
"DeletionPolicy": "Retain",
138+
"Properties": {
139+
"FunctionName": {
140+
"Ref": "SnapStartFunctionWithAlias"
141+
}
142+
},
143+
"Type": "AWS::Lambda::Version"
144+
},
145+
"SnapStartParameterFunction": {
146+
"Properties": {
147+
"Code": {
148+
"S3Bucket": "sam-demo-bucket",
149+
"S3Key": "hello.zip"
150+
},
151+
"Handler": "hello.handler",
152+
"Role": {
153+
"Fn::GetAtt": [
154+
"SnapStartParameterFunctionRole",
155+
"Arn"
156+
]
157+
},
158+
"Runtime": "python3.9",
159+
"SnapStart": {
160+
"ApplyOn": {
161+
"Ref": "SnapStartParam"
162+
}
163+
},
164+
"Tags": [
165+
{
166+
"Key": "lambda:createdBy",
167+
"Value": "SAM"
168+
}
169+
]
170+
},
171+
"Type": "AWS::Lambda::Function"
172+
},
173+
"SnapStartParameterFunctionRole": {
174+
"Properties": {
175+
"AssumeRolePolicyDocument": {
176+
"Statement": [
177+
{
178+
"Action": [
179+
"sts:AssumeRole"
180+
],
181+
"Effect": "Allow",
182+
"Principal": {
183+
"Service": [
184+
"lambda.amazonaws.com"
185+
]
186+
}
187+
}
188+
],
189+
"Version": "2012-10-17"
190+
},
191+
"ManagedPolicyArns": [
192+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
193+
],
194+
"Tags": [
195+
{
196+
"Key": "lambda:createdBy",
197+
"Value": "SAM"
198+
}
199+
]
200+
},
201+
"Type": "AWS::IAM::Role"
202+
}
203+
}
204+
}

tests/translator/output/aws-cn/globals_for_function.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
]
3838
},
3939
"Runtime": "nodejs12.x",
40+
"SnapStart": {
41+
"ApplyOn": "None"
42+
},
4043
"Tags": [
4144
{
4245
"Key": "lambda:createdBy",
@@ -165,6 +168,9 @@
165168
]
166169
},
167170
"Runtime": "python2.7",
171+
"SnapStart": {
172+
"ApplyOn": "PublishedVersions"
173+
},
168174
"Tags": [
169175
{
170176
"Key": "lambda:createdBy",
@@ -197,7 +203,7 @@
197203
},
198204
"FunctionVersion": {
199205
"Fn::GetAtt": [
200-
"MinimalFunctionVersion0a06fc8fb1",
206+
"MinimalFunctionVersione7c6f56e4d",
201207
"Version"
202208
]
203209
},
@@ -242,7 +248,7 @@
242248
},
243249
"Type": "AWS::IAM::Role"
244250
},
245-
"MinimalFunctionVersion0a06fc8fb1": {
251+
"MinimalFunctionVersione7c6f56e4d": {
246252
"DeletionPolicy": "Retain",
247253
"Properties": {
248254
"FunctionName": {

0 commit comments

Comments
 (0)