Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass in JSON type into YAML CloudFormation template

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iot1click-placement.html#cfn-iot1click-placement-associateddevices

states that the property AssociatedDevices is a type JSON, but when I am writing the template in YAML

I have read a few responses here and have tried the following:

AssociatedDevices: "--arguments": '{"SecuityButtonTemplate": !Ref TestITPA.DeviceId}'   
PlacementName: "TestITPAPlacement"      
Attributes:  "--arguments": '{"--Location": TestITPALoc}'

(this fails to build)

and this:

  AssociatedDevices: '{"SecuityButtonTemplate": !Ref TestITPA.DeviceId}'   
  PlacementName: "TestITPAPlacement"
  Attributes:  '{"Location":"TestingLoc"}'

(this also fails to build)

I have even search github for YAML code referencing AssociatedDevices but not finding how people are actually doing this - can anyone help me shed some light ?

I lastly have tried this:

     AssociatedDevices: !Sub |
{
    SecuityButtonTemplate: !Ref TestITPA.DeviceId
}   
  PlacementName: "TestITPAPlacement"
  Attributes:  !Sub |
{
    Location: "testingLoc"
}

(this throws what seems to be an IDE error - the middle variable of PlacementName is not longer red like the others)

like image 610
Kevin Avatar asked Oct 15 '25 15:10

Kevin


2 Answers

You can try the following:

AssociatedDevices: !Sub '{"SecuityButtonTemplate": "${TestITPA.DeviceId}"}'  
PlacementName: "TestITPAPlacement"
Attributes: '{"Location":"TestingLoc"}'
like image 128
Marcin Avatar answered Oct 17 '25 08:10

Marcin


Using ToJsonString intrinsic function

You can use Fn::ToJsonString intrinsic function to transform YAML to JSON.

Example:

AssociatedDevices: !ToJsonString
  SecuityButtonTemplate: !GetAtt TestITPA.DeviceId 
PlacementName: TestITPAPlacement
Attributes: !ToJsonString
  Location: TestingLoc

It works with other intrinsic functions (such as Ref, Sub, etc) so you code can be written in full YAML with top readability.

Make sure to add the AWS::LanguageExtensions transform in your template:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::LanguageExtensions
like image 27
Yves M. Avatar answered Oct 17 '25 09:10

Yves M.