amazon-web-services - 如何根据云形成模板中的阶段设置环境变量

标签 amazon-web-services aws-lambda aws-cloudformation aws-sam

必须根据阶段通过 lambda 处理程序设置 lambda 处理程序中的环境变量。不同阶段的 schema、endpoint 的值是不同的。如何通过 yml 模板来完成此操作?我对此很陌生,所以不知道如何完成。

Parameters:
   Stage: {Type: String, Default: ''}
Resources:
   LambdaHandler:
   Type: AWS::Serverless::Function
   Properties:
       Environment:
          Variables:
          ......
          ......

如何继续?

最佳答案

模板.yaml:

Parameters:
  Environment:
    AllowedValues:
      - dev
      - prod
    Type: String

Resources:
  myLambda:
    Type: AWS::Lambda::Function
    Properties:
      Environment:
        Variables:
          stage: !Ref Environment

你的外壳:

$ aws cloudformation deploy --parameter-overrides Environment=dev

假设您想要一个以环境为条件的变量:

Parameters:
  Environment:
    AllowedValues:
      - dev
      - prod
    Type: String

Mappings:
  Environments:
    dev:
      LogLevel: "DEBUG"
    prod:
      LogLevel: "ERROR"

Resources:
  myLambda:
    Type: AWS::Lambda::Function
    Properties:
      Environment:
        Variables:
          LOG_LEVEL: !FindInMap [Environments, !Ref Environment, LogLevel]

关于amazon-web-services - 如何根据云形成模板中的阶段设置环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65506398/

相关文章:

java - 使用 aws athena 解析 CSV

node.js - 如何进行DynamoDB细粒度访问控制?

node.js - 有人将 Node.js 与 Amazon SNS 和 Apple 推送通知一起使用吗?

amazon-web-services - 我无法将 Elasticache 连接到 Elastic Beanstalk(在 VPC 中)

amazon-s3 - 如何在 Yaml 中将该变量设为小写?

amazon-web-services - 调用 Lambda 函数的 aws boto3 unittest 函数

amazon-web-services - 强制AWS SQS一条一条地处理消息

amazon-web-services - awscli - lambda 函数更新触发器

amazon-web-services - 尽管可以访问所有 ec2,但无法访问 vpc 端点

amazon-web-services - 为什么我的 CFN IAM 角色失败并显示 "Missing required field Action"?