fix(ec2): report NAT gateways as zonal and honor PrivateIpAddress (#7059)

* fix(ec2): report NAT gateways as zonal and honor PrivateIpAddress

The Terraform AWS provider reads network_interface_id, private_ip and
public_ip from a NAT gateway only when it is reported with
availabilityMode zonal. Floci never sent availabilityMode, so all three
stayed null. Every gateway Floci creates sits in one subnet, so the
responses now say zonal.

Floci has no regional NAT gateways. A regional request that named only
its VPC failed on the missing subnet, and one that also named a subnet
got a zonal gateway, through both CreateNatGateway and
AWS::EC2::NatGateway. Both now refuse AvailabilityMode regional with
UnsupportedOperation. Any other value except zonal, such as Regional,
fails with InvalidParameterValue instead of creating a zonal gateway.

Once Terraform reads private_ip back, the gateway has to hold the
address it was asked for. private_ip forces a new gateway, and
CreateNatGateway ignored PrivateIpAddress, so a config that set it
would plan a replacement on every run. CreateNatGateway and
AWS::EC2::NatGateway now give the gateway's interface the requested
address, checked by the same rules as RunInstances: inside the subnet,
not reserved and not in use. A changed PrivateIpAddress replaces the
CloudFormation resource, as it is createOnly. A gateway has its primary
address only, so SecondaryAllocationIds, SecondaryPrivateIpAddresses
and SecondaryPrivateIpAddressCount are refused with
UnsupportedOperation instead of being dropped.

AWS::EC2::NatGateway also sets EniId, the gateway's primary interface,
which Fn::GetAtt resolved to the literal "Nat.EniId" although the
gateway has had a real interface since #5622.

Closes #6767

* fix(ec2): take no VPC network lease for a NAT gateway's requested address

DeleteNatGateway never returns a lease, so the one CreateNatGateway took
for its PrivateIpAddress kept the address from automatic allocation
after the gateway was gone. Check that the address is free the way
CreateNetworkInterface does, without the lease; automatic allocation
already steps around addresses a stored NAT gateway holds.

Ec2NatGatewayAddressIntegrationTest now sends its requests through the
shared Ec2Requests.ec2 instead of its own copy.
This commit is contained in:
Suja Shaj authored and GitHub committed 2026-10-31 05:32:32 -04:00
1 parent 2ad35bba1d
commit dcf8ec7430
13 files changed
+519 -244

No files matched your search

@@ -0,0 +1,93 @@
package com.floci.test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.AllocateAddressRequest;
import software.amazon.awssdk.services.ec2.model.AllocateAddressResponse;
import software.amazon.awssdk.services.ec2.model.AvailabilityMode;
import software.amazon.awssdk.services.ec2.model.ConnectivityType;
import software.amazon.awssdk.services.ec2.model.CreateNatGatewayRequest;
import software.amazon.awssdk.services.ec2.model.DeleteNatGatewayRequest;
import software.amazon.awssdk.services.ec2.model.DescribeNatGatewaysRequest;
import software.amazon.awssdk.services.ec2.model.DomainType;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
import software.amazon.awssdk.services.ec2.model.NatGateway;
import software.amazon.awssdk.services.ec2.model.NatGatewayAddress;
import software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
class Ec2NatGatewayTest {
@Test
@DisplayName("CreateNatGateway and DescribeNatGateways report a zonal gateway with its primary address")
void natGatewayIsZonalWithItsPrimaryAddress() {
// The Terraform AWS provider reads network_interface_id, private_ip and public_ip only from
// a gateway whose AvailabilityMode is zonal, so an unset mode leaves all three null.
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
String subnetId = TestFixtures.defaultVpcSubnets().get(0);
AllocateAddressResponse address = ec2.allocateAddress(AllocateAddressRequest.builder()
.domain(DomainType.VPC)
.build());
String natGatewayId = null;
try {
NatGateway created = ec2.createNatGateway(CreateNatGatewayRequest.builder()
.subnetId(subnetId)
.allocationId(address.allocationId())
.build())
.natGateway();
natGatewayId = created.natGatewayId();
assertThat(created.availabilityMode()).isEqualTo(AvailabilityMode.ZONAL);
NatGateway described = ec2.describeNatGateways(DescribeNatGatewaysRequest.builder()
.natGatewayIds(natGatewayId)
.build())
.natGateways().get(0);
assertThat(described.availabilityMode()).isEqualTo(AvailabilityMode.ZONAL);
assertThat(described.natGatewayAddresses()).hasSize(1);
NatGatewayAddress primary = described.natGatewayAddresses().get(0);
assertThat(primary.isPrimary()).isTrue();
assertThat(primary.networkInterfaceId()).startsWith("eni-");
assertThat(primary.privateIp()).isNotBlank();
assertThat(primary.publicIp()).isEqualTo(address.publicIp());
} finally {
if (natGatewayId != null) {
ec2.deleteNatGateway(DeleteNatGatewayRequest.builder().natGatewayId(natGatewayId).build());
}
ec2.releaseAddress(ReleaseAddressRequest.builder().allocationId(address.allocationId()).build());
}
}
}
@Test
@DisplayName("CreateNatGateway refuses secondary addresses rather than creating a gateway without them")
void secondaryAddressesAreRefused() {
// SecondaryAllocationId.N and SecondaryPrivateIpAddress.N are the list names the SDK sends.
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
String subnetId = TestFixtures.defaultVpcSubnets().get(0);
for (CreateNatGatewayRequest request : List.of(
CreateNatGatewayRequest.builder()
.subnetId(subnetId)
.allocationId("eipalloc-0123456789abcdef0")
.secondaryAllocationIds("eipalloc-0fedcba9876543210")
.build(),
CreateNatGatewayRequest.builder()
.subnetId(subnetId)
.connectivityType(ConnectivityType.PRIVATE)
.secondaryPrivateIpAddresses("172.31.0.200")
.build(),
CreateNatGatewayRequest.builder()
.subnetId(subnetId)
.connectivityType(ConnectivityType.PRIVATE)
.secondaryPrivateIpAddressCount(2)
.build())) {
Ec2Exception error = assertThrows(Ec2Exception.class, () -> ec2.createNatGateway(request));
assertThat(error.awsErrorDetails().errorCode()).isEqualTo("UnsupportedOperation");
}
}
}
}