Skip to content

Commit 0900f2b

Browse files
authored
fix: Append CodeSha256 Value to Description (#3337)
1 parent 216e7be commit 0900f2b

9 files changed

+564
-1
lines changed

integration/resources/templates/combination/connector_bucket_to_function_write.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Resources:
6464
TriggerBucket:
6565
# See also https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-notificationconfig.html
6666
DependsOn: MyConnector
67+
UpdateReplacePolicy: Delete
68+
DeletionPolicy: Delete
6769
Type: AWS::S3::Bucket
6870
Properties:
6971
BucketName: !Ref BucketName

samtranslator/model/sam_resources.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def resources_to_link(self, resources: Dict[str, Any]) -> Dict[str, Any]:
252252
raise InvalidResourceException(self.logical_id, e.message) from e
253253

254254
@cw_timer
255-
def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
255+
def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def] # noqa: PLR0912, PLR0915
256256
"""Returns the Lambda function, role, and event resources to which this SAM Function corresponds.
257257
258258
:param dict kwargs: already-converted resources that may need to be modified when converting this \
@@ -290,6 +290,15 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
290290
self.logical_id,
291291
"AutoPublishCodeSha256 must be a string",
292292
)
293+
# Lambda doesn't create a new version if the code in the unpublished version is the same as the
294+
# previous published version. To address situations where users modify only the 'CodeUri' content,
295+
# CloudFormation might not detect any changes in the Lambda function within the template, leading
296+
# to deployment issues. To resolve this, we'll append codesha256 value to the description.
297+
description = intrinsics_resolver.resolve_parameter_refs(self.Description)
298+
if not description or isinstance(description, str):
299+
lambda_function.Description = f"{description} {code_sha256}" if description else code_sha256
300+
else:
301+
lambda_function.Description = {"Fn::Join": [" ", [description, code_sha256]]}
293302
lambda_version = self._construct_version(
294303
lambda_function, intrinsics_resolver=intrinsics_resolver, code_sha256=code_sha256
295304
)

tests/translator/input/function_with_alias_and_code_sha256.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,24 @@ Resources:
1414
AutoPublishAlias: live
1515
AutoPublishCodeSha256: !Ref AutoPublishCodeSha256
1616
VersionDescription: sam-testing
17+
18+
FunctionWithIntrinsicDescription:
19+
Type: AWS::Serverless::Function
20+
Properties:
21+
CodeUri: s3://sam-demo-bucket/hello.zip
22+
Handler: hello.handler
23+
Runtime: python2.7
24+
AutoPublishAlias: live
25+
AutoPublishCodeSha256: !Ref AutoPublishCodeSha256
26+
VersionDescription: sam-testing
27+
Description: !Join [':', [a, b, c]]
28+
FunctionWithDescription:
29+
Type: AWS::Serverless::Function
30+
Properties:
31+
CodeUri: s3://sam-demo-bucket/hello.zip
32+
Handler: hello.handler
33+
Runtime: python2.7
34+
AutoPublishAlias: live
35+
AutoPublishCodeSha256: !Ref AutoPublishCodeSha256
36+
VersionDescription: sam-testing
37+
Description: My testing description

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"S3Bucket": "sam-demo-bucket",
77
"S3Key": "hello.zip"
88
},
9+
"Description": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
910
"Handler": "hello.handler",
1011
"Role": {
1112
"Fn::GetAtt": [

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

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,188 @@
77
}
88
},
99
"Resources": {
10+
"FunctionWithDescription": {
11+
"Properties": {
12+
"Code": {
13+
"S3Bucket": "sam-demo-bucket",
14+
"S3Key": "hello.zip"
15+
},
16+
"Description": "My testing description 6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
17+
"Handler": "hello.handler",
18+
"Role": {
19+
"Fn::GetAtt": [
20+
"FunctionWithDescriptionRole",
21+
"Arn"
22+
]
23+
},
24+
"Runtime": "python2.7",
25+
"Tags": [
26+
{
27+
"Key": "lambda:createdBy",
28+
"Value": "SAM"
29+
}
30+
]
31+
},
32+
"Type": "AWS::Lambda::Function"
33+
},
34+
"FunctionWithDescriptionAliaslive": {
35+
"Properties": {
36+
"FunctionName": {
37+
"Ref": "FunctionWithDescription"
38+
},
39+
"FunctionVersion": {
40+
"Fn::GetAtt": [
41+
"FunctionWithDescriptionVersion6b86b273ff",
42+
"Version"
43+
]
44+
},
45+
"Name": "live"
46+
},
47+
"Type": "AWS::Lambda::Alias"
48+
},
49+
"FunctionWithDescriptionRole": {
50+
"Properties": {
51+
"AssumeRolePolicyDocument": {
52+
"Statement": [
53+
{
54+
"Action": [
55+
"sts:AssumeRole"
56+
],
57+
"Effect": "Allow",
58+
"Principal": {
59+
"Service": [
60+
"lambda.amazonaws.com"
61+
]
62+
}
63+
}
64+
],
65+
"Version": "2012-10-17"
66+
},
67+
"ManagedPolicyArns": [
68+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
69+
],
70+
"Tags": [
71+
{
72+
"Key": "lambda:createdBy",
73+
"Value": "SAM"
74+
}
75+
]
76+
},
77+
"Type": "AWS::IAM::Role"
78+
},
79+
"FunctionWithDescriptionVersion6b86b273ff": {
80+
"DeletionPolicy": "Retain",
81+
"Properties": {
82+
"Description": "sam-testing",
83+
"FunctionName": {
84+
"Ref": "FunctionWithDescription"
85+
}
86+
},
87+
"Type": "AWS::Lambda::Version"
88+
},
89+
"FunctionWithIntrinsicDescription": {
90+
"Properties": {
91+
"Code": {
92+
"S3Bucket": "sam-demo-bucket",
93+
"S3Key": "hello.zip"
94+
},
95+
"Description": {
96+
"Fn::Join": [
97+
" ",
98+
[
99+
{
100+
"Fn::Join": [
101+
":",
102+
[
103+
"a",
104+
"b",
105+
"c"
106+
]
107+
]
108+
},
109+
"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b"
110+
]
111+
]
112+
},
113+
"Handler": "hello.handler",
114+
"Role": {
115+
"Fn::GetAtt": [
116+
"FunctionWithIntrinsicDescriptionRole",
117+
"Arn"
118+
]
119+
},
120+
"Runtime": "python2.7",
121+
"Tags": [
122+
{
123+
"Key": "lambda:createdBy",
124+
"Value": "SAM"
125+
}
126+
]
127+
},
128+
"Type": "AWS::Lambda::Function"
129+
},
130+
"FunctionWithIntrinsicDescriptionAliaslive": {
131+
"Properties": {
132+
"FunctionName": {
133+
"Ref": "FunctionWithIntrinsicDescription"
134+
},
135+
"FunctionVersion": {
136+
"Fn::GetAtt": [
137+
"FunctionWithIntrinsicDescriptionVersion6b86b273ff",
138+
"Version"
139+
]
140+
},
141+
"Name": "live"
142+
},
143+
"Type": "AWS::Lambda::Alias"
144+
},
145+
"FunctionWithIntrinsicDescriptionRole": {
146+
"Properties": {
147+
"AssumeRolePolicyDocument": {
148+
"Statement": [
149+
{
150+
"Action": [
151+
"sts:AssumeRole"
152+
],
153+
"Effect": "Allow",
154+
"Principal": {
155+
"Service": [
156+
"lambda.amazonaws.com"
157+
]
158+
}
159+
}
160+
],
161+
"Version": "2012-10-17"
162+
},
163+
"ManagedPolicyArns": [
164+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
165+
],
166+
"Tags": [
167+
{
168+
"Key": "lambda:createdBy",
169+
"Value": "SAM"
170+
}
171+
]
172+
},
173+
"Type": "AWS::IAM::Role"
174+
},
175+
"FunctionWithIntrinsicDescriptionVersion6b86b273ff": {
176+
"DeletionPolicy": "Retain",
177+
"Properties": {
178+
"Description": "sam-testing",
179+
"FunctionName": {
180+
"Ref": "FunctionWithIntrinsicDescription"
181+
}
182+
},
183+
"Type": "AWS::Lambda::Version"
184+
},
10185
"MinimalFunction": {
11186
"Properties": {
12187
"Code": {
13188
"S3Bucket": "sam-demo-bucket",
14189
"S3Key": "hello.zip"
15190
},
191+
"Description": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
16192
"Handler": "hello.handler",
17193
"Role": {
18194
"Fn::GetAtt": [

tests/translator/output/aws-us-gov/function_with_alias_all_properties_and_code_sha256.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"S3Bucket": "sam-demo-bucket",
77
"S3Key": "hello.zip"
88
},
9+
"Description": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
910
"Handler": "hello.handler",
1011
"Role": {
1112
"Fn::GetAtt": [

0 commit comments

Comments
 (0)