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 Antwort
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
EXPERTE
Greg_B
beantwortet vor 2 Jahren
profile pictureAWS
EXPERTE
Chris_G
überprüft vor 2 Jahren
  • 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".....
    

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen