【问题标题】:Template error: every value of the context object of every Fn::Sub object must be a string or a function that returns a string模板错误:每个 Fn::Sub 对象的上下文对象的每个值都必须是字符串或返回字符串的函数
【发布时间】:2021-06-12 18:14:37
【问题描述】:

当我在 uat env 中运行此模板并作为字符串“vpc-1234”时,我希望将 aws:SourceVpc 添加为字符串列表 ["vpc-7830jkd", "vpc-a1236"] 当我在 perf 中运行时。它在 perf env 中运行良好,但是当我在 uat 中运行时出现以下错误。

模板错误:每个 Fn::Sub 对象的上下文对象的每个值都必须是字符串或返回字符串的函数。有什么建议吗?

这可以通过结合 select、join 和 findinmap 来实现吗?

 Mappings:
  mVpcId:
   menv:
    perf: "vpc-1234"
    uat: "vpc-7830jkd,vpc-a1236"
 
 islowenv: !Equals [ !Ref Env, "perf" ]
 
 Parameters:
   Env:
    Type: String
 
 Resources:
 apigateway:
   Type: "AWS::ApiGateway::RestApi"
     Properties:
    Name: mygateway
   EndpointConfiguration:
     Types:
       - "PRIVATE"
   Policy: !Sub
     - |-
       {
         "Version": "2012-10-17",
         "Statement": [
           {
             "Effect": "Allow",
             "Principal": "*",
             "Action": "execute-api:Invoke",
             "Resource": [
               "execute-api:/*"
             ]
           },
           {
             "Effect": "Deny",
             "Principal": "*",
             "Action": "execute-api:Invoke",
             "Resource": [
               "execute-api:/*"
             ],
             "Condition": {
               "StringNotEquals": {
                 "aws:SourceVpc": "${myappid}"    --> i need this as list when run in uat
               }
             }
           }
         ]
       }
    - { myappid: !If [islowenv, !FindInMap [ "mVpcId", "menv",  !Ref "Env" ], !Split [ ",", !FindInMap [ "mVpcId", "menv",  !Ref "Env"] ]]}

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    由于您现在有条件并且您的 vpc 列表是硬编码的,您可以使用 SelectSub 的以下组合来生成有效的策略:

    Mappings:
      mVpcId:
       menv:
        perf: "vpc-1234"
        uat: "vpc-7830jkd,vpc-a1236"
    
    Conditions:
     islowenv: !Equals [ !Ref Env, "perf" ]
    
    Parameters:
       Env:
        Type: String
        AllowedValues: [perf,uat]
    
    Resources:
    
     apigateway:
       Type: "AWS::ApiGateway::RestApi"
       Properties:
        Name: mygateway
        EndpointConfiguration:
          Types:
            - "PRIVATE"
        Policy: !Sub
            - |-
              {
                "Version": "2012-10-17",
                "Statement": [
                {
                    "Effect": "Allow",
                    "Principal": "*",
                    "Action": "execute-api:Invoke",
                    "Resource": [
                    "execute-api:/*"
                    ]
                },
                {
                    "Effect": "Deny",
                    "Principal": "*",
                    "Action": "execute-api:Invoke",
                    "Resource": [
                    "execute-api:/*"
                    ],
                    "Condition": {
                    "StringNotEquals": {
                        "aws:SourceVpc": ${myappid}
                    }
                   }
                }
                ]
              }
            - myappid:
               !If
                 - islowenv
                 - !Sub
                   - "\"${value}\""
                   - value: !FindInMap ["mVpcId", "menv",  !Ref "Env" ]
                 - !Sub
                   - "[\"${value1}\", \"${value2}\"]"
                   - value1: !Select [0, !Split [ ",", !FindInMap [ "mVpcId", "menv",  !Ref "Env"] ]]
                     value2: !Select [1, !Split [ ",", !FindInMap [ "mVpcId", "menv",  !Ref "Env"] ]]
    

    但如果您需要它来处理任何长度的任何 vpc 列表,那么您需要自定义资源或宏。

    【讨论】:

    • 嗨..感谢您的时间和建议。但它没有用,并给了我以下错误。无效的政策文件。请检查策略语法并确保主体有效。 (服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException;请求 ID:xxxxxxxxxx;代理:null)。尝试转换为 yaml 相同的错误
    • @wlg 模板有效。我验证过了。我猜你改变了什么?
    • 模板语法有效我已经使用设计器进行了验证。但是当在 AWS 中部署时,它花了 30 分钟,超时并给了我这个错误。 .无效的政策文件。请检查策略语法并确保 Principals 有效
    • @Marcin..我没有更改模板中的任何内容。我对 uat 和 perf 有同样的错误。
    • @Marcin..你是最棒的,让我开心。修复缩进模板部署成功后。非常感谢
    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 2016-03-21
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多