I’m not sure if this is a bug or if I am doing something wrong but I keep getting this exception when I try to attach a custom domain to my API gateway:
3:28:13 p.m. | CREATE_FAILED | AWS::ApiGatewayV2::ApiMapping
| API/Default…DomainName$default Resource handler returned message:
“Invalid domain name identifier specified (Service:
AmazonApiGatewayV2; Status Code: 404; Error Code: NotFoundException;
Request ID: ; Proxy: null)” (RequestToken: ,
HandlerErrorCode: GeneralServiceException)
Here is my code:
export class APIStack extends Stack {
constructor(scope: Construct, id: string, props: APIStackProps) {
super(scope, id, props);
// Custom Domain
const apiDomainName = `api.${props.appName}.${props.rootDomain}`;
const hostedZone = HostedZone.fromLookup(this, "HostedZone", {
domainName: props.rootDomain,
});
const domainCertificate = new Certificate(
this,
`${props.appName}-Certificate`,
{
domainName: apiDomainName,
certificateName: `${props.appName}-Certificate`,
validation: CertificateValidation.fromDns(hostedZone),
}
);
// Custom domain for API
const gatewayDomain = new DomainName(this, `${props.appName}-DomainName`, {
domainName: apiDomainName,
certificate: domainCertificate,
endpointType: EndpointType.REGIONAL,
});
// Security
const apiAuthorizer = new HttpUserPoolAuthorizer(
`${props.appName}-APIAuthorizer`,
props.userPool,
{ userPoolClients: [props.appClient] }
);
// API
const httpApi = new HttpApi(this, `${props.appName}-API`, {
defaultAuthorizer: apiAuthorizer,
defaultDomainMapping: {
domainName: gatewayDomain,
mappingKey: "$default",
},
});
new ApiMapping(this, "Mapping", {
domainName: gatewayDomain,
api: httpApi,
});
// route53 records
new ARecord(this, `${props.appName}-AliasRecord`, {
zone: hostedZone,
recordName: apiDomainName,
target: RecordTarget.fromAlias(
new ApiGatewayv2DomainProperties(
gatewayDomain.regionalDomainName,
gatewayDomain.regionalHostedZoneId
)
),
});
// Lambdas
const testLambda = new NodejsFunction(this, `${props.appName}-TestLambda`, {
runtime: Runtime.NODEJS_20_X,
handler: "handler",
functionName: `${props.appName}-test-function`,
entry: path.join(__dirname, "../lambdas/endpoints/test", "index.ts"),
});
// Registering routes
const testLambdaIntegration = new HttpLambdaIntegration(
"TestLambdaIntegration",
testLambda
);
httpApi.addRoutes({
path: "/test",
methods: [HttpMethod.GET],
integration: testLambdaIntegration,
authorizer: apiAuthorizer,
});
}
}
The error occurs when I create the ApiMapping and, from what I can tell, CloudFormation is trying to create the mapping before the domain for the API Gateway (gatewayDomain) has been created and propogated. I’ve tried explicitly adding a dependency to create the domain before creating the mapping, but it still does not work:
apiMapping.node.addDependency(gatewayDomain)
I have found that creating the mapping using the console, or using the aws cli works but I’d like to use the cdk for this. Any help would be appreciate 🙂
Rohinesh Ram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.