fix(cloudformation): append AutoPublishCodeSha256 to a SAM function's Description (#6364)

This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-24 17:44:13 -04:00
1 parent 998276b8f5
commit 9c6d6a0ce8
4 files changed
+163 -5

No files matched your search

@@ -279,6 +279,7 @@ class SamTransformIntegrationTest {
FunctionName: sam-code-sha-func
Handler: index.handler
Runtime: nodejs20.x
Description: Release
AutoPublishAlias: live
AutoPublishCodeSha256: "%s"
InlineCode: "exports.handler = async () => ({ release: '%s' });"
@@ -310,6 +311,20 @@ class SamTransformIntegrationTest {
.then().statusCode(200).body("FunctionVersion", equalTo("2"));
given().when().get("/2015-03-31/functions/" + function + "/versions")
.then().statusCode(200).body("Versions.Version", equalTo(List.of("$LATEST", "1", "2")));
// A new hash alone publishes too: SAM appends it to the function's Description, so the
// function changes even though its code does not.
given().contentType("application/x-www-form-urlencoded")
.formParam("Action", "UpdateStack").formParam("StackName", stackName)
.formParam("TemplateBody", template.formatted("cccccccccc", "second"))
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
.when().post("/").then().statusCode(200);
waitForStackStatus(stackName, "UPDATE_COMPLETE");
given().when().get("/2015-03-31/functions/" + function + "/aliases/live")
.then().statusCode(200).body("FunctionVersion", equalTo("3"));
given().queryParam("Qualifier", "live")
.when().get("/2015-03-31/functions/" + function + "/configuration")
.then().statusCode(200).body("Version", equalTo("3")).body("Description", equalTo("Release cccccccccc"));
}
/** Under AutoPublishAliasAllProperties a property outside the default hash, here Timeout, publishes. */
@@ -636,6 +651,7 @@ class SamTransformIntegrationTest {
Handler: app.handler
Runtime: python3.12
InlineCode: "def handler(event, context): return {'statusCode': 200}"
Description: Configured by SAM
Timeout: 30
MemorySize: 256
Environment:
@@ -674,6 +690,7 @@ class SamTransformIntegrationTest {
.body("Configuration.FunctionName", equalTo("sam-configured-func"))
.body("Configuration.Handler", equalTo("app.handler"))
.body("Configuration.Runtime", equalTo("python3.12"))
.body("Configuration.Description", equalTo("Configured by SAM"))
.body("Configuration.Timeout", equalTo(30))
.body("Configuration.MemorySize", equalTo(256))
.body("Configuration.Environment.Variables.TABLE_NAME", equalTo("my-table"))
@@ -357,6 +357,111 @@ class SamTransformProcessorTest {
"only the first ten characters name the version");
}
/** As in SAM, the function's Description reaches the Lambda function as written. */
@Test
void expandSamTemplate_functionDescriptionIsCopiedToTheFunction() throws Exception {
String template = """
{
"Transform": "AWS::Serverless-2016-10-31",
"Parameters": {"Purpose": {"Type": "String", "Default": "orders"}},
"Resources": {
"MyFunc": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "index.handler", "Runtime": "nodejs20.x",
"InlineCode": "exports.handler = async () => ({});"%s
}
}
}
}
""";
assertEquals("\"Orders API\"", functionDescription(template.formatted(", \"Description\": \"Orders API\"")));
assertEquals("{\"Ref\":\"Purpose\"}", functionDescription(template.formatted(
", \"Description\": {\"Ref\": \"Purpose\"}")), "an intrinsic function is left to CloudFormation");
assertEquals("\"Orders API\"", functionDescription(template.formatted(
", \"Description\": \"Orders API\", \"AutoPublishAlias\": \"live\"")),
"AutoPublishAlias without AutoPublishCodeSha256 leaves it as written");
assertEquals("\"Orders API\"", functionDescription(template.formatted(
", \"Description\": \"Orders API\", \"AutoPublishCodeSha256\": \"abc\"")),
"AutoPublishCodeSha256 without AutoPublishAlias leaves it as written");
assertTrue(processor.expandSamTemplate(objectMapper.readTree(template.formatted(""))).path("Resources")
.path("MyFunc").path("Properties").path("Description").isMissingNode());
String allProperties = ", \"AutoPublishAlias\": \"live\", \"AutoPublishAliasAllProperties\": true";
assertNotEquals(publishedVersionId(template.formatted(allProperties + ", \"Description\": \"first\"")),
publishedVersionId(template.formatted(allProperties + ", \"Description\": \"second\"")),
"under AutoPublishAliasAllProperties a new Description names a new version");
}
/**
* With AutoPublishAlias, SAM appends a given AutoPublishCodeSha256 to the function's Description, so
* that a new value alone changes the function and a version publishes for it. A function without a
* description gets the bare hash, a plain string gets the hash after a space, and an intrinsic
* function is wrapped in an Fn::Join with the hash.
*/
@Test
void expandSamTemplate_autoPublishCodeSha256IsAppendedToTheDescription() throws Exception {
String template = """
{
"Transform": "AWS::Serverless-2016-10-31",
"Parameters": {
"AutoPublishCodeSha256": {"Type": "String",
"Default": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b"},
"Purpose": {"Type": "String", "Default": "orders"}
},
"Resources": {
"MyFunc": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "index.handler", "Runtime": "nodejs20.x", "AutoPublishAlias": "live",
"InlineCode": "exports.handler = async () => ({});", "AutoPublishCodeSha256": %s%s
}
}
}
}
""";
String sha = "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b";
String ref = "{\"Ref\": \"AutoPublishCodeSha256\"}";
assertEquals("\"" + sha + "\"", functionDescription(template.formatted(ref, "")));
assertEquals("\"My testing description " + sha + "\"",
functionDescription(template.formatted(ref, ", \"Description\": \"My testing description\"")));
assertEquals("{\"Fn::Join\":[\" \",[{\"Fn::Join\":[\":\",[\"a\",\"b\",\"c\"]]},\"" + sha + "\"]]}",
functionDescription(template.formatted(ref,
", \"Description\": {\"Fn::Join\": [\":\", [\"a\", \"b\", \"c\"]]}")));
assertEquals("\"payments abc\"", functionDescription(template.formatted("\"abc\"",
", \"Description\": {\"Ref\": \"Purpose\"}"), Map.of("Purpose", "payments")),
"a parameter is resolved first, as SAM resolves it");
assertEquals("{\"Fn::Join\":[\" \",[{\"Fn::Sub\":\"payments handler\"},\"abc\"]]}",
functionDescription(template.formatted("\"abc\"", ", \"Description\": {\"Fn::Sub\": "
+ "\"${Purpose} handler\"}"), Map.of("Purpose", "payments")),
"any other intrinsic function stays, with its parameters resolved");
assertEquals("\"abc\"", functionDescription(template.formatted("\"abc\"", ", \"Description\": \"\"")));
assertEquals("\"orders\"", functionDescription(template.formatted("\"\"", ", \"Description\": \"orders\"")),
"an empty AutoPublishCodeSha256 appends nothing");
assertEquals("{\"Ref\":\"Purpose\"}", functionDescription(template.formatted("\"\"",
", \"Description\": {\"Ref\": \"Purpose\"}"), Map.of("Purpose", "payments")),
"with nothing appended the Description stays as written");
assertEquals("\"Shared abc\"", functionDescription(template.formatted("\"abc\"", "").replace("\"Resources\"",
"\"Globals\": {\"Function\": {\"Description\": \"Shared\"}}, \"Resources\"")),
"a Description from Globals takes the value too");
assertEquals("\"orders \"", functionDescription(template.formatted(ref, ", \"Description\": \"orders\""),
Map.of("AutoPublishCodeSha256", "")),
"SAM tests the property as written, so a Ref to an empty value still appends it");
assertEquals("\"\"", functionDescription(template.formatted(ref, ""), Map.of("AutoPublishCodeSha256", "")),
"and a function without a Description gets an empty one");
}
private String functionDescription(String template) throws Exception {
return functionDescription(template, Map.of());
}
private String functionDescription(String template, Map<String, String> parameterValues) throws Exception {
return processor.expandSamTemplate(objectMapper.readTree(template), parameterValues).path("Resources")
.path("MyFunc").path("Properties").path("Description").toString();
}
/** SAM's types for the other version properties, and VersionDeletionPolicy needs AutoPublishAlias. */
@Test
void expandSamTemplate_versionPropertiesHaveSamsTypes() throws Exception {