feat(cloudformation): add Fn::GetAZs and Fn::Cidr intrinsics (#1365)

Implements the Fn::GetAZs and Fn::Cidr (IPv4) intrinsic functions in
CloudFormationTemplateEngine. Fn::GetAZs returns the region's availability zones;
Fn::Cidr carves a CIDR block into equally sized subnets. A resolveList helper
lets Fn::Select compose with these list-returning intrinsics. Adds an end-to-end
test that drives subnet provisioning through Fn::Select(Fn::GetAZs)/Fn::Cidr.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-06-15 21:37:31 -05:00
1 parent 0b665ab455
commit ad61bb5e2c
3 files changed
+263 -6

No files matched your search

@@ -130,6 +130,76 @@ class CloudFormationEc2IntegrationTest {
.body(containsString("<LoadBalancerArn>"));
}
@Test
void subnetAzAndCidrResolveFromGetAzsAndCidr() {
String suffix = Long.toString(System.nanoTime(), 36);
String stackName = "cfn-ec2-fn-" + suffix;
// Subnet AZ comes from Fn::Select(Fn::GetAZs("")) and its CIDR from Fn::Cidr — the
// pattern CDK emits. Both must resolve to real values on the provisioned subnet.
String template = """
{
"Resources": {
"Vpc": {
"Type": "AWS::EC2::VPC",
"Properties": {"CidrBlock": "10.30.0.0/16"}
},
"Subnet1": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {"Ref": "Vpc"},
"CidrBlock": {"Fn::Select": [1, {"Fn::Cidr": ["10.30.0.0/16", 4, 8]}]},
"AvailabilityZone": {"Fn::Select": [0, {"Fn::GetAZs": ""}]}
}
}
},
"Outputs": {
"Subnet1Id": {"Value": {"Ref": "Subnet1"}}
}
}
""";
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", "CreateStack")
.formParam("StackName", stackName)
.formParam("TemplateBody", template)
.when()
.post("/")
.then()
.statusCode(200);
String describeStacks = given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", "DescribeStacks")
.formParam("StackName", stackName)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"))
.extract().asString();
List<String> subnetIds = extractSubnetIds(describeStacks);
assertEquals(1, subnetIds.size(), "expected one exported subnet id, got: " + subnetIds);
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", EC2_AUTH)
.formParam("Action", "DescribeSubnets")
.formParam("SubnetId.1", subnetIds.get(0))
.when()
.post("/")
.then()
.statusCode(200)
// AZ resolved from Fn::Select(0, Fn::GetAZs("")) in the stack's region.
.body(containsString("ap-southeast-2a"))
// CIDR resolved from Fn::Select(1, Fn::Cidr(["10.30.0.0/16", 4, 8])).
.body(containsString("10.30.1.0/24"));
}
private static List<String> extractSubnetIds(String xml) {
Matcher m = Pattern.compile("<OutputValue>(subnet-[0-9a-fA-F]+)</OutputValue>").matcher(xml);
List<String> ids = new ArrayList<>();
@@ -0,0 +1,61 @@
package io.github.hectorvent.floci.services.cloudformation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.function.Function;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CloudFormationTemplateEngineTest {
private final ObjectMapper mapper = new ObjectMapper();
private CloudFormationTemplateEngine engine() {
return new CloudFormationTemplateEngine("000000000000", "us-east-1", "my-stack",
"stack/id", Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), mapper,
(Function<String, String>) name -> null);
}
private JsonNode json(String s) {
try {
return mapper.readTree(s);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
void getAzsReturnsStackRegionZones() {
assertEquals("us-east-1a,us-east-1b,us-east-1c",
engine().resolve(json("{\"Fn::GetAZs\": \"\"}")));
}
@Test
void getAzsHonoursExplicitRegion() {
assertEquals("eu-west-1a,eu-west-1b,eu-west-1c",
engine().resolve(json("{\"Fn::GetAZs\": \"eu-west-1\"}")));
}
@Test
void selectFromGetAzsResolvesZoneByIndex() {
CloudFormationTemplateEngine e = engine();
// CDK emits the index as a string; AWS also allows a number.
assertEquals("us-east-1a", e.resolve(json("{\"Fn::Select\": [\"0\", {\"Fn::GetAZs\": \"\"}]}")));
assertEquals("us-east-1b", e.resolve(json("{\"Fn::Select\": [1, {\"Fn::GetAZs\": \"\"}]}")));
}
@Test
void cidrSplitsBlockIntoSubnets() {
assertEquals("10.0.0.0/24,10.0.1.0/24,10.0.2.0/24,10.0.3.0/24",
engine().resolve(json("{\"Fn::Cidr\": [\"10.0.0.0/16\", 4, 8]}")));
}
@Test
void selectFromCidrResolvesSubnetByIndex() {
assertEquals("10.0.2.0/24",
engine().resolve(json("{\"Fn::Select\": [2, {\"Fn::Cidr\": [\"10.0.0.0/16\", 4, 8]}]}")));
}
}