fix(mwaa): put an environment's Airflow container on its VPC's network (#8470)

An environment's NetworkConfiguration names the subnets Amazon MWAA
runs its workers in, but the Airflow container stayed on Floci's own
network, so a DAG could not reach an EC2 instance, or anything else in
the VPC, at the private address EC2 reports.

Before the container starts, it now joins its VPC's Docker network at
an address leased from the first subnet, as EC2 instances and
VPC-configured Lambda functions do, and keeps the network it was
created on, where its metadata database is. The address goes back once
the container is removed, on delete or after a failed start. A subnet
EC2 does not know, or VPC networks switched off, leaves it as before.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 13:47:59 -04:00
1 parent f71476037a
commit f59990e270
5 files changed
+398 -5

No files matched your search

@@ -6,6 +6,7 @@ import com.github.dockerjava.api.model.ContainerNetwork;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.Base64Url;
import io.floci.aws.core.common.RequestScopes;
import io.floci.aws.core.common.docker.ContainerBuilder;
import io.floci.aws.core.common.docker.ContainerDetector;
import io.floci.aws.core.common.docker.ContainerExec;
@@ -15,7 +16,11 @@ import io.floci.aws.core.common.docker.ContainerSpec;
import io.floci.aws.core.common.docker.ContainerStorageHelper;
import io.floci.aws.core.common.docker.LaunchedContainerAwsEnv;
import io.floci.aws.core.common.docker.RetryingTarCopier;
import io.floci.aws.services.ec2.Ec2Service;
import io.floci.aws.services.ec2.model.Subnet;
import io.floci.aws.services.ec2.net.Cidr4;
import io.floci.aws.services.mwaa.model.Environment;
import io.floci.aws.services.mwaa.model.NetworkConfiguration;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -29,11 +34,13 @@ import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -91,18 +98,23 @@ public class MwaaEnvironmentManager {
private final ContainerDetector containerDetector;
private final EmulatorConfig config;
private final LaunchedContainerAwsEnv awsEnv;
private final Ec2Service ec2Service;
/** The VPC address each Airflow container holds, by container id, returned once the container is gone. */
private final Map<String, VpcLease> vpcLeases = new ConcurrentHashMap<>();
@Inject
public MwaaEnvironmentManager(ContainerBuilder containerBuilder,
ContainerLifecycleManager lifecycleManager,
ContainerDetector containerDetector,
EmulatorConfig config,
LaunchedContainerAwsEnv awsEnv) {
LaunchedContainerAwsEnv awsEnv,
Ec2Service ec2Service) {
this.containerBuilder = containerBuilder;
this.lifecycleManager = lifecycleManager;
this.containerDetector = containerDetector;
this.config = config;
this.awsEnv = awsEnv;
this.ec2Service = ec2Service;
}
/**
@@ -220,6 +232,7 @@ public class MwaaEnvironmentManager {
String containerId = lifecycleManager.create(spec);
ContainerInfo info;
try {
joinVpcNetwork(environment, containerId);
// Real MWAA documents startup scripts using "sudo yum install ..." as the baseline way
// to install OS packages — sudo is assumed to just work for the airflow user. The stock
// apache/airflow image ships sudo but requires a password, so without this grant every
@@ -252,6 +265,7 @@ public class MwaaEnvironmentManager {
info = lifecycleManager.startCreated(containerId, spec);
} catch (Exception e) {
lifecycleManager.removeIfExists(containerId);
releaseVpcLeaseIfGone(containerId);
throw e;
}
environment.setAirflowContainerId(info.containerId());
@@ -365,6 +379,7 @@ public class MwaaEnvironmentManager {
String name = environment.getName();
if (environment.getAirflowContainerId() != null) {
lifecycleManager.stopAndRemove(environment.getAirflowContainerId(), null);
releaseVpcLeaseIfGone(environment.getAirflowContainerId());
}
if (environment.getDbContainerId() != null) {
lifecycleManager.stopAndRemove(environment.getDbContainerId(), null);
@@ -376,6 +391,133 @@ public class MwaaEnvironmentManager {
LOG.infov("Stopped MWAA containers for environment {0}", name);
}
/**
* Puts the Airflow container on the VPC network of the environment's {@code NetworkConfiguration}, at an
* address leased from its first subnet, before the container starts: Amazon MWAA runs the environment's
* workers in those subnets, so its DAGs reach what the VPC holds (an EC2 instance, a database) by private
* address. The container keeps the network it was created on, which holds its metadata database. Without
* Docker-backed VPC networks, or for subnets EC2 does not know, it stays there alone. The subnet is checked and
* the lease recorded while EC2 holds its subnet references, so a DeleteSubnet cannot pass its dependency check
* between the two ({@link #holdsSubnet}).
*/
void joinVpcNetwork(Environment environment, String containerId) {
String subnetId = firstSubnet(environment);
if (ec2Service == null || subnetId == null) {
return;
}
String accountId = environmentAccount(environment);
String region = regionOf(environment);
// Provisioning can run outside the CreateEnvironment request, so the environment's account is named.
RequestScopes.runAs(accountId, region, () -> ec2Service.runWithSubnetReferencesHeld(region, () -> {
String vpcId = vpcOf(region, subnetId);
if (vpcId == null) {
return;
}
Optional<String> address = ec2Service.allocateVpcPrivateIp(region, subnetId);
if (address.isEmpty()) {
return;
}
if (ec2Service.attachContainerToVpc(region, vpcId, subnetId, containerId, address.get()).isPresent()) {
vpcLeases.put(containerId, new VpcLease(accountId, region, vpcId, subnetId, address.get()));
LOG.infov("MWAA environment {0} joined VPC {1} at {2}", environment.getName(), vpcId,
address.get());
} else {
ec2Service.releaseVpcPrivateIp(region, subnetId, address.get());
}
}));
}
/**
* Re-claims the VPC address an environment's Airflow container still holds after Floci restarts, when it was
* kept running ({@code keep-running-on-shutdown}): the address leases are in memory, so without this EC2 could
* hand the same address to an instance and Docker would refuse it. The container is found by its name.
*/
void reclaimVpcAddress(Environment environment) {
String subnetId = firstSubnet(environment);
if (ec2Service == null || subnetId == null) {
return;
}
String containerId;
Collection<ContainerNetwork> networks;
try {
InspectContainerResponse inspect = lifecycleManager.getDockerClient()
.inspectContainerCmd(airflowContainerName(config, environment)).exec();
containerId = inspect.getId();
networks = inspect.getNetworkSettings() == null || inspect.getNetworkSettings().getNetworks() == null
? List.of() : inspect.getNetworkSettings().getNetworks().values();
} catch (NotFoundException expected) {
// The container did not outlive the restart, so there is no address to re-claim.
return;
}
String accountId = environmentAccount(environment);
String region = regionOf(environment);
RequestScopes.runAs(accountId, region, () -> {
List<Subnet> subnets = ec2Service.describeSubnets(region, List.of(subnetId), Map.of());
Optional<Cidr4> cidr = subnets.size() == 1 ? Cidr4.parse(subnets.getFirst().getCidrBlock())
: Optional.empty();
if (cidr.isEmpty()) {
return;
}
for (ContainerNetwork network : networks) {
Optional<Cidr4> address = Cidr4.parse(network.getIpAddress() + "/32");
if (address.isPresent() && cidr.get().containsAddress(address.get().network())
&& ec2Service.reserveVpcPrivateIp(region, subnetId, network.getIpAddress())) {
vpcLeases.put(containerId, new VpcLease(accountId, region, subnets.getFirst().getVpcId(),
subnetId, network.getIpAddress()));
LOG.infov("MWAA environment {0} re-claimed its VPC address {1}", environment.getName(),
network.getIpAddress());
return;
}
}
});
}
/** Whether a live Airflow container of the account holds an address in the subnet. */
boolean holdsSubnet(String accountId, String region, String subnetId) {
return vpcLeases.values().stream().anyMatch(lease -> lease.accountId().equals(accountId)
&& lease.region().equals(region) && lease.subnetId().equals(subnetId));
}
/** Whether a live Airflow container of the account is on the VPC's network. */
boolean holdsVpc(String accountId, String region, String vpcId) {
return vpcLeases.values().stream().anyMatch(lease -> lease.accountId().equals(accountId)
&& lease.region().equals(region) && lease.vpcId().equals(vpcId));
}
private static String firstSubnet(Environment environment) {
NetworkConfiguration network = environment.getNetworkConfiguration();
List<String> subnetIds = network == null ? null : network.getSubnetIds();
return subnetIds == null || subnetIds.isEmpty() ? null : subnetIds.getFirst();
}
private String vpcOf(String region, String subnetId) {
List<Subnet> subnets = ec2Service.describeSubnets(region, List.of(subnetId), Map.of());
return subnets.size() == 1 ? subnets.getFirst().getVpcId() : null;
}
/**
* Gives the container's VPC address back once Docker no longer has the container. A removal that failed (it
* logs and carries on) leaves the address held, so the lease is kept rather than offered to someone else.
*/
private void releaseVpcLeaseIfGone(String containerId) {
VpcLease lease = vpcLeases.get(containerId);
if (lease == null) {
return;
}
try {
lifecycleManager.getDockerClient().inspectContainerCmd(containerId).exec();
LOG.warnv("MWAA container {0} is still present after removal; it keeps VPC address {1}", containerId,
lease.address());
} catch (NotFoundException expected) {
// Gone, so its address is free.
vpcLeases.remove(containerId);
ec2Service.releaseVpcPrivateIp(lease.region(), lease.subnetId(), lease.address());
}
}
private record VpcLease(String accountId, String region, String vpcId, String subnetId, String address) {
}
/** Writes {@code content} to {@code DAGS_MOUNT/relativePath} inside the Airflow container. */
public void copyDagFile(Environment environment, String relativePath, byte[] content) {
copyFileIntoContainer(environment.getAirflowContainerId(), DAGS_MOUNT, relativePath, content);
@@ -11,9 +11,11 @@ import io.floci.aws.core.common.docker.PortAllocator;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.ec2.VpcDependencyProvider;
import io.floci.aws.services.mwaa.model.CreateEnvironmentRequest;
import io.floci.aws.services.mwaa.model.Environment;
import io.floci.aws.services.mwaa.model.EnvironmentStatus;
import io.floci.aws.services.mwaa.model.NetworkConfiguration;
import io.floci.aws.services.mwaa.model.UpdateEnvironmentRequest;
import io.floci.aws.services.mwaa.proxy.MwaaProxyManager;
import io.floci.aws.services.mwaa.proxy.MwaaWebProxy;
@@ -45,7 +47,7 @@ import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ApplicationScoped
public class MwaaService implements TagHandler {
public class MwaaService implements TagHandler, VpcDependencyProvider {
private static final Logger LOG = Logger.getLogger(MwaaService.class);
@@ -133,9 +135,46 @@ public class MwaaService implements TagHandler {
if (!config.services().mwaa().mock()) {
startReadinessPoller();
startDagSyncPoller();
for (Environment environment : allEnvironments()) {
environmentManager.reclaimVpcAddress(environment);
}
}
}
/**
* Amazon MWAA keeps network interfaces in each subnet an environment names, so DeleteSubnet refuses one an
* environment still uses, as it does here. A plain read of the caller's environments and of the addresses live
* Airflow containers hold, which takes no lock of this service.
*/
@Override
public boolean hasSubnetDependency(String region, String subnetId) {
String accountId = regionResolver.getAccountId();
return holdsLiveAddresses() && environmentManager.holdsSubnet(accountId, region, subnetId)
|| environmentsOf(accountId, region).stream()
.filter(environment -> environment.getStatus() != EnvironmentStatus.DELETED)
.map(Environment::getNetworkConfiguration)
.anyMatch(network -> network != null && network.getSubnetIds() != null
&& network.getSubnetIds().contains(subnetId));
}
/** A VPC is only deleted once its subnets are, which {@link #hasSubnetDependency} guards; this adds live leases. */
@Override
public boolean hasVpcDependency(String region, String vpcId) {
return holdsLiveAddresses() && environmentManager.holdsVpc(regionResolver.getAccountId(), region, vpcId);
}
/** Mock mode runs no containers, so no Airflow container holds a VPC address there. */
private boolean holdsLiveAddresses() {
return !config.services().mwaa().mock();
}
private List<Environment> environmentsOf(String accountId, String region) {
List<Environment> environments = storage instanceof AccountAwareStorageBackend<Environment> aware
? aware.scanForAccount(accountId, k -> true)
: storage.scan(k -> true);
return environments.stream().filter(environment -> region.equals(environmentRegion(environment))).toList();
}
@PreDestroy
public void shutdown() {
readinessPoller.shutdownNow();
@@ -338,7 +377,13 @@ public class MwaaService implements TagHandler {
environment.setPluginsS3Path(request.getPluginsS3Path());
}
if (request.getNetworkConfiguration() != null) {
environment.setNetworkConfiguration(request.getNetworkConfiguration());
// UpdateEnvironment's NetworkConfiguration holds only SecurityGroupIds: an environment's subnets, and so
// its VPC, are fixed at creation, as Amazon MWAA has them.
NetworkConfiguration network = new NetworkConfiguration();
network.setSubnetIds(environment.getNetworkConfiguration() != null
? environment.getNetworkConfiguration().getSubnetIds() : null);
network.setSecurityGroupIds(request.getNetworkConfiguration().getSecurityGroupIds());
environment.setNetworkConfiguration(network);
}
if (request.getLoggingConfiguration() != null) {
environment.setLoggingConfiguration(request.getLoggingConfiguration());