簡體   English   中英

成雲模板中如何根據階段設置環境變量

[英]How to set environment variables according to stage in cloud formation template

lambda 處理程序中的環境變量必須根據階段通過 lambda 處理程序設置。 對於不同的階段,schema、endpoint 的值是不同的。 這如何通過 yml 模板完成。 我是新手,所以不知道這將如何完成。

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

如何進一步繼續?

我想應該是:

Environment:
        Variables:
          your-key: !Ref Stage

您可以使用如下條件

MyNotCondition:
  !Not [!Equals [!Ref EnvironmentType, prod]]

模板.yaml:

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

Resources:
  myLambda:
    Properties:
      Environment:
        Variables:
          stage: !Ref Environment

您的 shell:

$ 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]

查看這篇文章,了解如何使用階段參數和使用映射來設置環境變量https://www.singlestoneconsulting.com/blog/cloudformation-mapping-and-conditionals-making-your-templates-more-universal/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM