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

(aws-apigatewayv2): apiEndpoint returns incomplete URL #16638

Closed
moltar opened this issue Sep 24, 2021 · 9 comments · Fixed by #16854
Closed

(aws-apigatewayv2): apiEndpoint returns incomplete URL #16638

moltar opened this issue Sep 24, 2021 · 9 comments · Fixed by #16854
Assignees
Labels
@aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@moltar
Copy link
Contributor

moltar commented Sep 24, 2021

https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigatewayv2.HttpApi.html#apiendpointspan-classapi-icon-api-icon-experimental-titlethis-api-element-is-experimental-it-may-change-without-noticespan

Get the default endpoint for this API.

Not sure if this is intentional, as the docs aren't clear.

But calling apiEndpoint on an API that has a domainName and mappingKey set will produce a wrong URL.

Reproduction Steps

const api = new HttpApi(scope, 'api', {
  domainName: properties.domainName, // example.com
  mappingKey: properties.mappingKey, // foo
})

new CfnOutput(this, 'url', {
  value: api.apiEndpoint,
})

What did you expect to happen?

To have the following resolved value:

https://example.com/foo

What actually happened?

The value resolves to the following:

https://xyz.execute-api.ap-northeast-1.amazonaws.com

Note that the domain name is not applied, and the mapping key is not present.

Environment

  • CDK CLI Version : 1.123.0
  • Framework Version: 1.123.0
  • Node.js Version: v14.17.5
  • OS : macOS
  • Language (Version): TS

Other

Docs state:

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-apigatewayv2-readme.html#defining-http-apis

You can retrieve the full domain URL with mapping key using the domainUrl property as so -

screenshot-20210924T144249-hV0QdIDI

However, I don't have this prop on the defaultStage.

screenshot-20210924T144334-z2DNRfUX


This is 🐛 Bug Report

@moltar moltar added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 24, 2021
@github-actions github-actions bot added the @aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 label Sep 24, 2021
@nija-at
Copy link
Contributor

nija-at commented Oct 7, 2021

As documented, api.apiEndpoint is how you retrieve the execute API endpoint.

To get the domain URL, you will need to use api.defaultStage.domainUrl. I can confirm that all of the APIs are present where they say they are.

You need to check your code on your end. One thing to note - api.defaultStage can be undefined in some cases, so you may need to cast or add a check for your IDE to correctly autocomplete.

@nija-at nija-at added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Oct 7, 2021
@moltar
Copy link
Contributor Author

moltar commented Oct 7, 2021

I can confirm that all of the APIs are present where they say they are.

Can you please point out where they are? I cannot find it.

/**
* The default stage of this API
*/
public readonly defaultStage: IHttpStage | undefined;


export interface IHttpStage extends IStage {
/**
* The API this stage is associated to.
*/
readonly api: IHttpApi;
/**
* Metric for the number of client-side errors captured in a given period.
*
* @default - sum over 5 minutes
*/
metricClientError(props?: MetricOptions): Metric
/**
* Metric for the number of server-side errors captured in a given period.
*
* @default - sum over 5 minutes
*/
metricServerError(props?: MetricOptions): Metric
/**
* Metric for the amount of data processed in bytes.
*
* @default - sum over 5 minutes
*/
metricDataProcessed(props?: MetricOptions): Metric
/**
* Metric for the total number API requests in a given period.
*
* @default - SampleCount over 5 minutes
*/
metricCount(props?: MetricOptions): Metric
/**
* Metric for the time between when API Gateway relays a request to the backend
* and when it receives a response from the backend.
*
* @default - no statistic
*/
metricIntegrationLatency(props?: MetricOptions): Metric
/**
* The time between when API Gateway receives a request from a client
* and when it returns a response to the client.
* The latency includes the integration latency and other API Gateway overhead.
*
* @default - no statistic
*/
metricLatency(props?: MetricOptions): Metric
}


export interface IStage extends IResource {
/**
* The name of the stage; its primary identifier.
* @attribute
*/
readonly stageName: string;
/**
* The URL to this stage.
*/
readonly url: string;
/**
* Return the given named metric for this HTTP Api Gateway Stage
*
* @default - average over 5 minutes
*/
metric(metricName: string, props?: MetricOptions): Metric
}

@nija-at
Copy link
Contributor

nija-at commented Oct 7, 2021

Here -

public get domainUrl(): string {

@moltar
Copy link
Contributor Author

moltar commented Oct 7, 2021

But defaultStage is typed as IHttpStage, not a concrete class HttpStage.

However both IHttpStage, and IStage, which it extends, don't have the domainUrl getter.

@nija-at
Copy link
Contributor

nija-at commented Oct 7, 2021

Ah, I missed that. You are correct indeed.

This needs to be fixed.

@nija-at nija-at removed the closing-soon This issue will automatically close in 4 days unless further comments are made. label Oct 7, 2021
@nija-at
Copy link
Contributor

nija-at commented Oct 7, 2021

The workaround would be to cast it to HttpStage. Can you check if that works?

@nija-at nija-at added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Oct 7, 2021
nija-at pushed a commit that referenced this issue Oct 7, 2021
The `defaultStage` prop in `HttpApi` returns `IHttpStage`.

The `domainUrl` getter was previously added only to `HttpStage`.
Elevate this to the `IHttpStage` level so it's available from the
`HttpApi`.

closes #16638
@moltar
Copy link
Contributor Author

moltar commented Oct 8, 2021

This does indeed work!

(this.defaultStage as HttpStage).domainUrl

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Oct 8, 2021
@nija-at
Copy link
Contributor

nija-at commented Oct 11, 2021

I have a fix prepared - #16854

@nija-at nija-at added effort/small Small work item – less than a day of effort p1 and removed needs-triage This issue or PR still needs to be triaged. labels Oct 11, 2021
@mergify mergify bot closed this as completed in #16854 Oct 14, 2021
mergify bot pushed a commit that referenced this issue Oct 14, 2021
…16854)

The `defaultStage` prop in `HttpApi` returns `IHttpStage`.

The `domainUrl` getter was previously added only to `HttpStage`.
Elevate this to the `IHttpStage` level so it's available from the
`HttpApi`.

closes #16638


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this issue Feb 21, 2022
…ws#16854)

The `defaultStage` prop in `HttpApi` returns `IHttpStage`.

The `domainUrl` getter was previously added only to `HttpStage`.
Elevate this to the `IHttpStage` level so it's available from the
`HttpApi`.

closes aws#16638


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 bug This issue is a bug. effort/small Small work item – less than a day of effort p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants