简体   繁体   English

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

[英]use the stage variable in serverless.yml at handler.js

I'd like to use the variable stage in handler.js我想在handler.js使用变量 stage

serverless.yml无服务器.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}`
    }
  }
}

If your method of integration is "Lambda Proxy Integration", You should be able to access stage via event.requestContext.stage .如果您的集成方法是“Lambda 代理集成”,您应该能够通过event.requestContext.stage访问 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}`
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Serverless.yml AWS Lambda 将 EJS 模板嵌入到 Handler.js 中 - Serverless.yml AWS Lambda Embedding EJS Template into Handler.js Serverless.yml AWS Lambda 将 EJS 模板主体参数嵌入到 handler.js 中 - Serverless.yml AWS Lambda Embedding EJS Template body parameters into handler.js 为 AWS Lambda Serverless.yml 编写处理程序的正确方法 - Correct way to write the Handler for AWS Lambda Serverless.yml 如何在`serverless.yml`的`Resources`中使用`If`条件? - How can I use `If` condition in `Resources` of `serverless.yml`? 是否可以通过 serverless.yml 向 PATH 环境变量添加路径? - Is it possible to add paths to the PATH environment variable through serverless.yml? 如何在 serverless.yml 中为 lambda 使用条件配置? - How can I use conditional configuration in serverless.yml for lambda? 拆分 serverless.yml 文件 - Split serverless.yml file serverless.yml 调用 python - serverless.yml to invoke python aws serverless.yml 文件“找不到满足声明‘opt:stage’的有效选项”错误 - aws serverless.yml file “A valid option to satisfy the declaration 'opt:stage' could not be found” error 如何在无服务器框架的“serverless.yml”文件中获取 URL 端点详细信息作为变量? - How to get URL endpoint detail as variable in Serverless Framework's `serverless.yml` file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM