How to specify JSON-formatted string in Cloudformation?

cloudformation !sub
cloudformation multiline string
cloudformation variables
cloudformation yaml json object
cloudformation functions
cloudformation join
yaml define json string
cloudformation parameters

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?

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

Learn Template Basics - AWS CloudFormation, Found out the answer myself: "Input": "{ \"test\" : \"value11\", \"test2\" : \"value22\"}". Hope it helps someone else. Template Anatomy A template is a JSON- or YAML-formatted text file that describes your AWS infrastructure. The following examples show an AWS CloudFormation template structure and its sections.

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

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

Fn::Sub - AWS CloudFormation, The template is stored as a text file whose format complies with the JavaScript Object Notation such as the AccessControl property of the AWS::S3::Bucket resource, so specifying a value The Description is any valid JSON or YAML string. The intrinsic function Fn::Join appends a set of values into a single value, separated by the specified delimiter. If a delimiter is the empty string, the set of values are concatenated with no delimiter.

This should work:

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

Fn::JsonString · Issue #78 · aws-cloudformation/aws-cloudformation , Whenever a resource has property that requires content as JSON String, we all /questions/39041209/how-to-specify-json-formatted-string-in-cloudformation. Dynamic references provide a compact, powerful way for you to specify external values that are stored and managed in other services, such as the Systems Manager Parameter Store, in your stack templates. When you use a dynamic reference, CloudFormation retrieves the value of the specified reference when necessary during stack and change set

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

How to specify JSON-formatted string in Cloudformation?, 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. String. A literal string. For example, users could specify "MyUserName".. Number. An integer or float. AWS CloudFormation validates the parameter value as a number; however, when you use the parameter elsewhere in your template (for example, by using the Ref intrinsic function), the parameter value becomes a string.

How to convert CloudFormation JSON to YAML, going to take an AWS CloudFormation file written in JSON and then convert that file to YAML format. Select “Design Template”. Click “Template” at the bottom. Paste in the following CloudFormation template Type: String If you set the NoEcho attribute to true, CloudFormation returns the parameter value masked as asterisks (*****) for any calls that describe the stack or stack events. In the example above, the WordPressUser parameter value is not visible to anyone viewing the stack's settings, and its value is returned as asterisks.

How and Why to Use CloudFormation Macros, How to set up a CloudFormation macro. An example of writing a CloudFormation template macro to add Python-style string formatting to your CloudFormation import json def variable_substitution(event, context): context  Update. Bamboo does not parse variable content in any way, i.e. the content is always just a string. The problem you are encountering is passing a JSON string as a parameter to AWS CloudFormation via the AWS CloudFormation Stack task, which expects those parameters to be passed into the Parameters field as a JSON string in turn, e.g.:

Cloud Templating with AWS CloudFormation with Examples, CloudFormation templates are JSON or YAML files that describe in depth every AWS resource needed, its configuration, and its relation to the “Description” : “​Any JSON string”, “Description” : “Accessing IP (in CIDR format, e.g.: 1.2.3.4/24​)”, Mappings are used to reference a key to a set of values. 76 Getting Outputs from aws cloudformation describe-stacks Jan 13 '17 44 Getting Outputs from aws cloudformation describe-stacks Jan 13 '17 19 How to specify all ports in Security group - CloudFormation Aug 18 '16

Comments
  • Feature request: github.com/aws-cloudformation/…
  • THANKS !! that helps
  • Does this work in Cloudformation template as well @Pau? Any requirements?
  • Yeah, It works. You just would use YAML instead of JSON.
  • This doesn't work in cloud formation, but you can use the JSON.stringify() function in a browser console to convert to the format needed for the CFN Text field. imgur.com/a/JNXxnhb
  • It looks like he was asking how you pass JSON in a string field in a CFN template. It would need to be stringified, for example with the JavaScript function JSON.Stringify()