Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apigateway: support custom domains #3103

Closed
eladb opened this issue Jun 27, 2019 · 14 comments · Fixed by #3135 or MechanicalRock/tech-radar#14 · May be fixed by MechanicalRock/cdk-constructs#5, MechanicalRock/cdk-constructs#6 or MechanicalRock/cdk-constructs#7
Assignees
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway feature-request A feature should be added or improved.

Comments

@eladb
Copy link
Contributor

eladb commented Jun 27, 2019

Missing capability in API gateway (#723)

@eladb eladb added the needs-triage This issue or PR still needs to be triaged. label Jun 27, 2019
@eladb eladb self-assigned this Jun 27, 2019
@eladb
Copy link
Contributor Author

eladb commented Jun 27, 2019

WIP

@eladb eladb mentioned this issue Jun 27, 2019
12 tasks
@NGL321 NGL321 added @aws-cdk/aws-apigateway Related to Amazon API Gateway feature-request A feature should be added or improved. gap and removed needs-triage This issue or PR still needs to be triaged. labels Jun 27, 2019
eladb pushed a commit that referenced this issue Jun 30, 2019
Adds support for custom domain names in API Gateway and using route53 aliases.

Fixes #3103

Misc: change the awslint rule that verifies resource attributes to use CloudFormation attribute names.
eladb pushed a commit that referenced this issue Jul 1, 2019
Adds support for custom domain names in API Gateway and using route53 aliases.

Fixes #3103

Misc: change the awslint rule that verifies resource attributes to use CloudFormation attribute names.
@moravcik
Copy link

moravcik commented Jul 4, 2019

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

@jderose9
Copy link

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.

@eladb
Copy link
Contributor Author

eladb commented Jul 31, 2019

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

@f-Stabby
Copy link

I am also running into the issue referenced above but not responded to.
#3103 (comment)

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?

@jderose9
Copy link

jderose9 commented Aug 14, 2019

@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!

@f-Stabby
Copy link

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

@devarpi-zz
Copy link

@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?

@JoshM1994
Copy link

JoshM1994 commented Dec 3, 2019

@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.

@mbsimonovic
Copy link

@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?

@kamaz
Copy link

kamaz commented Dec 13, 2019

@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
});

@roitman-g
Copy link

What is domainNameAliasTarget in domain name attributes?

@uglow
Copy link

uglow commented Jul 4, 2022

@roitman-g I found the answer to to your question here

@BwL1289
Copy link

BwL1289 commented Mar 1, 2024

@roitman-g and for anyone else out there trying to figure out how to get domainNameAliasTarget programmatically in CDK -- from what I've found, there's no good way.

The answer here is correct, but unless I'm mistaken, it doesn't seem there's a way to grab it as a property from any other resource construct (except after you create the custom domain in CDK like new_custom_domain_name.domain_name_alias_domain_name, for example).

There may be a good reason for this under the hood, but not sure.

Best alternative I found is saving as an SSM parameter in CDK when I create the custom domain and importing it thereafter.

If @eladb you know of (or if anyone finds one) a better way, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment