how i can add a http api stage in serverless

1.1k Views Asked by At

I am trying to deploy a serverless application to different stages (prod and dev). I want to deploy it to a single API gateway on two different stages like:- http://vfdfdf.execute-api.us-west-1.amazonaws.com/dev/ http://vfdfdf.execute-api.us-west-1.amazonaws.com/prod/

I have written a code in serverless -

provider:
  name: aws
  runtime: nodejs14.x
  region: ${self:custom.${self:custom.stage}.lambdaRegion}
  httpApi: 
    id: ${self:custom.${self:custom.stage}.httpAPIID}
  stage: ${opt:stage, 'dev'}
1

There are 1 best solutions below

3
On

Edited to reflect the comments

That can be done during the serverless deployment phase.

I would just have the dev by default in the serverless yml file

provider:
  name: aws
  runtime: nodejs14.x
  stage: dev
  region: eu-west-1
  httpApi:
    # Attach to an externally created HTTP API via its ID:
    id: w6axy3bxdj
    # or commented on the very first deployment so serverless creates the HTTP API

custom:
  stage: ${opt:stage, self:provider.stage}

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /${self:custom.stage}/hello
          method: get

Then, the command:

serverless deploy

deploys in stage dev and region here eu-west-1. It's using the default values.

endpoint: GET - https://w6axy3bxdj.execute-api.eu-west-1.amazonaws.com/dev/hello

While for production, the default values can be overridden on the command line. Then I would use the command:

serverless deploy --stage prod

endpoint: GET - https://w6axy3bxdj.execute-api.eu-west-1.amazonaws.com/prod/hello

In my understanding, you do not change the region between dev and prod; but in case you would want to do that. The production deployment could be:

serverless deploy --stage prod --region eu-west-2

to deploy in a different region than the default one from the serverless yml file.