fix(ec2): resolve a NAT gateway's network interface and mark its Elastic IP associated (#5622)

* fix(ec2): resolve a NAT gateway's network interface and mark its Elastic IP associated

CreateNatGateway gave each gateway address a networkInterfaceId of
"eni-" + randomHex(17), but nothing else knew about that interface:
DescribeNetworkInterfaces had no NAT arm, so the id DescribeNatGateways
publishes (aws_nat_gateway.network_interface_id) answered
InvalidNetworkInterfaceID.NotFound. The Elastic IP the gateway took was
also never marked associated, so DescribeAddresses showed a held
allocation as free.

The interface id is now derived from the gateway id, as endpoint ENIs
are, and DescribeNetworkInterfaces reports one requester-managed
nat_gateway interface per gateway address, built from the stored
address so the two reads cannot disagree. CreateNatGateway records the
association, interface and private address on the allocation (no
instanceId), and DeleteNatGateway clears them if the association is
still the gateway's.

* fix(ec2): refuse a NAT gateway on an occupied Elastic IP, keep its association, scope its ENI lookup to the account

CreateNatGateway rejects an allocation already held by an instance or another gateway
(Resource.AlreadyAssociated) under the lock that writes the association. DisassociateAddress
rejects an association owned by a NAT gateway. The NAT arm of DescribeNetworkInterfaces reads
gateways and subnets for the account asked about.

* fix(ec2): lock NAT gateway EIP release, treat a legacy gateway's EIP as held

DeleteNatGateway releases the Elastic IP under the lock CreateNatGateway
takes. CreateNatGateway also refuses an allocation any live gateway
references, covering gateways persisted before Address records were
marked. The CloudFormation networking test gives the replacement NAT
gateway a new EIP: replacement creates before it deletes, and AWS's
CreateNatGateway refuses an EIP still associated elsewhere.

* fix(ec2): AssociateAddress refuses a NAT gateway's Elastic IP; legacy gateways hold theirs

AssociateAddress checks for a gateway holding the allocation and writes
under the allocation lock CreateNatGateway and DeleteNatGateway take, so
an instance cannot take over an address the gateway still claims. The
gateway scan behind that check, and CreateNatGateway's, now includes
unmigrated pre-account-isolation gateway records for the default
account, never another account's.

* fix(ec2): scope NAT gateway ownership checks to the caller's records

callerNatGateways scanned every account's gateways before filtering. Read
the caller's own records, plus unprefixed legacy records only for the
default account.

* fix(ec2): create a NAT gateway failed on an Elastic IP already in use

CreateNatGateway with an allocation another gateway or an instance holds
no longer answers 400. As the NatGateway shape documents, the gateway is
created in state failed with FailureCode Resource.AlreadyAssociated and
FailureMessage "Elastic IP address <alloc> is already associated". It
gets no interface, claims nothing, and does not count as a holder.
AWS::EC2::NatGateway fails the resource and deletes the failed gateway.

ReleaseAddress on an allocation a NAT gateway holds, legacy gateways
included, answers InvalidIPAddress.InUse. DisassociateAddress and
ReleaseAddress run under privateIpAllocationLock. The NAT interface lookup
reads legacy unprefixed gateways and subnets for the default account. It
also loads the interface's tags, and DeleteNatGateway deletes them.
endpointEniId and natEniId become one helper, eniIdFromSeed.

* test(cloudformation): say why the replacement template keeps NatEip

The replacement template keeps NatEip for two reasons. On AWS a gateway
given an Elastic IP the displaced gateway still holds is created failed.
And Floci update cleanup deletes removed resources before the old copies
of replaced ones, so dropping the EIP while replacing its gateway would
try to release it while it is still held.

* fix(ec2): a failed NAT gateway does not pin its subnet or VPC

#5503 made DeleteSubnet and DeleteVpc refuse while a NAT gateway that is
not deleted sits in the subnet. A failed gateway was never created and
holds no interface, so it no longer counts, matching the Elastic IP
holder check.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-15 13:53:49 -04:00
1 parent d3fce14256
commit 1e423420ed
10 files changed
+806 -51

No files matched your search

@@ -573,6 +573,11 @@ public class Ec2NetworkCfnProvisioner implements CfnResourceProvisioner {
&& connectivityType.equalsIgnoreCase(existing.getConnectivityType() == null ? "public" : existing.getConnectivityType());
NatGateway nat = reuse ? existing
: ec2Service.createNatGateway(ctx.region(), subnetId, allocationId, connectivityType, tagList(props, ctx));
if ("failed".equals(nat.getState())) {
// The resource fails and its failed gateway is not left behind.
ec2Service.deleteNatGateway(ctx.region(), nat.getNatGatewayId());
throw new AwsException(nat.getFailureCode(), nat.getFailureMessage(), 400);
}
if (reuse) {
CfnRollback.guarded(r, ctx, false, () -> { }, () -> undoInPlace(r), () -> applyTags(r, true, nat.getNatGatewayId(), existing.getTags(), props, ctx));
}
@@ -5117,6 +5117,8 @@ public class Ec2QueryHandler {
.elem("subnetId", natGateway.getSubnetId())
.elem("vpcId", natGateway.getVpcId())
.elem("state", natGateway.getState())
.elem("failureCode", natGateway.getFailureCode())
.elem("failureMessage", natGateway.getFailureMessage())
.elem("connectivityType", natGateway.getConnectivityType());
if (natGateway.getCreateTime() != null) {
xml.elem("createTime", ISO_FMT.format(natGateway.getCreateTime()));
@@ -4790,7 +4790,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
* it the VPC: an instance, a NAT gateway, a standalone network interface, or an interface or
* Gateway Load Balancer endpoint. A {@code shutting-down} instance still holds its interface and
* a {@code deleting} NAT gateway still holds its address, so only {@code terminated} and
* {@code deleted} release the subnet; a standalone interface is removed from the store on
* {@code deleted} release the subnet. A {@code failed} NAT gateway was never created and holds
* no interface, so it does not count either; a standalone interface is removed from the store on
* delete, so its presence alone counts. AWS answers {@code DependencyViolation} until then.
*/
private void requireNoSubnetResidents(String region, Set<String> subnetIds, String message) {
@@ -4804,7 +4805,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|| !"terminated".equals(instance.getState().getName())))
|| natGateways.scan(k -> k.startsWith(regionPrefix)).stream()
.anyMatch(natGateway -> subnetIds.contains(natGateway.getSubnetId())
&& !"deleted".equals(natGateway.getState()))
&& !"deleted".equals(natGateway.getState())
&& !"failed".equals(natGateway.getState()))
|| networkInterfaces.scan(k -> k.startsWith(regionPrefix)).stream()
.anyMatch(ni -> subnetIds.contains(ni.getSubnetId()))
|| vpcEndpoints.scan(k -> k.startsWith(regionPrefix)).stream()
@@ -5340,7 +5342,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
* <p>Split out so that everything reporting an endpoint's interfaces -- the ids on
* the wire, the objects flow-log attribution reads -- comes from one place and
* cannot disagree. The alternative, deriving ids independently from the same
* {@link #endpointEniId}, looks equivalent and is not: this method skips a subnet
* {@link #eniIdFromSeed}, looks equivalent and is not: this method skips a subnet
* whose record has gone, and a second derivation that forgot to would report an
* interface the first one denies exists. Congruence by construction beats congruence
* by inspection, and the two had in fact already diverged.
@@ -5372,7 +5374,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
continue;
}
NetworkInterface ni = new NetworkInterface();
ni.setNetworkInterfaceId(endpointEniId(endpoint.getVpcEndpointId(), subnetId));
ni.setNetworkInterfaceId(eniIdFromSeed(endpoint.getVpcEndpointId() + "|" + subnetId));
ni.setSubnetId(subnetId);
ni.setVpcId(endpoint.getVpcId());
ni.setAvailabilityZone(subnet.getAvailabilityZone());
@@ -5435,9 +5437,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
return result;
}
private static String endpointEniId(String endpointId, String subnetId) {
String hex = java.util.UUID.nameUUIDFromBytes(
(endpointId + "|" + subnetId).getBytes(StandardCharsets.UTF_8))
/** A stable interface id derived from {@code seed}, for interfaces Floci projects rather than stores. */
private static String eniIdFromSeed(String seed) {
String hex = UUID.nameUUIDFromBytes(seed.getBytes(StandardCharsets.UTF_8))
.toString().replace("-", "");
return "eni-" + hex.substring(0, 17);
}
@@ -5453,7 +5455,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
* <p>Mapped over {@link #endpointNetworkInterfacesOf(VpcEndpoint)} rather than derived
* separately, so the ids on the wire come BY CONSTRUCTION from the same derivation
* flow-log attribution uses, and no endpoint can be described two ways. An earlier
* version of this method called {@link #endpointEniId} itself and looked equivalent;
* version of this method called {@link #eniIdFromSeed} itself and looked equivalent;
* it was not, because it lacked that method's skip of a subnet whose record has gone,
* and the two were measured reporting different sets after a subnet was deleted out
* from under a live endpoint.
@@ -9365,7 +9367,36 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
natGateway.setCreateTime(Instant.now());
natGateway.setRegion(region);
synchronized (privateIpAllocationLock) {
natGateway.getNatGatewayAddresses().add(natGatewayAddress(region, subnetId, allocationId));
// Checked under the same lock that writes the association below, so two gateways
// cannot both see the allocation free. As on AWS, an allocation already held by an
// instance or another gateway does not fail the call: the gateway is created in state
// failed, with no interface, and the allocation is left with its holder.
if (isSet(allocationId)) {
Address held = getRequiredAddress(region, allocationId);
// The gateway scan covers gateways persisted before the Address record was marked.
if (isSet(held.getAssociationId()) || isSet(held.getInstanceId())
|| isHeldByNatGateway(region, allocationId)) {
natGateway.setState("failed");
natGateway.setFailureCode("Resource.AlreadyAssociated");
natGateway.setFailureMessage("Elastic IP address " + allocationId + " is already associated");
}
}
if (!"failed".equals(natGateway.getState())) {
NatGatewayAddress gatewayAddress =
natGatewayAddress(region, natGateway.getNatGatewayId(), subnetId, allocationId);
natGateway.getNatGatewayAddresses().add(gatewayAddress);
// The Elastic IP is now held by the gateway's interface, so DescribeAddresses has to
// say so: an allocation that looks free is one Terraform and the console would offer
// to reuse. No instanceId, a NAT gateway is not an instance.
if (isSet(allocationId)) {
addresses.get(key(region, allocationId)).ifPresent(eip -> {
eip.setAssociationId(gatewayAddress.getAssociationId());
eip.setNetworkInterfaceId(gatewayAddress.getNetworkInterfaceId());
eip.setPrivateIpAddress(gatewayAddress.getPrivateIp());
addresses.put(key(region, allocationId), eip);
});
}
}
if (natGatewayTags != null && !natGatewayTags.isEmpty()) {
natGateway.setTags(new ArrayList<>(natGatewayTags));
tags.put(natGateway.getNatGatewayId(), new ArrayList<>(natGatewayTags));
@@ -9382,9 +9413,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
* network_interface_id), and Gruntwork's VPC modules re-export nat_gateway_public_ips, so an
* address carrying only an allocation id propagates empty values into dependent modules.
*/
private NatGatewayAddress natGatewayAddress(String region, String subnetId, String allocationId) {
private NatGatewayAddress natGatewayAddress(String region, String natGatewayId, String subnetId,
String allocationId) {
NatGatewayAddress address = new NatGatewayAddress();
address.setNetworkInterfaceId("eni-" + randomHex(17));
address.setNetworkInterfaceId(eniIdFromSeed("nat|" + natGatewayId));
address.setPrivateIp(assignPrivateIp(region, subnetId));
if (isSet(allocationId)) {
address.setAllocationId(allocationId);
@@ -9397,6 +9429,51 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
return address;
}
/**
* The interfaces a NAT gateway owns, one per stored address, built from that address so the id
* DescribeNatGateways publishes is the id DescribeNetworkInterfaces resolves. Empty when the
* gateway's subnet record is gone, as for an endpoint.
*/
private List<NetworkInterface> natNetworkInterfacesOf(NatGateway natGateway, String ownerId) {
String subnetKey = key(natGateway.getRegion(), natGateway.getSubnetId());
Subnet subnet = (subnets instanceof AccountAwareStorageBackend<Subnet> aware
? aware.getForAccount(ownerId, subnetKey).or(() -> aware.getLegacyForAccount(ownerId, subnetKey))
: subnets.get(subnetKey)).orElse(null);
if (subnet == null) {
return List.of();
}
List<NetworkInterface> result = new ArrayList<>();
for (NatGatewayAddress address : natGateway.getNatGatewayAddresses()) {
if (address.getNetworkInterfaceId() == null) {
continue;
}
NetworkInterface ni = new NetworkInterface();
ni.setNetworkInterfaceId(address.getNetworkInterfaceId());
ni.setSubnetId(natGateway.getSubnetId());
ni.setVpcId(natGateway.getVpcId());
ni.setAvailabilityZone(subnet.getAvailabilityZone());
ni.setDescription("Interface for NAT Gateway " + natGateway.getNatGatewayId());
ni.setInterfaceType("nat_gateway");
ni.setRequesterManaged(true);
ni.setOwnerId(ownerId);
ni.setPrivateIpAddress(address.getPrivateIp());
NetworkInterfacePrivateIpAddress primaryIp = new NetworkInterfacePrivateIpAddress();
primaryIp.setPrivateIpAddress(address.getPrivateIp());
primaryIp.setPrimary(true);
if (address.getPublicIp() != null) {
NetworkInterfaceAssociation assoc = new NetworkInterfaceAssociation();
assoc.setPublicIp(address.getPublicIp());
assoc.setAllocationId(address.getAllocationId());
assoc.setAssociationId(address.getAssociationId());
assoc.setIpOwnerId(ownerId);
primaryIp.setAssociation(assoc);
}
ni.getPrivateIpAddresses().add(primaryIp);
result.add(ni);
}
return result;
}
public List<NatGateway> describeNatGateways(String region, List<String> natGatewayIds,
Map<String, List<String>> filters) {
ensureDefaultResources(region);
@@ -9416,9 +9493,28 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
public NatGateway deleteNatGateway(String region, String natGatewayId) {
ensureDefaultResources(region);
NatGateway natGateway = getRequiredNatGateway(region, natGatewayId);
natGateway.setState("deleted");
natGateways.delete(key(region, natGatewayId));
tags.delete(natGatewayId);
// Hand the Elastic IP back, but only if the association is still the gateway's own. Under
// the lock createNatGateway takes, so a concurrent create cannot have its association wiped.
synchronized (privateIpAllocationLock) {
natGateway.setState("deleted");
for (NatGatewayAddress gatewayAddress : natGateway.getNatGatewayAddresses()) {
if (!isSet(gatewayAddress.getAllocationId())) {
continue;
}
addresses.get(key(region, gatewayAddress.getAllocationId()))
.filter(eip -> gatewayAddress.getAssociationId() != null
&& gatewayAddress.getAssociationId().equals(eip.getAssociationId()))
.ifPresent(eip -> {
eip.setAssociationId(null);
eip.setNetworkInterfaceId(null);
eip.setPrivateIpAddress(null);
addresses.put(key(region, eip.getAllocationId()), eip);
});
}
natGateways.delete(key(region, natGatewayId));
tags.delete(natGatewayId);
natGateway.getNatGatewayAddresses().forEach(a -> tags.delete(a.getNetworkInterfaceId()));
}
return natGateway;
}
@@ -9447,16 +9543,26 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
public Address associateAddress(String region, String allocationId, String instanceId) {
ensureDefaultResources(region);
Address addr = getRequiredAddress(region, allocationId);
// Under the lock createNatGateway and deleteNatGateway take, so the gateway check and the
// write cannot interleave with a gateway claiming or releasing the same allocation.
synchronized (privateIpAllocationLock) {
Address addr = getRequiredAddress(region, allocationId);
// A NAT gateway holds its Elastic IP until DeleteNatGateway; reassociation would leave
// the gateway claiming an address an instance now holds.
if (isHeldByNatGateway(region, allocationId)) {
throw new AwsException("Resource.AlreadyAssociated",
"Elastic IP address " + allocationId + " is already associated with a NAT gateway.", 400);
}
if (addr.getAssociationId() != null) {
disassociateAddress(region, addr.getAssociationId());
if (addr.getAssociationId() != null) {
disassociateAddress(region, addr.getAssociationId());
}
addr.setInstanceId(instanceId);
addr.setAssociationId("eipassoc-" + randomHex(17));
pointAddressAtInstance(addr, region, instanceId);
addresses.put(key(region, allocationId), addr);
return addr;
}
addr.setInstanceId(instanceId);
addr.setAssociationId("eipassoc-" + randomHex(17));
pointAddressAtInstance(addr, region, instanceId);
addresses.put(key(region, allocationId), addr);
return addr;
}
/**
@@ -9546,38 +9652,93 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
public void disassociateAddress(String region, String associationId) {
ensureDefaultResources(region);
for (Address addr : addresses.scan(k -> true)) {
if (addr.getRegion().equals(region) && associationId.equals(addr.getAssociationId())) {
addr.setInstanceId(null);
addr.setAssociationId(null);
updateStoredAddressAssociation(addr);
addr.setNetworkInterfaceId(null);
addr.setPrivateIpAddress(null);
// Hand the allocation back its allocated address: with no instance behind it,
// there is nothing reachable left for it to stand for.
if (addr.getAllocatedPublicIp() != null) {
addr.setPublicIp(addr.getAllocatedPublicIp());
// Under the lock NAT create and delete take, so the gateway check and the clear see the
// same association.
synchronized (privateIpAllocationLock) {
for (Address addr : addresses.scan(k -> true)) {
if (addr.getRegion().equals(region) && associationId.equals(addr.getAssociationId())) {
// A NAT gateway holds its Elastic IP for its whole life and is released by
// DeleteNatGateway; clearing only the allocation would leave the gateway and its
// interface still claiming the address.
if (isNatGatewayAssociation(region, associationId)) {
throw new AwsException("InvalidParameterValue", "The association " + associationId
+ " belongs to a NAT gateway and cannot be disassociated; delete the NAT gateway.",
400);
}
addr.setInstanceId(null);
addr.setAssociationId(null);
updateStoredAddressAssociation(addr);
addr.setNetworkInterfaceId(null);
addr.setPrivateIpAddress(null);
// Hand the allocation back its allocated address: with no instance behind it,
// there is nothing reachable left for it to stand for.
if (addr.getAllocatedPublicIp() != null) {
addr.setPublicIp(addr.getAllocatedPublicIp());
}
addresses.put(key(region, addr.getAllocationId()), addr);
return;
}
addresses.put(key(region, addr.getAllocationId()), addr);
return;
}
}
}
/**
* The caller's gateways, including unmigrated pre-account-isolation records when the caller is
* the default account (they belong to it); never another account's.
*/
private List<NatGateway> callerNatGateways() {
return natGatewaysOf(callerAccountId());
}
private List<NatGateway> natGatewaysOf(String account) {
if (natGateways instanceof AccountAwareStorageBackend<NatGateway> aware) {
List<NatGateway> own = aware.scanForAccount(account, k -> true);
if (!account.equals(defaultAccountId)) {
return own;
}
List<NatGateway> all = new ArrayList<>(own);
all.addAll(aware.scanUnscopedLegacy(g -> true));
return all;
}
return natGateways.scan(k -> true);
}
private boolean isHeldByNatGateway(String region, String allocationId) {
return callerNatGateways().stream()
.filter(g -> region.equals(g.getRegion())
&& !"deleted".equals(g.getState()) && !"failed".equals(g.getState()))
.anyMatch(g -> allocationId.equals(g.getAllocationId())
|| g.getNatGatewayAddresses().stream().anyMatch(a -> allocationId.equals(a.getAllocationId())));
}
private boolean isNatGatewayAssociation(String region, String associationId) {
return callerNatGateways().stream()
.filter(g -> region.equals(g.getRegion()))
.flatMap(g -> g.getNatGatewayAddresses().stream())
.anyMatch(a -> associationId.equals(a.getAssociationId()));
}
public void releaseAddress(String region, String allocationId) {
ensureDefaultResources(region);
Address address = getRequiredAddress(region, allocationId);
if (address.getAssociationId() != null) {
NetworkInterface ni = networkInterfaces.get(key(region, address.getNetworkInterfaceId())).orElse(null);
String vpcId = ni != null ? ni.getVpcId()
: getRequiredInstance(region, address.getInstanceId()).getVpcId();
if (!getRequiredVpc(region, vpcId).isDefault()) {
synchronized (privateIpAllocationLock) {
Address address = getRequiredAddress(region, allocationId);
// A NAT gateway's Elastic IP is released by DeleteNatGateway, never out from under it.
if (isHeldByNatGateway(region, allocationId)) {
throw new AwsException("InvalidIPAddress.InUse",
"Address " + address.getPublicIp() + " is in use.", 400);
}
disassociateAddress(region, address.getAssociationId());
if (address.getAssociationId() != null) {
NetworkInterface ni = networkInterfaces.get(key(region, address.getNetworkInterfaceId())).orElse(null);
String vpcId = ni != null ? ni.getVpcId()
: getRequiredInstance(region, address.getInstanceId()).getVpcId();
if (!getRequiredVpc(region, vpcId).isDefault()) {
throw new AwsException("InvalidIPAddress.InUse",
"Address " + address.getPublicIp() + " is in use.", 400);
}
disassociateAddress(region, address.getAssociationId());
}
addresses.delete(key(region, allocationId));
}
addresses.delete(key(region, allocationId));
}
private void releaseInterfaceAddresses(String region, String networkInterfaceId, boolean deleted) {
@@ -10872,6 +11033,31 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
result.add(endpointNi);
}
// The ENI a NAT gateway owns. DescribeNatGateways publishes its id (aws_nat_gateway's
// network_interface_id), so it has to resolve here too. Unlike the endpoint arm above this
// one honours the explicit account: gateways and their subnets are read for safeAccount.
for (NatGateway natGateway : natGatewaysOf(safeAccount)) {
if (!region.equals(natGateway.getRegion())) {
continue;
}
for (NetworkInterface natNi : natNetworkInterfacesOf(natGateway, safeAccount)) {
String natEniId = natNi.getNetworkInterfaceId();
if (foundIds.contains(natEniId)) {
continue;
}
if (!networkInterfaceIds.isEmpty() && !networkInterfaceIds.contains(natEniId)) {
continue;
}
foundIds.add(natEniId);
natNi.getTagSet().addAll((tags instanceof AccountAwareStorageBackend<List<Tag>> aware
? aware.getForAccount(safeAccount, natEniId) : tags.get(natEniId)).orElse(List.of()));
if (!matchesFilters(natNi, filters, region)) {
continue;
}
result.add(natNi);
}
}
// Phase 6: validate requested IDs exist
for (String id : networkInterfaceIds) {
if (!foundIds.contains(id)) {
@@ -17,6 +17,8 @@ public class NatGateway {
private String allocationId;
private String state = "available";
private String connectivityType = "public";
private String failureCode;
private String failureMessage;
private Instant createTime;
private String region;
private List<NatGatewayAddress> natGatewayAddresses = new ArrayList<>();
@@ -39,6 +41,12 @@ public class NatGateway {
public String getState() { return state; }
public void setState(String state) { this.state = state; }
public String getFailureCode() { return failureCode; }
public void setFailureCode(String failureCode) { this.failureCode = failureCode; }
public String getFailureMessage() { return failureMessage; }
public void setFailureMessage(String failureMessage) { this.failureMessage = failureMessage; }
public String getConnectivityType() { return connectivityType; }
public void setConnectivityType(String connectivityType) { this.connectivityType = connectivityType; }