serverless-domain-manager icon indicating copy to clipboard operation
serverless-domain-manager copied to clipboard

Allow custom stage for HTTP API

Open webpepper opened this issue 4 years ago • 4 comments

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Bug Report

Error Description

When using HTTP API the stage seems to be hardcoded to $default.

const params = {
  ApiId: domain.apiId,
  ApiMappingKey: domain.basePath,
  DomainName: domain.givenDomainName,
  Stage: domain.apiType === Globals.apiTypes.http ? "$default" : domain.stage, // <-- bug??
};

See https://github.com/amplify-education/serverless-domain-manager/blob/257664190acf6e5148cf083149823e68c6d587e1/index.ts#L606

Currently it's impossible to use anything other than the $default stage with this plugin.

Command Run sls deploy

Console Output

Serverless: [AWS apigatewayv2 400 0.928s 0 retries] createApiMapping({
  ApiId: 'xxx',
  ApiMappingKey: 'dev',
  DomainName: 'xxx',
  Stage: '$default'
})
Serverless Domain Manager: Error: xxx:  BadRequestException: Invalid stage identifier specified
Serverless Domain Manager: Error: xxx:  Error: Error: xxx: Unable to create basepath mapping.

Domain Manager Configuration

custom:
  customDomain:
    domainName: somedomain.com
    basePath: dev
    stage: dev
    certificateName: somedomain.com
    createRoute53Record: true
    endpointType: 'regional'
    apiType: http
    securityPolicy: tls_1_2
    hostedZoneId: XYZ

Versions

  • Domain Manager version(s): 4.0.0
  • Node/npm version: 12.16.1/6.13.4
  • Serverless Version: 1.69.0
  • Lambda Code: Javascript

Possible Solution

Replace

Stage: domain.apiType === Globals.apiTypes.http ? "$default" : domain.stage

With

Stage: domain.apiType === Globals.apiTypes.http ? (domain.stage || "$default") : domain.stage

webpepper avatar May 07 '20 20:05 webpepper

Hmm, so I recall working on this and it was based on the related conversation here: https://github.com/serverless/serverless/issues/7052#issuecomment-584267234 and https://github.com/serverless/serverless/pull/7331

...where the Serverless framework will always deploy HTTP APIs to the $default stage as the serverless framework doesn't really use stages and creates an entirely different API for each "stage".

What is the use case you have for a defined stage instead of $default?

You might be able to drop stage from the serverless domain config if it isn't necessary.

tehnrd avatar May 12 '20 23:05 tehnrd

Looks like they reverted that change and added back support for paths, so it would make sense to fix this. https://github.com/serverless/serverless/pull/7383

tehnrd avatar May 12 '20 23:05 tehnrd

The original need for this was migration from the REST API. Even if I don't need multiple stages right now, API Gateway supports them. So not allowing to use them in this plugin is a limitation.

This reasoning doesn't work in my case:

creates an entirely different API for each "stage"

Mainly due CloudFormation's 200 resource limit I split API creation into its own separate stack.

Since this HTTP API is being created in the Resources section, theoretically I'm not limited by the number of stages I can create.

Then in other stacks with lambdas/endpoints I reuse this previously created HTTP API resource. So I should be able to use this API by referencing it (related to #335) and specifying a stage to which I want to deploy and link my domain.

webpepper avatar May 13 '20 09:05 webpepper

Hello, I have one doubt regarding the $default stage. When I want to deploy an HTTP API, the $default stage is not automatically created, and I always get the error "BadRequestException: Invalid stage identifier specified".

When I manually create the $default stage in the API everything works fine. There is any way how I could automate that creation?

tiagoramalho avatar Jul 21 '20 07:07 tiagoramalho

Any intention of implementing this? It's an odd limitation.

hope-portal-services avatar Nov 04 '22 02:11 hope-portal-services