diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md index 7fbb4e1568..c73992e018 100644 --- a/docs/services/cloudformation.md +++ b/docs/services/cloudformation.md @@ -360,11 +360,12 @@ set to exactly what the template lists. Removing the property leaves collection numeric `Version` attribute. CloudFormation resolves the version before creating the alias. As in SAM, the version's logical id is `Version` followed by ten hex digits of a SHA-1 over the function's code, with its template parameter references resolved (pseudo -parameters such as `${AWS::Region}` stay as written), and its `Environment` and `MemorySize` as -written. An update that changes any of them, or a parameter the code uses, publishes a new version -and moves the alias to it. A parameter used only in `Environment` or `MemorySize` does not, and -neither does a property outside the hash, such as `Timeout`. `AutoPublishAliasAllProperties` -hashes every property floci gives the function instead, and a string `AutoPublishCodeSha256` +parameters such as `${AWS::Region}` stay as written), its `Environment` and `MemorySize` as +written, and its `SnapStart` when `ApplyOn` is anything but `None`. An update that changes any of +them, or a parameter the code uses, publishes a new version and moves the alias to it. A parameter +used only in `Environment` or `MemorySize` does not, and neither does a property outside the hash, +such as `Timeout`. `AutoPublishAliasAllProperties` hashes every property floci gives the function, +and its `SnapStart`, instead, and a string `AutoPublishCodeSha256` replaces the hash with its first ten characters. A new id publishes only when the function's code or configuration changed since its last version, so a new `AutoPublishCodeSha256` alone, or a `Tags` edit under `AutoPublishAliasAllProperties`, leaves the alias where it is. On AWS a new diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java index f27dabdced..87343e9729 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java @@ -1083,16 +1083,15 @@ class SamTransformProcessor { } /** - * SAM's logical id for the version it publishes: {@code Version} and the first ten hex - * digits of the SHA-1 of the function's {@code Code}, with references to template parameters - * resolved, merged with its {@code Environment} and {@code MemorySize}, or of all its properties - * under {@code AutoPublishAliasAllProperties}, as sorted compact JSON. A string - * {@code AutoPublishCodeSha256} takes the hash's place with its own first ten characters, as - * SAM's {@code LogicalIdGenerator} does with a given hash. A change to the hashed input is a new - * logical id: the update publishes a new version and the alias moves to it. Unchanged, the id - * and the version it published stay, since the version provisioner never republishes under the - * same id. Like Python's {@code json.dumps} by default, the JSON escapes every character - * outside printable ASCII, in lowercase hex. + * The logical id SAM gives the version it publishes: {@code Version} and the first ten + * hex digits of the SHA-1 of the function's {@code Code}, with references to template parameters + * resolved, merged with its {@code Environment}, its {@code MemorySize} and a {@code SnapStart} + * that applies, or of all its properties under {@code AutoPublishAliasAllProperties}, as sorted + * compact JSON with every character outside printable ASCII escaped in lowercase hex. A string + * {@code AutoPublishCodeSha256} names the version itself, by its first ten characters. A change + * to the hashed input is a new logical id: the update publishes a new version and the alias moves + * to it. Unchanged, the id and the version it published stay, since the version provisioner never + * republishes under the same id. */ private String versionLogicalId(String functionLogicalId, JsonNode samProperties, JsonNode functionProperties, JsonNode template, Map parameterValues) { @@ -1107,11 +1106,15 @@ class SamTransformProcessor { // SAM resolves the code in place, so the all-properties hash sees it resolved as well. JsonNode code = resolveParameterRefs(functionProperties.path("Code"), template, parameterValues); ObjectNode hashed = objectMapper.createObjectNode(); + JsonNode snapStart = samProperties.path("SnapStart"); if (samProperties.path("AutoPublishAliasAllProperties").asBoolean(false)) { hashed.setAll((ObjectNode) functionProperties.deepCopy()); if (code.isObject()) { hashed.set("Code", code); } + if (isPropertyPresent(snapStart)) { + hashed.set("SnapStart", snapStart.deepCopy()); + } } else { if (code.isObject()) { hashed.setAll((ObjectNode) code); @@ -1123,6 +1126,10 @@ class SamTransformProcessor { if (functionProperties.has("MemorySize")) { hashed.set("MemorySize", functionProperties.get("MemorySize").deepCopy()); } + // Turning SnapStart on publishes a version, so that version gets its snapshot. + if (snapStart.isObject() && !"None".equals(snapStart.path("ApplyOn").asText("None"))) { + hashed.set("SnapStart", snapStart.deepCopy()); + } } String hashInput; try { diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java index 632a6de32a..6c798fafd2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java @@ -180,6 +180,40 @@ class SamTransformProcessorTest { "nested keys are sorted too"); } + /** + * SAM also hashes a function's SnapStart when its ApplyOn is anything but "None", so turning + * SnapStart on publishes a new version; absent, empty or "None" leaves the id unchanged. With + * AutoPublishAliasAllProperties it is hashed like every other property. + */ + @Test + void expandSamTemplate_autoPublishAliasVersionIdIncludesAppliedSnapStart() throws Exception { + String template = """ + { + "Transform": "AWS::Serverless-2016-10-31", + "Resources": { + "MyFunc": { + "Type": "AWS::Serverless::Function", + "Properties": { + "Handler": "index.handler", "Runtime": "java21", "AutoPublishAlias": "live", + "InlineCode": "exports.handler = async () => ({});"%s + } + } + } + } + """; + String snapStart = ", \"SnapStart\": {\"ApplyOn\": \"PublishedVersions\"}"; + + assertEquals("MyFuncVersiona8f283d3fa", publishedVersionId(template.formatted(snapStart))); + assertEquals("MyFuncVersion2154246fbb", + publishedVersionId(template.formatted(", \"MemorySize\": 256" + snapStart))); + for (String off : List.of("", ", \"SnapStart\": {\"ApplyOn\": \"None\"}", ", \"SnapStart\": {}")) { + assertEquals("MyFuncVersion102360dfc3", publishedVersionId(template.formatted(off)), off); + } + String allProperties = ", \"AutoPublishAliasAllProperties\": true"; + assertNotEquals(publishedVersionId(template.formatted(allProperties)), + publishedVersionId(template.formatted(allProperties + ", \"SnapStart\": {\"ApplyOn\": \"None\"}"))); + } + /** * Python's {@code json.dumps} escapes everything outside printable ASCII, DEL included, in * lowercase hex, and a character beyond the Basic Multilingual Plane as its surrogate pair.