Aws-cdk: Need to add base path to existing custom domain on api gatway.

Created on 6 Nov 2019  Â·  17Comments  Â·  Source: aws/aws-cdk


DomainName.fromDomainNameAttributes gives a way to add existing custom domain for my new api. I would like to add base path mapping for this new api.

Use Case

We do not want to have multiple custom domain for our apis. As we develop, we want to add new apis with different base path under same custom domain, so we can brand our api for partners under same custom domain.

Proposed Solution

Other

Like DomainName.fromDomainNameAttributes, there should be method to associate existing custom domain with new api with unique custom domain.

  • [ ] :wave: I may be able to implement this feature request
  • [ ] :warning: This feature might incur a breaking change

This is a :rocket: Feature Request

@aws-cdaws-apigateway feature-request needs-triage response-requested

Most helpful comment

@devarpi where do you get domainNameAliasTarget? I try to get this parameter from PublicHostedZone construct in cdk, but don't see it anywhere.

All 17 comments

Hi Guys, Sorry at the same time, It's stopping me to move forward with CDK for our implementation. I would really appreciate if you have any work around or a way forward for this request.

I don't think I understand your issue/ask here.

You should be able to add a base path mapping to your imported domain name using the BasePathMapping construct.

Could you describe more in detail what it is that you're trying and unable to do?

So we have custom domain created for our API platform. And we want to add more apis under same custom domain as we build. Kind of branding our all api under same domain. Is BasePathMapping construct going to allow me to add and existing custom domain? If so I would appreciate if you have sample, I tried few things and that didn't work.

We have some examples in our documentation and in our integ tests.

I would suggest to go read up on APIGateway documentation to see whether they have what you're looking for and what combination of APIGateway's primitives satisfies that.

CDK's role is here to make it easier to use AWS service, of which APIGateway is one. Closing this issue; re-open if you have a CDK issue.

Sorry, I have looked at those already and issue with that is, it created new custom domain than allowing me to add new base path, my use case is custom domain is already in place, I want to attach new base path mapping to existing custom domain.

Hey @devarpi you figured this out?

this is doable with CloudFormation:

  NewApiGatewayBasePathMapping:
    Type: AWS::ApiGateway::BasePathMapping
    Properties:
      BasePath: your-new-basepath-here
      DomainName:
        Fn::ImportValue:
          Fn::Sub: ExistingCustomApiGatewayDomainName
      RestApiId:
        Ref: NewApiGateway

Now I'm trying to figure out how to get this with CDK, but dont see how DomainName.fromDomainNameAttributes can be used because it returns the interface IDomainName, which doesn't have addBasePathMapping method. Any suggestions?

Can someone post an example on how to use DomainName.fromDomainNameAttributes to fetch an existing domain and add a new base path mapping?

Sorry for the late reply, I am on vacation after reInvant , will surely
look in to this and share, have learnt new technics to create custom
solution, that will help to fix this.
I really think this is an issue that needs to be resolved soon.

On Wed, Dec 11, 2019, 6:33 PM Milan Simonovic notifications@github.com
wrote:

Can someone post an example on how to use
DomainName.fromDomainNameAttributes to fetch an existing domain and add a
new base path mapping?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aws/aws-cdk/issues/4887?email_source=notifications&email_token=AAHVHQIF6GIOM7GSFVZQCM3QYDQLBA5CNFSM4JJ6TWAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGTAW7I#issuecomment-564530045,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAHVHQLSPN5EXRGNYIWBEVTQYDQLBANCNFSM4JJ6TWAA
.

Sorry for late reply. Here is the solution that worked.

//Get custom domain from attribute
const domainName = DomainName.fromDomainNameAttributes(this,
    `${envName}-${appName}-${YOUR_DOMAIN_NAME}`, {
    domainName: `${YOUR_DOMAIN_NAME}`,
    domainNameAliasTarget: `${domainNameAliasTarget}`,       // GET from Route 53- I would prefer having this property as optional
    domainNameAliasHostedZoneId: `${domainNameAliasHostedZoneId}` // GET from Route 53- I would prefer having this property as optional
});

//Rest API
const api = new apiGateway.RestApi(this, `${envName}-${appName}`,
{
    restApiName: `${envName}-${apiName}`,
    deployOptions: { stageName: envName }
});



//Used in custom domain
const pathMapping = new BasePathMapping(this,
    `${envName}-${appName}-path-mapping`, {
    basePath: `${appName}-${envName}`,
    domainName: domainName,
    restApi: api
});

I started getting https://github.com/aws/aws-cdk/issues/5794 when upgraded cdk (1.25.0)

Ended up fixing as recommended on the issue page above.

//BASE Path already exists issue with following

                // //Used in custom domain
                // const pathMapping = new BasePathMapping(this,
                //     `${envName}-${appName}-path-mapping`, {
                //     basePath: `${appName}-${envName}`,
                //     domainName: domainName,
                //     restApi: api
                // });

                //fix by CFNBasePathMapping.
                new apiGateway.CfnBasePathMapping(this, `${envName}-${appName}-path-mapping`, {
                    basePath: `${appName}-${envName}`,
                    domainName: domainName.domainName,
                    restApiId: api.restApiId,
                    stage: api.deploymentStage.stageName
                });

@devarpi where do you get domainNameAliasTarget? I try to get this parameter from PublicHostedZone construct in cdk, but don't see it anywhere.

Hi @mokus did you figure out where to get the domainNameAliasTarget?

@mokus @seanyu4296 i am at the same question right now. The docs are kind of wishy washy there...

@seanyu4296 @logemann
I've found using new DomainName(...).addBasePathMapping(...) the most convenient.
And don't forget also to create an ARecord in route53 with new ARecord(...)

However in my specific case I need to unattach the same domain from one APIGateway and attach to another. And CDK can't swallow my scenario. So I ended creating a CustomResource with lambda attached to it that does all the stuff via aws-sdk.

cant use new DomainName() in my stack, thats the reason why i am using fromDomainNameAttributes() in the first place. And its not clear what domainNameAliasTarget is exactly. In my original Stack, the target is defined like this:

RecordTarget.fromAlias(new targets.ApiGatewayDomain(originalDomainName)),

I'm having the same problem I have a pre existing custom domain name and am trying to just add a new base path mapping to it. Has anyone worked it out?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kawamoto picture kawamoto  Â·  3Comments

ababra picture ababra  Â·  3Comments

eladb picture eladb  Â·  3Comments

artyom-melnikov picture artyom-melnikov  Â·  3Comments

vgribok picture vgribok  Â·  3Comments