Aws-cdk: apigateway: support custom domains

Created on 27 Jun 2019  路  12Comments  路  Source: aws/aws-cdk

Missing capability in API gateway (#723)

@aws-cdaws-apigateway feature-request

Most helpful comment

I'm trying to map my RestApi to existing domain name (created earlier and mapped to multiple rest APIs) with following code:

  const domainName = DomainName.fromDomainNameAttributes(this, 'MyApiDomain', {
    domainName: 'my.existing.domain.name',
    domainNameAliasTarget: 'XXX,       // I would prefer having this property as optional
    domainNameAliasHostedZoneId: 'YYY' // I would prefer having this property as optional
  });
  domainName.addBasePathMapping(myRestApi, { basePath: 'mymapping' });

and I am getting following error (when compiling cdk code):

Property 'addBasePathMapping' does not exist on type 'IDomainName'.

or when I retype domainName as DomainName (on cdk synth, cdk diff or cdk deploy):

TypeError: domainName.addBasePathMapping is not a function

Are there any plans to address such case? I am using CDK version 0.36.2

All 12 comments

WIP

I'm trying to map my RestApi to existing domain name (created earlier and mapped to multiple rest APIs) with following code:

  const domainName = DomainName.fromDomainNameAttributes(this, 'MyApiDomain', {
    domainName: 'my.existing.domain.name',
    domainNameAliasTarget: 'XXX,       // I would prefer having this property as optional
    domainNameAliasHostedZoneId: 'YYY' // I would prefer having this property as optional
  });
  domainName.addBasePathMapping(myRestApi, { basePath: 'mymapping' });

and I am getting following error (when compiling cdk code):

Property 'addBasePathMapping' does not exist on type 'IDomainName'.

or when I retype domainName as DomainName (on cdk synth, cdk diff or cdk deploy):

TypeError: domainName.addBasePathMapping is not a function

Are there any plans to address such case? I am using CDK version 0.36.2

It seems odd that the default endpoint type is REGIONAL. It seems that most users that are making a public API with custom domain name would benefit from choosing EDGE instead, which is the default selection when using the console.

Thanks for this feedback. This makes sense to me. I'll open an issue

I am also running into the issue referenced above but not responded to.
https://github.com/aws/aws-cdk/issues/3103#issuecomment-508544101

I can import an existing custom domain mapping with the from_domain_name_attributes, but there is no add_base_path_mapping method on IDomainNames for some reason, so I can do nothing with it.

Any solution?

@moravcik @f-Stabby

I'm not really sure what fromDomainNameAttributes is for, but I was able to add a base path mapping this way:

````
const domain = new apigw.DomainName(this, 'myApiDomain', {
domainName: "api.mydomain.com",
certificate: acm.Certificate.fromCertificateArn(this, "myApiDomainCertificate", ""),
endpointType: apigw.EndpointType.EDGE
});

domain.addBasePathMapping(api, { basePath: 'v1'});
````

Hope that helps somehow!

FromDomainNameAttributes should be like the other "fromxx" functions that exist in most constructs in that, it is for referencing a resource that already exists.

Rather than deleting a preexisting resource and recreating it, I would like to be able to reference the domain name using FromDomainNameAttributes, then add a base path mapping to it.

Hope this gives more context.
@jderose9

@moravcik @f-Stabby

I'm not really sure what fromDomainNameAttributes is for, but I was able to add a base path mapping this way:

const domain = new apigw.DomainName(this, 'myApiDomain', {
  domainName: "api.mydomain.com",
  certificate: acm.Certificate.fromCertificateArn(this, "myApiDomainCertificate", "<cert arn>"),
  endpointType: apigw.EndpointType.EDGE
});

domain.addBasePathMapping(api, { basePath: 'v1'});

Hope that helps somehow!

We need to add new api to existing custom domain. This will create new domain. Is there a way to do that?

@devarpi

We need to add new api to existing custom domain. This will create new domain. Is there a way to do that?

I believe if you follow a similar pattern but instead of using new apigw.DomainName... you use:

const domain = apigw.DomainName.fromDomainNameAttributes(...)

then you should be able to use a domain which already exists.

@JoshM1994 read the comments above, fromDomainNameAttributes returns the interface IDomainName which does not have this property addBasePathMapping. Casting it to the class results in a runtime error TypeError: domainName.addBasePathMapping is not a function. So seems like there's no way to do this?

@mbsimonovic yes, it is possible.

Instead of using new DomainName() you have to use DomainName.fromDomainNameAttributes and then define BasePathMapping.

Example below, I hope it helps:

const api = new RestApi(this, "rest-api", {}); 

const domainName = DomainName.fromDomainNameAttributes(this, "dev-example", {
   domainName: "dev.example.com",
   domainNameAliasHostedZoneId: "",
   domainNameAliasTarget: ""
});

new BasePathMapping(this, "path-mapping-users", {
      basePath: "users",
      domainName: domainName,
      restApi: api
});

What is domainNameAliasTarget in domain name attributes?

Was this page helpful?
0 / 5 - 0 ratings