AWS API Gateway - Remove Stage Name From URI
Asked Answered
V

4

78

The stage name is added to the url when I deploy the API. Let's say the stage name is "test", then the generated URL for the resource includes the stage name. Something like: https://abcabc.execute-api.us-east-1.amazonaws.com/test/my/path

I would like to remove the stage name in the URL. How can I do it?

Vitrescent answered 16/9, 2016 at 3:0 Comment(1)
I ended up setting up a Cloudfront distribution to use a custom domain name and then used a Cloudfront function to remove the stage name that was appended to my custom domain name for my Zappa deployed Flask app: #69504866Slothful
C
61

You can do this with API Gateway Custom Domains. This allows you to optionally omit the stage name in the custom domain name mapping by setting path to *.

Checkoff answered 16/9, 2016 at 22:17 Comment(9)
So, in the example above, "mydomain.com/my/path" point to my "test" stage? it wouldn't show "mydomain.com/test/my/path" ?Vitrescent
That's correct - you can omit the stage name in the custom domain mappingCheckoff
@Checkoff in which step we can omit the stage name? I could only see path (optional) in the Configure API mappings panel. If I put in somepath for the Path, the api becomes https://example.com/somepath/dev0/yyyy but the stage name dev0 is still thereSnoopy
@Checkoff my bad, just tried, setting path (optional) worksSnoopy
@Checkoff where exactly you seen an option for omitting the stage name in the custom domain settings?Economical
This answer doesn't work for me as my APIG is private and can't use a custom domain nameLavin
To be clear in the Custom Domain, you need to add a Path mapping where you just put * as only mappingEulogistic
"You must have a registered internet domain name in order to set up custom domain names for your APIs"Collete
Michael, could you add @Kyriba 's answer as an edit for 2024? This answer is not working for me and the other one is doing the job.Hardship
E
16

I'm using public HTTP API and no CloudFront. The Config Mapping solution did not work for me. The path was still prepended with the stage name, whether I used * or just an empty path as a mapping Path.

I have a single catch-all route, ANY /{proxy+} with a single Lambda integration. To make it work I configured Integration parameter mapping to overwrite the path with $request.path - It is "The request path, without the stage name."

enter image description here

Ekg answered 30/1, 2023 at 16:47 Comment(0)
M
12

I was unable to use the custom domains approach in the accepted answer because my api gateway is already in use with the stage in the url. I needed to remove the stage for a particular route. For this example, this route: https://example.com/apple-app-site-association needs to hit api gateway route: https://example.com/v1/apple-app-site-association.

Using cloudfront,

in the Origins tab, create a new origin to point to the api gateway including the stage:

Origin Domain Name: example.com

Origin Path: /v1

Origin ID: (give this a unique name and note this for next step)

(Leave remaining settings as default or change as you require, I set Origin Protocol Policy: HTTPS Only)

in the Behaviors tab, create a new behavior:

Path Pattern: /apple-app-site-association

Origin: (the Origin ID you set above)

(Leave remaining settings as default or change as you require)

Mckinleymckinney answered 14/11, 2018 at 23:46 Comment(0)
C
0

[2024] Use new HTTP API

  1. Create a new HTTP API (Not the old REST API)
  2. Create catch all route and assign your lambda function: ANY /{proxy+} -> Your lambda
  3. Leave the stage as $default
  4. Visit the invoke URL of $default stage
Cryometer answered 9/6 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.