How to remove stage from URLs for AWS Lambda functions + Serverless framework?
This is an API Gateway feature/convention NOT from Serverless Framework so serverless
can't do anything about it.
API Gateway requires you with a stage and it is appended at the end of your endpoint.
API Gateway endpoints are meant for developers though so it is not meant to be user-friendly.
If you want it to be user-friendly, you can add a custom domain for it. Different stages can have different custom subdomains.
In the local environment, we may use the flag --noPrependStageInUrl when running the dev server: sls offline start --noPrependStageInUrl
when using serverless offline. Online, we may set up a CloudFront or custom domain.
Triggered by @dashnug's answer "API Gateway requires you with a stage and it is appended at the end of your endpoint" and another reply that I read elsewhere, I 'solved' the problem by making the stage specification a bit less telling (about which stage environment was referred to) by using v1
as a stage. That also suggests some sort of API versioning, which is acceptable in my case as well.
So, my serverless.yml
section now contains:
provider:
name: aws
runtime: python3.6
memorySize: 512
region: ${opt:region, 'eu-west-1'}
profile: ${opt:profile, 'default'}
stage: ${opt:stage, 'v1'} # A trick to don't end up with "production" or "staging" as stage.
One thing you can do is use a custom domain that you own (e.g. mycompany.com
) and map that to your API Gateway. This way, rather than making a request to https://ab1cd2ef3g.execute-api.eu-west-1.amazonaws.com/dev/
, you would make a request to https://api.mycompany.com/
.
There's a plugin called serverless-domain-manager
that makes it much easier to set up this custom domains. Check out this blog post for a full walkthrough on how to use it.