【问题标题】:How to specify JSON-formatted string in Cloudformation?如何在 Cloudformation 中指定 JSON 格式的字符串?
【发布时间】:2016-12-26 17:49:34
【问题描述】:

我的 CloudFormation 模板上有以下资源,用于创建运行 Lambda 函数的规则,来自 AWS 文档:

  "ScheduledRule": {
    "Type": "AWS::Events::Rule",
    "Properties": {
    "Description": "ScheduledRule",
    "ScheduleExpression": "rate(5 minutes)",
    "State": "ENABLED",
    "Targets": [{
      "Arn": { "Fn::GetAtt": ["myLambda", "Arn"] },
      "Id": "TargetFunctionV1"
    }]
    }
  }

我想指定输入:

{
  "Arn" : String,
  "Id" : String,
  "Input" : String,
  "InputPath" : String
}

并且 Input 是传递给目标的 JSON 格式的文本字符串。此值会覆盖匹配的事件。

我希望我的 JSON 格式文本是:

{
  "mykey1": "Some Value"
}

我不知道如何在输入中指定它,当我输入时:

  "ScheduledRule": {
    "Type": "AWS::Events::Rule",
    "Properties": {
    "Description": "ScheduledRule",
    "ScheduleExpression": "rate(5 minutes)",
    "State": "ENABLED",
    "Targets": [{
      "Arn": { "Fn::GetAtt": ["myLambda", "Arn"] },
      "Id": "TargetFunctionV1",
      "Input": { "mykey1": "Some Value" }
    }]
    }
  }

我会得到错误:

Value of property Input must be of type String

我应该如何正确指定?

【问题讨论】:

标签: json amazon-cloudformation


【解决方案1】:

我会使用 YAML,因为它更容易阅读:

Input:
  !Sub |
    {
        mykey1: "${myKey}"
    }

【讨论】:

  • 这是否也适用于 Cloudformation 模板@Pau?有什么要求吗?
  • 是的,它有效。您只需使用 YAML 而不是 JSON。
  • 这种技术效果很好,您可以简单地定义您的 json 键/值,而无需任何 json 到字符串的转换。阅读 Mooncraters 对使用 !Sub 的评论
【解决方案2】:

自己找到了答案:

"Input": "{ \"test\" : \"value11\", \"test2\" : \"value22\"}"

希望对其他人有所帮助。

更新:

您基本上使用 JSON.Stringify() 的结果将字符串放入“输入”字段。使用在线 JSON.Stringify() 如https://onlinetexttools.com/json-stringify-text

【讨论】:

    【解决方案3】:

    我想扩展@Pau 的答案。基本上,如果您使用| 表示下面的整个块将被视为原始字符串。

    如果您需要替换 JSON 中的任何变量,则可以使用 Sub,但如果您没有任何变量,则不需要 Sub。一个例子是:

    Input:|
      {
        "jsonVar":"jsonVal",
        "jsonVar2" : "jsonVal2"
      }
    

    您可以稍后通过JSON.parse(<input-variable>) 获取 JSON 对象。

    注意:如果没有下一个变量,请不要在 JSON 中的变量末尾添加逗号。示例:

    Input:|
      {
        "jsonVar":"jsonVal",
        "jsonVar2" : "jsonVal2",
      }
    

    这会导致 JSON 解析错误。

    【讨论】:

      【解决方案4】:

      如果您在 yaml 中编写 CloudFormation 脚本并发现难以使用 JSON 字符串(例如策略文档),最简单的方法是使用 online converter 将 JSON 转换为 yaml

      ApiGatewayRestApi:
          Type: AWS::ApiGateway::RestApi
          Properties:
            Description: API Gateway for some API
            EndpointConfiguration:
              Types:
                - PRIVATE
            Name: MyAPIGateway 
            Policy: <Policy Doc >
      

      假设政策文档如下。

      {
      "Version": "2012-10-17",
      "Statement": [
          {
              "Effect": "Allow",
              "Principal": "*",
              "Action": "execute-api:Invoke",
              "Resource": "arn:aws:execute-api:ap-southeast-2:something/*",
              "Condition": {
                  "ForAnyValue:StringEquals": {
                      "aws:sourceVpce": "vpce-abcd"
                  }
              }
          }
      ]
      }
      

      使用转换器可以将 JSON 转换为相同的 yaml 并按如下方式使用。

      ApiGatewayRestApi:
          Type: AWS::ApiGateway::RestApi
          Properties:
            Description: API Gateway for some API
            EndpointConfiguration:
              Types:
                - PRIVATE
            Name: MyAPIGateway 
            Policy: 
              Version: '2012-10-17'
              Statement:
              - Effect: Allow
                Principal: "*"
                Action: execute-api:Invoke
                Resource: arn:aws:execute-api:ap-southeast-2:something/*
                Condition:
                  ForAnyValue:StringEquals:
                    aws:sourceVpce: vpce-abcd
      

      【讨论】:

      • 看起来他在问你如何在 CFN 模板的字符串字段中传递 JSON。它需要被字符串化,例如使用 JavaScript 函数 JSON.Stringify()
      • 非常感谢我的朋友,我一直在寻找这样的东西。这比在 yaml 文件中使用 json 要简洁得多。
      【解决方案5】:

      这是我用于“输入”行的类似 YAML 代码。我花了一整天的时间才弄清楚语法,所以我希望这对将来的人有所帮助。

      Input: "{\"action\":[\"configure\"],\"mode\":[\"ec2\"],\"optionalConfigurationSource\":[\"ssm\"],\"optionalConfigurationLocation\":[\"AmazonCloudWatch-Baseline-Windows\"],\"optionalRestart\":[\"yes\"]}"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-18
        • 1970-01-01
        相关资源
        最近更新 更多