fix(sam): append resource lists to Globals lists (#7118)

* fix(sam): append resource lists to Globals lists

* fix(sam): replace intrinsic Globals values atomically

* fix(sam): let function architectures replace globals
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-31 16:54:10 -04:00
1 parent 3cb727a85e
commit 227769385f
3 files changed
+130 -5

No files matched your search

@@ -123,9 +123,13 @@ class SamLayerVersionTransformTest {
function.put("Type", "AWS::Serverless::Function");
function.putObject("Properties").put("Runtime", "nodejs22.x").put("Handler", "index.handler")
.put("InlineCode", "exports.handler=async()=>({})");
((ObjectNode) function.path("Properties")).putArray("Layers")
.add("arn:aws:lambda:us-east-1:123456789012:layer:local:1");
JsonNode result = processor.expandSamTemplate(input);
String id = result.at("/Outputs/LayerArn/Value/Ref").asText();
assertEquals(id, result.at("/Resources/Function/Properties/Layers/0/Ref").asText());
assertEquals("arn:aws:lambda:us-east-1:123456789012:layer:local:1",
result.at("/Resources/Function/Properties/Layers/1").asText());
}
@Test
@@ -2119,6 +2119,67 @@ class SamTransformProcessorTest {
assertEquals(3, lambdaProps.path("Timeout").asInt());
}
@Test
void expandSamTemplate_prependsGlobalFunctionLayers() throws Exception {
JsonNode template = objectMapper.readTree("""
{
"Transform": "AWS::Serverless-2016-10-31",
"Globals": {"Function": {"Layers": ["arn:aws:lambda:us-east-1:123456789012:layer:shared:1"]}},
"Resources": {
"MyFunc": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Runtime": "nodejs20.x",
"Handler": "index.handler",
"InlineCode": "exports.handler = async () => ({});",
"Layers": ["arn:aws:lambda:us-east-1:123456789012:layer:local:1"]
}
}
}
}
""");
JsonNode layers = processor.expandSamTemplate(template)
.path("Resources").path("MyFunc").path("Properties").path("Layers");
assertEquals(2, layers.size());
assertEquals("arn:aws:lambda:us-east-1:123456789012:layer:shared:1", layers.get(0).asText());
assertEquals("arn:aws:lambda:us-east-1:123456789012:layer:local:1", layers.get(1).asText());
}
@Test
void expandSamTemplate_functionArchitecturesReplaceGlobals() throws Exception {
JsonNode template = objectMapper.readTree("""
{
"Transform": "AWS::Serverless-2016-10-31",
"Globals": {"Function": {"Architectures": ["x86_64"]}},
"Resources": {"MyFunc": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Runtime": "nodejs20.x",
"Handler": "index.handler",
"InlineCode": "exports.handler = async () => ({});",
"Architectures": ["arm64"]
}
}, "InheritedFunc": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Runtime": "nodejs20.x",
"Handler": "index.handler",
"InlineCode": "exports.handler = async () => ({});"
}
}}
}
""");
JsonNode expanded = processor.expandSamTemplate(template);
JsonNode architectures = expanded
.path("Resources").path("MyFunc").path("Properties").path("Architectures");
assertEquals(objectMapper.readTree("[\"arm64\"]"), architectures);
JsonNode inheritedArchitectures = expanded
.path("Resources").path("InheritedFunc").path("Properties").path("Architectures");
assertEquals(objectMapper.readTree("[\"x86_64\"]"), inheritedArchitectures);
}
@Test
void expandSamTemplate_mergesNestedMapsFromGlobals() throws Exception {
// Environment.Variables must merge key-wise: globals-only keys preserved, function keys win on clash
@@ -2153,6 +2214,50 @@ class SamTransformProcessorTest {
assertEquals("from-function", vars.path("SHARED").asText()); // clash resolved in favor of function
}
@Test
void expandSamTemplate_replacesIntrinsicValuesWithinGlobalsMaps() throws Exception {
JsonNode template = objectMapper.readTree("""
{
"Transform": "AWS::Serverless-2016-10-31",
"Globals": {"Function": {"Environment": {"Variables": {
"GLOBAL": "global",
"URL": {"Fn::Join": ["", ["https://global.example", "/old"]]},
"SCALAR": {"Fn::Sub": "global-${AWS::Region}"},
"REFERENCE": {"Ref": "GlobalParam"}
}}}},
"Resources": {"MyFunc": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Runtime": "nodejs20.x",
"Handler": "index.handler",
"InlineCode": "exports.handler = async () => ({});",
"Environment": {"Variables": {
"LOCAL": "local",
"URL": {"Fn::Join": ["", ["https://local.example", "/v1"]]},
"SCALAR": {"Fn::Base64": "local"},
"REFERENCE": {"Fn::Sub": "local-${AWS::Region}"}
}}
}
}}
}
""");
JsonNode variables = processor.expandSamTemplate(template)
.path("Resources").path("MyFunc").path("Properties")
.path("Environment").path("Variables");
assertEquals("global", variables.path("GLOBAL").asText());
assertEquals("local", variables.path("LOCAL").asText());
assertEquals(objectMapper.readTree("""
{"Fn::Join": ["", ["https://local.example", "/v1"]]}
"""), variables.path("URL"));
assertEquals(objectMapper.readTree("""
{"Fn::Base64": "local"}
"""), variables.path("SCALAR"));
assertEquals(objectMapper.readTree("""
{"Fn::Sub": "local-${AWS::Region}"}
"""), variables.path("REFERENCE"));
}
@Test
void expandSamTemplate_stripsGlobalsWhenResourcesAbsent() throws Exception {
// SAM-only Globals must be stripped even on the early return (no/!object Resources)