diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilter.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilter.java
new file mode 100644
index 0000000000..c5a7077dd7
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilter.java
@@ -0,0 +1,115 @@
+package io.github.hectorvent.floci.services.apigateway;
+
+import io.github.hectorvent.floci.services.apigateway.model.BasePathMapping;
+import io.github.hectorvent.floci.services.apigateway.model.CustomDomain;
+import jakarta.annotation.Priority;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.container.PreMatching;
+import jakarta.ws.rs.core.UriBuilder;
+import jakarta.ws.rs.ext.Provider;
+import org.jboss.logging.Logger;
+
+import java.net.URI;
+
+/**
+ * Routes requests based on the Host header for API Gateway custom domains.
+ *
+ *
When a request arrives with a Host header matching a registered custom domain's
+ * {@code regionalDomainName} (e.g., {@code my-domain.regional.local:4566}), this filter
+ * resolves the corresponding base path mapping and rewrites the request URI to the
+ * standard execute-api path format: {@code /execute-api/{apiId}/{stageName}/{path}}.
+ *
+ *
This enables users to invoke API Gateway APIs using custom domain names, matching
+ * real AWS behavior where custom domains route to REST APIs via base path mappings.
+ */
+@Provider
+@PreMatching
+@Priority(10) // Run after Lambda URL filter (5) but before general processing
+public class ApiGatewayCustomDomainFilter implements ContainerRequestFilter {
+
+ private static final Logger LOG = Logger.getLogger(ApiGatewayCustomDomainFilter.class);
+ private static final String REGIONAL_SUFFIX = ".regional.local";
+
+ private final ApiGatewayService apiGatewayService;
+
+ @Inject
+ public ApiGatewayCustomDomainFilter(ApiGatewayService apiGatewayService) {
+ this.apiGatewayService = apiGatewayService;
+ }
+
+ @Override
+ public void filter(ContainerRequestContext requestContext) {
+ String host = requestContext.getHeaderString("Host");
+ if (host == null) {
+ return;
+ }
+
+ String hostname = stripPort(host);
+
+ // Try matching by regionalDomainName first, then by bare domain name
+ CustomDomain domain = null;
+ if (hostname.endsWith(REGIONAL_SUFFIX)) {
+ domain = apiGatewayService.findDomainByRegionalHostname(hostname);
+ }
+ if (domain == null) {
+ domain = apiGatewayService.findDomainByName(hostname);
+ }
+ if (domain == null) {
+ return;
+ }
+
+ URI originalUri = requestContext.getUriInfo().getRequestUri();
+ String path = originalUri.getRawPath();
+ if (path == null) {
+ path = "/";
+ }
+
+ // Resolve the base path mapping for this domain + path
+ BasePathMapping mapping = apiGatewayService.resolveBasePathMapping(domain.getDomainName(), path);
+ if (mapping == null) {
+ LOG.debugv("No base path mapping found for domain {0} path {1}", domain.getDomainName(), path);
+ return;
+ }
+
+ String restApiId = mapping.getRestApiId();
+
+ if (restApiId == null) {
+ return;
+ }
+
+ // Strip the base path from the request path to get the remaining path
+ String remainingPath = apiGatewayService.stripBasePath(path, mapping);
+
+ String effectiveStage = mapping.getStage();
+ if (effectiveStage == null || effectiveStage.isEmpty()) {
+ return;
+ }
+
+ if (remainingPath.startsWith("/")) {
+ remainingPath = remainingPath.substring(1);
+ }
+
+ // Rewrite to the standard execute-api path
+ String newPath = "/execute-api/" + restApiId + "/" + effectiveStage + "/" + remainingPath;
+
+ URI newUri = UriBuilder.fromUri(originalUri)
+ .replacePath(newPath)
+ .build();
+
+ LOG.debugv("Custom domain routing: {0}{1} -> {2}", host, path, newUri.getPath());
+ requestContext.setRequestUri(newUri);
+ }
+
+ private static String stripPort(String host) {
+ int colonIndex = host.lastIndexOf(':');
+ if (colonIndex > 0) {
+ String maybePart = host.substring(colonIndex + 1);
+ if (!maybePart.isEmpty() && maybePart.chars().allMatch(Character::isDigit)) {
+ return host.substring(0, colonIndex);
+ }
+ }
+ return host;
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java
index 078fef83f6..23fe8602c4 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayService.java
@@ -686,6 +686,13 @@ public class ApiGatewayService {
String domainName = (String) request.get("domainName");
if (domainName == null) throw new AwsException("BadRequestException", "domainName is required", 400);
+ // AWS enforces global uniqueness of custom domain names across all regions
+ boolean exists = !domainStore.scan(k -> k.endsWith("::" + domainName)).isEmpty();
+ if (exists) {
+ throw new AwsException("ConflictException",
+ "The domain name you provided already exists.", 409);
+ }
+
CustomDomain domain = new CustomDomain();
domain.setDomainName(domainName);
domain.setCertificateName((String) request.get("certificateName"));
@@ -748,6 +755,91 @@ public class ApiGatewayService {
basePathMappingStore.delete(mappingKey(region, domainName, path));
}
+ // ──────────────────────────── Custom Domain Resolution ────────────────────────────
+
+ /**
+ * Resolves a custom domain by its regionalDomainName (e.g., "my-domain.regional.local").
+ * Derives the domain name from the regionalDomainName and performs a key-based lookup.
+ *
+ * @return the CustomDomain if found, or null if no domain matches
+ */
+ public CustomDomain findDomainByRegionalHostname(String regionalDomainName) {
+ if (!regionalDomainName.endsWith(".regional.local")) {
+ return null;
+ }
+ String domainName = regionalDomainName.substring(0,
+ regionalDomainName.length() - ".regional.local".length());
+ return findDomainByName(domainName);
+ }
+
+ /**
+ * Resolves a custom domain by its actual domain name (e.g., "api.example.com").
+ * Domain names are globally unique across regions.
+ *
+ * @return the CustomDomain if found, or null if no domain matches
+ */
+ public CustomDomain findDomainByName(String domainName) {
+ List results = domainStore.scan(k -> k.endsWith("::" + domainName));
+ return results.isEmpty() ? null : results.get(0);
+ }
+
+ /**
+ * Resolves the base path mapping for a given domain and request path.
+ * Uses longest-prefix matching on the base path.
+ *
+ * @param domainName the custom domain name
+ * @param requestPath the incoming request path (e.g., "/v1/items/123")
+ * @return the matching BasePathMapping, or null if none matches
+ */
+ public BasePathMapping resolveBasePathMapping(String domainName, String requestPath) {
+ // Get all mappings across all regions for this domain
+ List allMappings = basePathMappingStore.scan(k -> k.contains("::" + domainName + "::"));
+
+ if (allMappings.isEmpty()) {
+ return null;
+ }
+
+ // Find the best match using longest-prefix matching
+ BasePathMapping bestMatch = null;
+ int bestLength = -1;
+
+ for (BasePathMapping mapping : allMappings) {
+ String basePath = mapping.getBasePath();
+ if ("(none)".equals(basePath)) {
+ // Catch-all mapping — matches if no better mapping exists
+ if (bestLength < 0) {
+ bestMatch = mapping;
+ bestLength = 0;
+ }
+ } else {
+ String prefix = "/" + basePath;
+ if (requestPath.equals(prefix) || requestPath.startsWith(prefix + "/")) {
+ if (basePath.length() > bestLength) {
+ bestMatch = mapping;
+ bestLength = basePath.length();
+ }
+ }
+ }
+ }
+
+ return bestMatch;
+ }
+
+ /**
+ * Returns the remaining path after stripping the matched base path prefix.
+ */
+ public String stripBasePath(String requestPath, BasePathMapping mapping) {
+ String basePath = mapping.getBasePath();
+ if ("(none)".equals(basePath)) {
+ return requestPath;
+ }
+ String prefix = "/" + basePath;
+ if (requestPath.equals(prefix)) {
+ return "/";
+ }
+ return requestPath.substring(prefix.length());
+ }
+
// ──────────────────────────── Update Methods ────────────────────────────
public RestApi updateRestApi(String region, String apiId, List