0

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'}
Mradul Jain
  • 62
  • 12

1 Answers1

0

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.

tsamaya
  • 376
  • 2
  • 13
  • 1
    Yes, I have tried this it will create a cloud formation and lambda prefix with stage, but did not use gateway stage like /dev for --stage dev & /prod for -- stage prod with http api gateways – Mradul Jain Mar 25 '22 at 11:58
  • Right, this is a difference between REST API and HTTP API. let me quicky change the response above, still using the same pattern, default is dev in the yml file and production on the command line – tsamaya Mar 25 '22 at 12:24
  • i don't want to write these line of code in every function. path: /${self:custom.stage}/hello – Mradul Jain Mar 25 '22 at 15:53