在handler.js的serverless.yml中使用stage变量

时间:2019-12-02 02:39:48

标签: aws-lambda serverless

我想在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}`
    }
  }
}

1 个答案:

答案 0 :(得分:1)

如果您的集成方法是“ Lambda代理集成”,则您应该能够通过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}`
    }
  }
}