fix(lambda): hold the subnets a function's VpcConfig names (#8609)

On AWS a function's Hyperplane network interfaces sit in the subnets
its VpcConfig names, and EC2 refuses DeleteSubnet, and so DeleteVpc,
while one is there. Floci's Lambda never told EC2 a function used a
subnet, so DeleteSubnet succeeded under a VPC-configured function and
left it naming a subnet that was gone.

- LambdaService is a VpcDependencyProvider: a function of the caller's
  account, or a published version of one, holds each subnet its
  VpcConfig names and the VPC it is configured in
  (LambdaFunctionStore.listWithVersionsForAccount, which also reads a
  record stored before accounts were kept apart, unprefixed until it
  is next read).
- CreateFunction and UpdateFunctionConfiguration save a function that
  names subnets under EC2's subnet lock, so a DeleteSubnet either sees
  it or runs before it is stored. A VpcConfig the request set is
  resolved again under the lock, so a subnet deleted since the request
  was validated is unknown, as one never created is, and the function
  infers no VPC from it.

ECS awsvpc tasks already hold their subnet through the network
interface each task is given, which DeleteSubnet counts.

LambdaVpcResourceValidationIntegrationTest has DeleteSubnet refused
under a function, then under its published version once $LATEST moves
to another subnet, and allowed once the version is deleted, and a
subnet deleted while a function is created leaves it no inferred VPC.
LambdaFunctionStoreAccountScopeTest covers the unprefixed record.
ec2.md and lambda.md say so.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 15:04:43 -04:00
1 parent 3d6f59c064
commit 94fcf5cbf3
6 files changed
+159 -6

No files matched your search

@@ -152,6 +152,25 @@ public class LambdaFunctionStore implements Resettable {
return backend.scan(key -> key.startsWith(prefix) && key.endsWith("::$LATEST"));
}
/**
* Lists an account's functions and their published versions without relying on request context, including one
* stored before accounts were kept apart, which stays unprefixed until it is next read.
*/
public List<LambdaFunction> listWithVersionsForAccount(String accountId, String region) {
String prefix = "lambda::" + region + "::";
if (backend instanceof AccountAwareStorageBackend<LambdaFunction> aware) {
List<LambdaFunction> functions = new ArrayList<>(
aware.scanForAccount(accountId, key -> key.startsWith(prefix)));
aware.scanRaw(rawKey -> rawKey.startsWith(prefix)).stream()
.filter(function -> belongsToAccount(function, accountId))
.forEach(functions::add);
return functions;
}
return backend.scan(key -> key.startsWith(prefix)).stream()
.filter(function -> belongsToAccount(function, accountId))
.toList();
}
/** Lists the caller's {@code $LATEST} functions without relying on request context. */
public List<LambdaFunction> listForAccount(String accountId, String region) {
String prefix = "lambda::" + region + "::";
@@ -23,6 +23,7 @@ import io.floci.aws.core.resource.SupportedResourceType;
import io.floci.aws.core.storage.StorageBackedMap;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.ec2.Ec2Service;
import io.floci.aws.services.ec2.VpcDependencyProvider;
import io.floci.aws.services.ec2.model.SecurityGroup;
import io.floci.aws.services.ec2.model.Subnet;
import io.floci.aws.services.iam.AccessDeniedMessages.PolicyType;
@@ -93,7 +94,7 @@ import java.util.stream.Collectors;
* Business logic for Lambda function management and invocation.
*/
@ApplicationScoped
public class LambdaService implements ResourceProvider {
public class LambdaService implements ResourceProvider, VpcDependencyProvider {
private static final Logger LOG = Logger.getLogger(LambdaService.class);
@@ -695,7 +696,7 @@ public class LambdaService implements ResourceProvider {
}
}
functionStore.save(region, fn);
saveHoldingSubnets(region, fn, vpcConfig != null);
LOG.infov("Created Lambda function: {0} in region {1}", functionName, region);
if (Boolean.TRUE.equals(request.get("Publish"))) {
return publishVersion(region, functionName, null);
@@ -1220,7 +1221,7 @@ public class LambdaService implements ResourceProvider {
// Drain warm containers so the next invocation picks up the new configuration
warmPool.drainEnvironment(fn);
functionStore.save(region, fn);
saveHoldingSubnets(region, fn, requestedVpcConfigUpdate != null);
// LastUpdateStatus reports the last update of either kind, so this one replaces a failed sync's.
failedReactiveSyncs.remove(fn.getFunctionArn());
LOG.infov("Updated configuration for function: {0}", functionName);
@@ -2811,6 +2812,53 @@ public class LambdaService implements ResourceProvider {
}
}
// ─── VPC dependencies ────────────────────────────────────────────────────────
/**
* Whether a function of the caller's account, or a published version of one, names the subnet in its VpcConfig:
* Lambda creates a Hyperplane network interface in each subnet a function's VpcConfig names, and EC2 refuses
* DeleteSubnet while one is there.
*/
@Override
public boolean hasSubnetDependency(String region, String subnetId) {
return functionStore.listWithVersionsForAccount(regionResolver.getAccountId(), region).stream()
.anyMatch(fn -> namesSubnet(fn, subnetId));
}
/** Whether a function of the caller's account, or a version of one, is configured in the VPC. */
@Override
public boolean hasVpcDependency(String region, String vpcId) {
return functionStore.listWithVersionsForAccount(regionResolver.getAccountId(), region).stream()
.anyMatch(fn -> vpcId.equals(fn.getVpcId()));
}
private static boolean namesSubnet(LambdaFunction fn, String subnetId) {
Object subnets = fn.getVpcConfig() == null ? null : fn.getVpcConfig().get("SubnetIds");
return subnets instanceof List<?> list && list.contains(subnetId);
}
/**
* Saves a function, under EC2's subnet lock when its VpcConfig names subnets, so a DeleteSubnet or DeleteVpc
* either sees it or runs before it is stored, never between. A VpcConfig this request set is resolved again under
* the lock: a subnet deleted since the request was validated is then unknown, as one never created is, and the
* function infers no VPC from it.
*
* @param vpcConfigSet whether this request set the function's VpcConfig
*/
private void saveHoldingSubnets(String region, LambdaFunction fn, boolean vpcConfigSet) {
Object subnets = fn.getVpcConfig() == null ? null : fn.getVpcConfig().get("SubnetIds");
if (ec2Service != null && subnets instanceof List<?> list && !list.isEmpty()) {
ec2Service.runWithSubnetReferencesHeld(region, () -> {
if (vpcConfigSet) {
fn.setVpcId(resolveVpcId(region, fn.getVpcConfig()));
}
functionStore.save(region, fn);
});
} else {
functionStore.save(region, fn);
}
}
/** Resolve the common VPC only after every requested subnet and security group is validated. */
private String resolveVpcId(String region, Map<String, Object> vpcConfig) {
List<String> subnetIds = vpcResourceIds(vpcConfig, "SubnetIds");