Search code examples
aws-lambdaserverless

use the stage variable in serverless.yml at handler.js


I'd like to use the variable stage in handler.js

serverless.yml

provider:
  name: aws
  runtime: nodejs10.x
  region: ap-northeast-2
  stage: ${opt:stage, "dev"}
...

handler.js

export const hello = () => {
  // I'd like to use the stage in here,
  const isDev = stage === 'dev';
  return {
    statusCode: 200,
    body: {
      message: `isDev ${isDev}`
    }
  }
}


Solution

  • If your method of integration is "Lambda Proxy Integration", You should be able to access stage via event.requestContext.stage.

    export const hello = (event) => {
      // I'd like to use the stage in here,
      console.log('stage is: ', event.context.stage)
      const isDev = stage === 'dev';
      return {
        statusCode: 200,
        body: {
          message: `isDev ${isDev}`
        }
      }
    }