【问题标题】:Serverless lambda Global Environmental Variables无服务器 lambda 全局环境变量
【发布时间】:2021-11-17 02:22:11
【问题描述】:

我正在玩无服务器,我正在尝试弄清楚如何重写这个 serverless.yml 文件,这样我就不会为每个函数复制环境变量。有没有办法全局设置环境变量?

service: test-api
frameworkVersion: ">=1.2.0 <2.0.0"
provider:
  name: aws
  runtime: nodejs12.x
  timeout: 30
  stage: dev
  memorysize: 2048
  region: us-east-2
  logRetentionInDays: 21

functions:
  doCreate:
    handler: functions/do-create.handler
    environment:
      DB_PORT: ${ssm:/${self:custom.stage}/db_port}
      DB_URL: ${ssm:/${self:custom.stage}/db_url}
      API_KEY: ${ssm:/${self:custom.stage}/api_key}
      ENV: "${self:custom.stage}"
      SEARCH_ARN: ${ssm:/${self:custom.stage}/search_arn}
  doUpdate:
    handler: functions/do-update.handler
    environment:
      DB_PORT: ${ssm:/${self:custom.stage}/db_port}
      DB_URL: ${ssm:/${self:custom.stage}/db_url}
      API_KEY: ${ssm:/${self:custom.stage}/api_key}
      ENV: "${self:custom.stage}"
      SEARCH_ARN: ${ssm:/${self:custom.stage}/search_arn}

【问题讨论】:

    标签: amazon-web-services aws-lambda serverless-framework serverless


    【解决方案1】:

    使用 SAM 模板中的 Globals 部分

    Globals:
      Function:
        Runtime: nodejs12.x
        Timeout: 180
        Handler: index.handler
        Environment:
          Variables:
            TABLE_NAME: data-table
    

    更多详情请浏览此文档https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html

    【讨论】:

    • 您有如何将其转换为 serverless.yml 的示例吗?
    【解决方案2】:

    您只需将它们移至provider 部分即可。它们将应用于同一服务中的所有功能。

    service: test-api
    frameworkVersion: ">=1.2.0 <2.0.0"
    provider:
      name: aws
      runtime: nodejs12.x
      timeout: 30
      stage: dev
      memorysize: 2048
      region: us-east-2
      logRetentionInDays: 21
      environment:
        DB_PORT: ${ssm:/${self:custom.stage}/db_port}
        DB_URL: ${ssm:/${self:custom.stage}/db_url}
        API_KEY: ${ssm:/${self:custom.stage}/api_key}
        ENV: "${self:custom.stage}"
        SEARCH_ARN: ${ssm:/${self:custom.stage}/search_arn}
    functions:
      doCreate:
        handler: functions/do-create.handler
      doUpdate:
        handler: functions/do-update.handler
    

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      相关资源
      最近更新 更多