CloudFormation - Creating a Provisioning Template - Template Body JSON contains 'Ref'

0

I am attempting to use Cloud Formation to create fleet provisioning template. however the JSON used for the provisioning template uses 'Ref' as a key and is cloudformation is using this as the function 'Ref'

my fleet provisioning template looks like:

{
  "Parameters": {
    "ThingName": {
      "Type": "String"
    },
    "ThingGroupName": {
      "Type": "String"
    }
  },
  "Resources": {
    "MyThing": {
      "OverrideSettings": {
        "AttributePayload": "REPLACE",
        "ThingGroups": "REPLACE",
        "ThingTypeName": "REPLACE"
      },
      "Properties": {
        "AttributePayload": {},
        "ThingGroups": [{
          "Ref": "ThingGroupName"
        }],
        "ThingName": {
          "Ref": "ThingName"
        }
      },
      "Type": "AWS::IoT::Thing"
    },
    "MyPolicy": {
      "Properties": {
        "PolicyName": "Prefix-FleetOperator-ProvTemp"
      },
      "Type": "AWS::IoT::Policy"
    },
    "MyCertificate": {
      "Properties": {
        "CertificateId": {
          "Ref": "AWS::IoT::Certificate::Id"
        },
        "Status": "Active"
      },
      "Type": "AWS::IoT::Certificate"
    }
  }
}

and a snippet of my cloud formation looks like

  FleetProvisioningTemplate:
    Type: 'AWS::IoT::ProvisioningTemplate'
    Properties:
      Description: Prov Template
      Enabled: true
      PreProvisioningHook:
        TargetArn: >-
          arn:aws:lambda:
      ProvisioningRoleArn: !GetAtt FleetProvisioningRole.Arn
      TemplateBody:
          Fn::ToJsonString:
            Parameters:
              ThingName:
                Type: String
              ThingGroupName:
                Type: String
            Resources:
              MyThing:
                OverrideSettings:
                  AttributePayload: REPLACE
                  ThingGroups: REPLACE
                  ThingTypeName: REPLACE
                Properties:
                  AttributePayload: {}
                  ThingGroups:
                  - Ref: ThingGroupName
                  ThingName:
                    Ref: ThingName
                Type: AWS::IoT::Thing
              MyPolicy:
                Properties:
                  PolicyName: !Sub ${Prefix}-${FleetOperator}-IoTThingPolicy
                Type: AWS::IoT::Policy
              MyCertificate:
                Properties:
                  CertificateId:
                    Ref: AWS::IoT::Certificate::Id
                  Status: Active
                Type: AWS::IoT::Certificate
      TemplateName: !Sub ${Prefix}-${FleetOperator}-ProvTemp
    Metadata:
      'AWS::CloudFormation::Designer':
        id: 841d6a68-6dc0-4a56-8dd8-e784ca97dc79

I receive the following error when trying to create the stack:

Template format error: Unresolved resource dependencies [ThingName, AWS::IoT::Certificate::Id, ThingGroupName] in the Resources block of the template

How do I get cloud formation to pass "Ref" in the template as a string and not as a function?

1 Answer
1

Hi Phil. I think you need a literal block in the YAML, so the JSON provisioning template is taken as a string. Note the pipe character after TemplateBody: statement.

      TemplateBody: |
		{
		  "Parameters": {
			"ThingName": {
			  "Type": "String"
			},
			"ThingGroupName": {
			  "Type": "String"
			}
		  },
                  etc
profile pictureAWS
EXPERT
Greg_B
answered 2 years ago
profile pictureAWS
EXPERT
Chris_G
reviewed 2 years ago
  • I looked into this further and as mentioned above you can add the literal JSON Block using the '|' pipe character. I ran into further trouble when I wanted to use variables with in the fleet provisioning template. My solution was using the Join Function like this:

          TemplateBody: !Join
            - ''
            - - >-
                {"Parameters":{"ThingName":{"Type":"String"},"ThingGroupName":{"Type":"String","Default":
                "
              - !Sub '${Prefix}_${FleetOperator}_Group'
              - >-
                "},"serialNumber".....
    

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions