【问题标题】:Cloudformation - environment variable used when condition is metCloudformation - 满足条件时使用的环境变量
【发布时间】:2022-01-22 02:36:40
【问题描述】:

我需要根据我的 lambda 函数的环境传递不同的变量。在以下条件下创建,但在尝试使用 !If 时出错。感谢有关如何解决此问题的所有帮助和建议。

部署时出错:

Properties validation failed for resource LambdaFunction

YML 文件:

Conditions:
  IsProd: !Equals [!Ref AppEnv, "production"]
  IsNonProd: !Not [!Equals [!Ref AppEnv, "production"]]

  LambdaFunction:
    Type: AWS::Lambda::Function
    DependsOn:
      - LambdaLayer
    Properties:
      FunctionName: !Sub '${AppName}-${AppEnv}'
      Handler: function.handler
      Layers:
        - !Ref LambdaLayer
      VpcConfig:
        SecurityGroupIds:
          - Ref: SecurityGroup
        SubnetIds:
          - Ref: PrivateSubnetAz1
          - Ref: PrivateSubnetAz2
      Code:
        S3Bucket: !Sub 'app-deploy-${AWS::AccountId}-${AWS::Region}'
        S3Key: !Ref S3LambdaKey
      Environment:
        Variables:
          !If
            - "IsNonProd"
            - 
              - DD_SITE: !GetAtt EncryptedDDSite.CipherText
              - DD_API_KEY: !GetAtt EncryptedDDApiKey.CipherText
              - USER_DEV: !GetAtt EncryptedUserDev.CipherText
              - PASS_DEV: !GetAtt EncryptedPassDev.CipherText
              - USER_STG: !GetAtt EncryptedUserStg.CipherText
              - PASS_STG: !GetAtt EncryptedPassStg.CipherText
              - USER_TEST: !GetAtt EncryptedUserTest.CipherText
              - PASS_TEST: !GetAtt EncryptedPassTest.CipherText
              - AppEnv: !Ref AppEnv
            - 
              - DD_SITE: !GetAtt EncryptedDDSite.CipherText
              - DD_API_KEY: !GetAtt EncryptedDDApiKey.CipherText
              - USER_PRD: !GetAtt EncryptedUserPrd.CipherText
              - PASS_PRD: !GetAtt EncryptedPassPrd.CipherText
              - AppEnv: !Ref AppEnv
      Runtime: python3.8
      MemorySize: 128
      Timeout: 300
      Role: !GetAtt
        - LambdaRole
        - Arn

【问题讨论】:

  • 你得到哪个错误?
  • Json parsing exception message = Unrecognized token 'AWSTemplateFormatVersion': was expecting ('true', 'false' or 'null') at [ Yaml parsing exception message = while parsing a block mapping in 'reader', line 339, column 9: Variables: ^expected <block end>, but found '<block mapping start>' in 'reader', line 356, column 11: AppEnv: !Ref AppEnv ^ at [
  • 模板的 parameters 部分有什么内容?

标签: amazon-web-services amazon-cloudformation


【解决方案1】:

!If 需要一个数组,第二个和第三个项目也应该是一个数组。 AppEnv 也必须添加两次。

  Environment:
    Variables:
      !If:
        - "IsNonProd"
        - 
          - DD_SITE: !GetAtt EncryptedDDSite.CipherText
          - DD_API_KEY: !GetAtt EncryptedDDApiKey.CipherText
          - USER_DEV: !GetAtt EncryptedUserDev.CipherText
          - PASS_DEV: !GetAtt EncryptedPassDev.CipherText
          - USER_STG: !GetAtt EncryptedUserStg.CipherText
          - PASS_STG: !GetAtt EncryptedPassStg.CipherText
          - USER_TEST: !GetAtt EncryptedUserTest.CipherText
          - PASS_TEST: !GetAtt EncryptedPassTest.CipherText
          - AppEnv: !Ref AppEnv
        - 
          - DD_SITE: !GetAtt EncryptedDDSite.CipherText
          - DD_API_KEY: !GetAtt EncryptedDDApiKey.CipherText
          - USER_PRD: !GetAtt EncryptedUserPrd.CipherText
          - PASS_PRD: !GetAtt EncryptedPassPrd.CipherText
          - AppEnv: !Ref AppEnv

【讨论】:

  • 遇到类似错误:Json parsing exception message = Unrecognized token 'AWSTemplateFormatVersion': was expecting ('true', 'false' or 'null') at [ Yaml parsing exception message = while parsing a block mapping in 'reader', line 338, column 9: Variables: ^expected <block end>, but found '<block mapping start>' in 'reader', line 355, column 11: AppEnv: !Ref AppEnv ^ at [
  • 您需要在两个列表中添加 AppEnv。
  • 这是一个错过。新错误YAML not well-formed. (line 339, column 11)
  • 发布您的新代码。很可能您遇到了缩进问题。
  • 检查事项:名称不超过 64 个字符。存在安全组和子网。 Lambda 层有效。
猜你喜欢
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 2019-09-29
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
相关资源
最近更新 更多