fix(cloudformation): read ECS awsvpc subnets and security groups as lists (#6180)

* fix(cloudformation): read ECS awsvpc subnets and security groups as lists

AWS::ECS::Service resolves Subnets and SecurityGroups with the engine's list resolver, so a Ref to a List<AWS::EC2::Subnet::Id> parameter gives every subnet.

* fix(cloudformation): read ECS awsvpc lists from the branch Fn::If chooses

The provisioner reads Subnets and SecurityGroups from the node with each Fn::If replaced by its chosen branch, so a conditional AwsvpcConfiguration keeps its subnets.
This commit is contained in:
lex00 authored and GitHub committed 2026-10-21 16:00:56 -04:00
1 parent 110f58ae34
commit 09343917c1
2 files changed
+94 -5

No files matched your search

@@ -628,12 +628,13 @@ public class EcsCfnProvisioner implements CfnResourceProvisioner {
if (resolved == null || !resolved.isObject() || !resolved.hasNonNull("AwsvpcConfiguration")) {
return null;
}
JsonNode awsvpc = resolved.path("AwsvpcConfiguration");
JsonNode awsvpc = ctx.engine().omitNoValue(node).path("AwsvpcConfiguration");
AwsVpcConfiguration awsvpcConfig = new AwsVpcConfiguration();
awsvpcConfig.setSubnets(toStringList(awsvpc.path("Subnets")));
awsvpcConfig.setSecurityGroups(toStringList(awsvpc.path("SecurityGroups")));
if (awsvpc.hasNonNull("AssignPublicIp")) {
awsvpcConfig.setAssignPublicIp(awsvpc.path("AssignPublicIp").asText());
awsvpcConfig.setSubnets(ctx.engine().resolveStringList(awsvpc.get("Subnets")));
awsvpcConfig.setSecurityGroups(ctx.engine().resolveStringList(awsvpc.get("SecurityGroups")));
JsonNode assignPublicIp = resolved.path("AwsvpcConfiguration").path("AssignPublicIp");
if (!assignPublicIp.isMissingNode() && !assignPublicIp.isNull()) {
awsvpcConfig.setAssignPublicIp(assignPublicIp.asText());
}
NetworkConfiguration networkConfiguration = new NetworkConfiguration();
networkConfiguration.setAwsvpcConfiguration(awsvpcConfig);
@@ -16,6 +16,7 @@ import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.startsWith;
/**
* CloudFormation reports an {@code AWS::ECS::Service} complete only once the service is stable,
@@ -93,6 +94,63 @@ class CloudFormationEcsServiceStableIntegrationTest {
}
""";
private static final String LIST_PARAMETER_TEMPLATE = """
{
"Parameters": {
"SubnetIds": { "Type": "List<AWS::EC2::Subnet::Id>" },
"Private": { "Type": "String", "Default": "true" }
},
"Conditions": { "IsPrivate": { "Fn::Equals": [{ "Ref": "Private" }, "true"] } },
"Resources": {
"Cluster": {
"Type": "AWS::ECS::Cluster",
"Properties": { "ClusterName": "%1$s" }
},
"TaskSg": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": { "GroupDescription": "%1$s tasks" }
},
"TaskDef": {
"Type": "AWS::ECS::TaskDefinition",
"Properties": {
"Family": "%1$s-td",
"NetworkMode": "awsvpc",
"RequiresCompatibilities": ["FARGATE"],
"Cpu": "256",
"Memory": "512",
"ContainerDefinitions": [
{ "Name": "app", "Image": "app:v1", "Essential": true }
]
}
},
"Service": {
"Type": "AWS::ECS::Service",
"Properties": {
"ServiceName": "%1$s-svc",
"Cluster": { "Ref": "Cluster" },
"TaskDefinition": { "Ref": "TaskDef" },
"DesiredCount": 1,
"LaunchType": "FARGATE",
"NetworkConfiguration": {
"AwsvpcConfiguration": { "Fn::If": ["IsPrivate",
{
"AssignPublicIp": "DISABLED",
"SecurityGroups": [{ "Fn::GetAtt": ["TaskSg", "GroupId"] }],
"Subnets": { "Ref": "SubnetIds" }
},
{
"AssignPublicIp": "ENABLED",
"SecurityGroups": [{ "Fn::GetAtt": ["TaskSg", "GroupId"] }],
"Subnets": { "Ref": "SubnetIds" }
}
] }
}
}
}
}
}
""";
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
@@ -149,6 +207,36 @@ class CloudFormationEcsServiceStableIntegrationTest {
equalTo(List.of(subnet)));
}
@Test
void createStack_awsvpcServiceWithSubnetListParameterUnderFnIf_usesEverySubnet() {
String name = "cfn-stable-list-" + Long.toString(System.nanoTime(), 36);
String subnetA = Ec2Service.defaultSubnetId("us-east-1", "a");
String subnetB = Ec2Service.defaultSubnetId("us-east-1", "b");
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateStack")
.formParam("StackName", name)
.formParam("TemplateBody", LIST_PARAMETER_TEMPLATE.formatted(name))
.formParam("Parameters.member.1.ParameterKey", "SubnetIds")
.formParam("Parameters.member.1.ParameterValue", subnetA + "," + subnetB)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<StackId>"));
assertThat(stackStatus(name), equalTo("CREATE_COMPLETE"));
Response service = describeService(name);
assertThat(service.path("services[0].networkConfiguration.awsvpcConfiguration.subnets"),
equalTo(List.of(subnetA, subnetB)));
List<String> groups = service.path("services[0].networkConfiguration.awsvpcConfiguration.securityGroups");
assertThat(groups, hasSize(1));
assertThat(groups.get(0), startsWith("sg-"));
assertThat(service.path("services[0].networkConfiguration.awsvpcConfiguration.assignPublicIp"),
equalTo("DISABLED"));
}
private static void stackCall(String action, String stackName, String template) {
given()
.contentType("application/x-www-form-urlencoded")