fix(cloudformation): resolve Fn::GetAtt [Vpc, DefaultSecurityGroup] (#1977)

* fix(cloudformation): resolve Fn::GetAtt [Vpc, DefaultSecurityGroup]

provisionVpc now records the VPC's default security group id as the
DefaultSecurityGroup attribute, so GetAtt resolves to the real sg-* id
instead of passing the literal 'LogicalId.DefaultSecurityGroup' string to
downstream resources (breaks CDK's Custom::VpcRestrictDefaultSG handler).

Fixes #1976

* refactor(cloudformation): extract AWS::EC2::VPC into a per-service provisioner
This commit is contained in:
lex00 authored and GitHub committed 2026-08-04 22:26:03 -05:00
1 parent 4e0a815f20
commit bdad066694
3 files changed
+116 -10

No files matched your search

@@ -338,7 +338,6 @@ public class CloudFormationResourceProvisioner {
// EC2 networking. These delegate to Ec2Service so the resources actually exist
// (describe-subnets, ELBv2, etc. can find them) instead of being stubbed with a
// fake physical id. Topological ordering guarantees parents are provisioned first.
case "AWS::EC2::VPC" -> provisionVpc(resource, properties, engine, region);
case "AWS::EC2::Subnet" -> provisionSubnet(resource, properties, engine, region);
case "AWS::EC2::SecurityGroup" -> provisionSecurityGroup(resource, properties, engine, region, stackName);
case "AWS::EC2::InternetGateway" -> provisionInternetGateway(resource, region);
@@ -586,15 +585,6 @@ public class CloudFormationResourceProvisioner {
// ELBv2 create-load-balancer, etc. resolve it). physicalId is set to the real EC2 id so
// Ref/exports resolve to a real vpc-/subnet-/... id rather than a stub.
private void provisionVpc(StackResource r, JsonNode props, CloudFormationTemplateEngine engine, String region) {
String cidr = resolveOptional(props, "CidrBlock", engine);
var vpc = ec2Service.createVpc(region, cidr, false);
r.setPhysicalId(vpc.getVpcId());
r.getAttributes().put("VpcId", vpc.getVpcId());
if (vpc.getCidrBlock() != null) {
r.getAttributes().put("CidrBlock", vpc.getCidrBlock());
}
}
private void provisionSubnet(StackResource r, JsonNode props, CloudFormationTemplateEngine engine, String region) {
String vpcId = resolveOptional(props, "VpcId", engine);
@@ -0,0 +1,52 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* CloudFormation provisioning for {@code AWS::EC2::VPC}. Extracted verbatim from
* {@code CloudFormationResourceProvisioner} as part of the per-service decomposition.
*/
@ApplicationScoped
public class Ec2VpcCfnProvisioner implements CfnResourceProvisioner {
private final Ec2Service ec2Service;
@Inject
public Ec2VpcCfnProvisioner(Ec2Service ec2Service) {
this.ec2Service = ec2Service;
}
@Override
public Set<String> resourceTypes() {
return Set.of("AWS::EC2::VPC");
}
@Override
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
String cidr = ctx.resolveOptional(props, "CidrBlock");
var vpc = ec2Service.createVpc(ctx.region(), cidr, false);
r.setPhysicalId(vpc.getVpcId());
r.getAttributes().put("VpcId", vpc.getVpcId());
if (vpc.getCidrBlock() != null) {
r.getAttributes().put("CidrBlock", vpc.getCidrBlock());
}
// Fn::GetAtt DefaultSecurityGroup — CDK's Custom::VpcRestrictDefaultSG handler
// depends on it resolving to the VPC's default security group id.
ec2Service.describeSecurityGroups(ctx.region(), List.of(), List.of("default"), Map.of()).stream()
.filter(sg -> vpc.getVpcId().equals(sg.getVpcId()))
.findFirst()
.ifPresent(sg -> r.getAttributes().put("DefaultSecurityGroup", sg.getGroupId()));
}
// No delete override: the switch this replaces had no AWS::EC2::VPC delete arm,
// so stack teardown leaves the VPC alone. Adding one here would change teardown
// behavior beyond the scope of this extraction.
}