How to specify JSON-formatted string in Cloudforma

2020-08-10 06:03发布

问题:

I have the following resource on my CloudFormation template to create a rule to run a Lambda function, from the AWS documentation:

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

I would like to specify the Input:

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

and Input is a JSON-formatted text string that is passed to the target. This value overrides the matched event.

I would like my JSON formatted text to be:

{
  "mykey1": "Some Value"
}

I do not know how to specify it in the Input, when I put:

  "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" }
    }]
    }
  }

I will get error:

Value of property Input must be of type String

How should I specify it correctly?

回答1:

Found out the answer myself:

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

Hope it helps someone else.

Update:

You basically use the result of JSON.Stringify() to get the string into "Input" field. Use online JSON.Stringify() like https://onlinetexttools.com/json-stringify-text



回答2:

I would use YAML as it is easier and more readable:

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


回答3:

This should work:

"Input": JSON.stringify({ mykey1: "Some Value" })


回答4:

If you are writing your CloudFormation scripts in yaml and finding it difficult to use a JSON string (such as a policy doc) the easiest way is to convert your JSON into yaml using an online converter

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

Lets say the policy doc is as follows.

{
"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"
            }
        }
    }
]
}

Using the converter you can convert the JSON into an identical yaml and use as follows.

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