Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cloudformation): carry PackageType through SAM Function expansion (#1772)
Jay4a109b2f6cb6
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.javaindex 9a5dbc13f7..ba2ccedf15 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@@ -417,8 +417,10 @@ class SamTransformProcessor {ObjectNode lambdaProps = objectMapper.createObjectNode();copyIfPresent(properties, "FunctionName", lambdaProps);+ copyIfPresent(properties, "PackageType", lambdaProps);copyIfPresent(properties, "Handler", lambdaProps);copyIfPresent(properties, "Runtime", lambdaProps);+ copyIfPresent(properties, "ImageConfig", lambdaProps);lambdaProps.set("Code", buildLambdaCode(properties));diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.javaindex 37d4a92702..a922624a22 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java@@ -6504,6 +6504,122 @@ class CloudFormationIntegrationTest {.statusCode(200);}+ // ── SAM AWS::Serverless::Function PackageType: Image dropped by the transform ──++ @Test+ void createStack_samFunctionWithPackageTypeImageDeploysAsImageFunction() {+ // Without PackageType carried through by the SAM transform, CloudFormationResourceProvisioner+ // defaults PackageType to "Zip" (buildLambdaDesiredState's resolveOrDefault), which then also+ // forces Runtime/Handler defaults onto a function that declared neither — the function is+ // created as a broken Zip function instead of running the real container image.+ String template = """+ {+ "Transform": "AWS::Serverless-2016-10-31",+ "Resources": {+ "MyFunction": {+ "Type": "AWS::Serverless::Function",+ "Properties": {+ "PackageType": "Image",+ "ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest"+ }+ }+ }+ }+ """;++ String stackName = "cfn-sam-image-function-stack";++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateStack")+ .formParam("StackName", stackName)+ .formParam("TemplateBody", template)+ .formParam("Capabilities", "CAPABILITY_IAM")+ .when()+ .post("/")+ .then()+ .statusCode(200);++ String resourcesXml = given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStackResources")+ .formParam("StackName", stackName)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().asString();++ String functionName = physicalIdByLogicalId(resourcesXml, "MyFunction");++ given()+ .when().get("/2015-03-31/functions/" + functionName)+ .then()+ .statusCode(200)+ .body("Configuration.PackageType", equalTo("Image"))+ .body("Code.ImageUri", equalTo("000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest"));+ }++ @Test+ void createStack_samFunctionWithImageConfigDeploysWithOverrides() {+ // ImageConfig must also be carried through the SAM transform to CloudFormationResourceProvisioner,+ // which already reads it (provisionLambda's putResolvedMapIfPresent(configRequest, props,+ // "ImageConfig", ...)) — without the transform copying it, a PackageType: Image SAM function's+ // EntryPoint/Command/WorkingDirectory override silently never reaches the deployed function.+ String template = """+ {+ "Transform": "AWS::Serverless-2016-10-31",+ "Resources": {+ "MyFunction": {+ "Type": "AWS::Serverless::Function",+ "Properties": {+ "PackageType": "Image",+ "ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest",+ "ImageConfig": {+ "EntryPoint": ["/bootstrap"],+ "Command": ["handler.main"],+ "WorkingDirectory": "/var/task"+ }+ }+ }+ }+ }+ """;++ String stackName = "cfn-sam-imageconfig-function-stack";++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateStack")+ .formParam("StackName", stackName)+ .formParam("TemplateBody", template)+ .formParam("Capabilities", "CAPABILITY_IAM")+ .when()+ .post("/")+ .then()+ .statusCode(200);++ String resourcesXml = given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStackResources")+ .formParam("StackName", stackName)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().asString();++ String functionName = physicalIdByLogicalId(resourcesXml, "MyFunction");++ given()+ .when().get("/2015-03-31/functions/" + functionName)+ .then()+ .statusCode(200)+ .body("Configuration.ImageConfigResponse.ImageConfig.EntryPoint[0]", equalTo("/bootstrap"))+ .body("Configuration.ImageConfigResponse.ImageConfig.Command[0]", equalTo("handler.main"))+ .body("Configuration.ImageConfigResponse.ImageConfig.WorkingDirectory", equalTo("/var/task"));+ }+private static final String SFN_CONTENT_TYPE = "application/x-amz-json-1.0";@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.javaindex 3371ffa9eb..a7abb8e28f 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@@ -101,6 +101,76 @@ class SamTransformProcessorTest {assertEquals("Arn", roleRef.path("Fn::GetAtt").get(1).asText());}+ @Test+ void expandSamTemplate_functionWithPackageTypeImage() throws Exception {+ // PackageType must be carried through to the expanded AWS::Lambda::Function: without it,+ // CloudFormationResourceProvisioner.buildLambdaDesiredState defaults PackageType to "Zip"+ // (resolveOrDefault(props, "PackageType", engine, "Zip")), which then forces Runtime/Handler+ // defaults (nodejs18.x / index.handler) onto a function that never had either — the function+ // gets created as a Zip function running the wrong runtime instead of the real container image.+ JsonNode template = objectMapper.readTree("""+ {+ "Transform": "AWS::Serverless-2016-10-31",+ "Resources": {+ "MyFunc": {+ "Type": "AWS::Serverless::Function",+ "Properties": {+ "PackageType": "Image",+ "ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest"+ }+ }+ }+ }+ """);++ JsonNode expanded = processor.expandSamTemplate(template);++ JsonNode lambdaProps = expanded.path("Resources").path("MyFunc").path("Properties");+ assertEquals("Image", lambdaProps.path("PackageType").asText());+ assertEquals("000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest",+ lambdaProps.path("Code").path("ImageUri").asText());+ // No Handler/Runtime were declared and none should be synthesized by the transform itself —+ // CloudFormationResourceProvisioner is responsible for not defaulting them once it sees+ // PackageType: Image (verified separately in CloudFormationIntegrationTest).+ assertTrue(lambdaProps.path("Handler").isMissingNode());+ assertTrue(lambdaProps.path("Runtime").isMissingNode());+ }++ @Test+ void expandSamTemplate_functionWithImageConfig() throws Exception {+ // ImageConfig (EntryPoint/Command/WorkingDirectory overrides for a container-image+ // function) must also be carried through: CloudFormationResourceProvisioner.provisionLambda+ // already reads it (putResolvedMapIfPresent(configRequest, props, "ImageConfig", ...)), but+ // the SAM transform previously dropped it, silently losing any container entry-point/command+ // override on a PackageType: Image SAM function.+ JsonNode template = objectMapper.readTree("""+ {+ "Transform": "AWS::Serverless-2016-10-31",+ "Resources": {+ "MyFunc": {+ "Type": "AWS::Serverless::Function",+ "Properties": {+ "PackageType": "Image",+ "ImageUri": "000000000000.dkr.ecr.us-east-1.localhost:5100/my-repo:latest",+ "ImageConfig": {+ "EntryPoint": ["/bootstrap"],+ "Command": ["handler.main"],+ "WorkingDirectory": "/var/task"+ }+ }+ }+ }+ }+ """);++ JsonNode expanded = processor.expandSamTemplate(template);++ JsonNode imageConfig = expanded.path("Resources").path("MyFunc").path("Properties").path("ImageConfig");+ assertEquals("/bootstrap", imageConfig.path("EntryPoint").get(0).asText());+ assertEquals("handler.main", imageConfig.path("Command").get(0).asText());+ assertEquals("/var/task", imageConfig.path("WorkingDirectory").asText());+ }+@Testvoid expandSamTemplate_functionWithExplicitRole() throws Exception {JsonNode template = objectMapper.readTree("""