fix(cloudformation): carry PackageType through SAM Function expansion (#1772)

* fix(cloudformation): carry PackageType through SAM Function expansion

expandServerlessFunction/createLambdaFunction copied FunctionName, Handler,
and Runtime from AWS::Serverless::Function onto the expanded
AWS::Lambda::Function, but never PackageType. CloudFormationResourceProvisioner's
buildLambdaDesiredState defaults a missing PackageType to "Zip"
(resolveOrDefault(props, "PackageType", engine, "Zip")), which then also
forces Runtime/Handler defaults (nodejs18.x / index.handler) onto a
function that declared neither. Net effect: a SAM function declared with
PackageType: Image and an ImageUri silently deployed as a broken Zip
function running the wrong runtime instead of the real container image —
Code.ImageUri was preserved by buildLambdaCode, but nothing on the
resource said to actually use it as an image.

Found by deploying a real container-image SAM template end-to-end against
a build with #1758+#1759 applied: the function created successfully and
the stack reported CREATE_COMPLETE, but invoking it pulled
public.ecr.aws/lambda/nodejs:18 and failed with
Runtime.ImportModuleError ("Cannot find module 'index'") instead of
running the pushed image.

Adds a one-line copyIfPresent(properties, "PackageType", lambdaProps) fix,
a SamTransformProcessorTest unit test asserting the transform output
carries PackageType/ImageUri through with no synthesized Handler/Runtime,
and a CloudFormationIntegrationTest asserting the full create-stack ->
GetFunction round trip reports PackageType: Image.

* fix(cloudformation): carry ImageConfig through SAM Function expansion

Same class of omission as PackageType: CloudFormationResourceProvisioner
already reads ImageConfig off AWS::Lambda::Function (provisionLambda's
putResolvedMapIfPresent(configRequest, props, "ImageConfig", ...)), but
SamTransformProcessor.createLambdaFunction never copied it from the SAM
resource's Properties. A PackageType: Image SAM function combined with an
ImageConfig block (EntryPoint/Command/WorkingDirectory container overrides)
silently lost that configuration on the deployed function.

ImageConfig's SAM shape is identical to raw CFN's, so a plain
copyIfPresent is sufficient (no case conversion needed, unlike some other
SAM->CFN property mappings in this file).

Adds a unit test asserting the expanded resource carries ImageConfig
through, and an integration test asserting the full CreateStack ->
GetFunction round trip reports it under Configuration.ImageConfigResponse.

Addresses review feedback on #1772.
This commit is contained in:
Jay authored and GitHub committed 2026-07-15 00:54:27 -04:00
1 parent fe49bee41e
commit 4a109b2f6c
3 files changed
+188

No files matched your search

@@ -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";
@Test
@@ -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());
}
@Test
void expandSamTemplate_functionWithExplicitRole() throws Exception {
JsonNode template = objectMapper.readTree("""