fix(ec2): derive default vpc/subnet/sg ids per region (#2384)

* fix(ec2): derive default vpc/subnet/sg ids per region

The default VPC, subnets, and security group seeded by ensureDefaultResources
used literal ids ("vpc-default", "subnet-default-a/b/c", "sg-default")
regardless of region. Storage was already keyed by region, so nothing
actually collided server-side, but every region echoed identical ids back
to callers, making a region's default resources indistinguishable from any
other region's via the AWS SDK/CLI.

Add Ec2Service.defaultVpcId/defaultSubnetId/defaultSecurityGroupId, which
derive region-scoped ids (e.g. "vpc-default-eu-west-1"), and use them at
every seeding and fallback-resolution call site in Ec2Service. RdsService's
default DB subnet group lookup now calls Ec2Service.defaultVpcId(region) too,
so it keeps resolving against the right region's default VPC.

Updates tests across Ec2Service/Ec2Integration/Eks/ElbV2/AutoScaling/
CloudFormation that asserted on the old literal ids, and adds a regression
test (Ec2ServiceTest#defaultVpcSubnetAndSecurityGroupIdsAreRegionScoped)
asserting two different regions get distinct default vpc/subnet/sg ids.

Fixes lex00/floci#21

* fix(ec2,rds): resolve default vpc/sg ids against persisted storage

ensureDefaultResources skips reseeding a region that already has a VPC on
file, so storage written before default ids were made region-scoped keeps
its old unscoped literal ("vpc-default"/"sg-default") forever. Every lookup
that blindly computed the new scoped id instead of resolving against what
storage actually holds would silently stop finding that region's real
default VPC and security group.

Added Ec2Service.resolveDefaultVpcId/resolveDefaultSecurityGroupId, which
check the scoped id first and fall back to the pre-#21 literal. Used them
at every lookup call site (runInstances' default subnet/SG fallback,
createSecurityGroup's default-VPC fallback, DescribeAccountAttributes,
and RdsService.buildDefaultSubnetGroup's vpc-id filter). Seeding itself is
unchanged, it always assigns the new scoped id for a genuinely fresh
region.

RdsServiceTest mocks Ec2Service, so its shared setup now stubs
resolveDefaultVpcId to match the old static call's behavior.

* test(compat): fetch RDS proxy subnet ids per signed region

sdkScopesTheSameDbProxyNameBySignedRegion reused the shared us-east-1
subnetIds field for both the east and west proxy, which only worked
because default subnet ids collided across regions before #21. Real AWS
scopes subnet ids per region and rejects a cross-region id, which is
exactly what floci now correctly does too.

Added subnetIdsFor(Region) and an ec2Client(Region) fixture, and changed
createIamProxy to take explicit subnet ids instead of the shared field.

* test(cloudformation): use region-scoped subnet ids in the update-reconcile test

dbSubnetGroupTemplate hardcoded the pre-#21 unscoped "subnet-default-a/b"
literals, written before this branch's default-id fix landed. Broke on
rebase since those subnets no longer exist under that name. Matches the
sibling test method in the same file, which already used
Ec2Service.defaultSubnetId(region, az).

* docs(ec2): spell out the #21 comment references

Bare #21 in this repo links to floci-io/floci#21, not the fork issue the
comment means.
This commit is contained in:
lex00 authored and GitHub committed 2026-08-19 17:14:48 -04:00
1 parent 41ff13e66d
commit 337dead734
11 files changed
+247 -66

No files matched your search

@@ -232,8 +232,10 @@ class RdsControlPlaneTest {
String regionalProxyName = TestFixtures.uniqueName("rds-proxy-regional");
try (RdsClient east = rdsClient(Region.US_EAST_1);
RdsClient west = rdsClient(Region.US_WEST_2)) {
var eastProxy = createIamProxy(east, regionalProxyName);
var westProxy = createIamProxy(west, regionalProxyName);
// Subnet ids are region-scoped on real AWS (and on floci, since #21), so each proxy
// needs subnets from its own signed region rather than the shared us-east-1 subnetIds.
var eastProxy = createIamProxy(east, regionalProxyName, subnetIdsFor(Region.US_EAST_1));
var westProxy = createIamProxy(west, regionalProxyName, subnetIdsFor(Region.US_WEST_2));
assertThat(eastProxy.dbProxy().dbProxyArn()).contains(":rds:us-east-1:");
assertThat(westProxy.dbProxy().dbProxyArn()).contains(":rds:us-west-2:");
@@ -344,15 +346,34 @@ class RdsControlPlaneTest {
}
}
private static CreateDbProxyResponse createIamProxy(RdsClient client, String name) {
private static CreateDbProxyResponse createIamProxy(RdsClient client, String name, List<String> vpcSubnetIds) {
return client.createDBProxy(b -> b
.dbProxyName(name)
.engineFamily("POSTGRESQL")
.roleArn("arn:aws:iam::000000000000:role/rds-proxy-regional")
.vpcSubnetIds(subnetIds)
.vpcSubnetIds(vpcSubnetIds)
.defaultAuthScheme("IAM_AUTH"));
}
private static List<String> subnetIdsFor(Region region) {
try (Ec2Client ec2 = ec2Client(region)) {
return ec2.describeSubnets().subnets().stream()
.map(subnet -> subnet.subnetId())
.sorted()
.limit(2)
.toList();
}
}
private static Ec2Client ec2Client(Region region) {
return Ec2Client.builder()
.endpointOverride(TestFixtures.endpoint())
.region(region)
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create("test", "test")))
.build();
}
private static software.amazon.awssdk.services.rds.model.CreateDbInstanceResponse createDbInstance(
RdsClient client, String name, String password) {
return client.createDBInstance(b -> b