Search code examples
aws-cloudformation

AWS cloudformation: Fn:Base64:: Unable to handle special characters


I have created a cloud formation script

# in mumbai region (working)
Resources:
  MyNewEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0fcbc9ed97efe271e
      InstanceType: t2.micro
      SubnetId: subnet-fc2df495
      KeyName: sant-test
      UserData:
        Fn::Base64:
          Fn::Sub: |
          #!/bin/bash -xe
          yum update -y
          echo somenthing:khskhsdh >> test.txt

this is throwing error at echo somenthing:khskhsdh >> test.txt

enter image description here

i get error something like:

enter image description here

How to handle special characters


Solution

  • I looked up all my templates and found an example that works 100%:

    Resources:
      MyNewEC2Instance:
        Type: AWS::EC2::Instance
        Properties:
          ImageId: ami-0fcbc9ed97efe271e
          InstanceType: t2.micro
          SubnetId: subnet-fc2df495
          KeyName: sant-test
          UserData:
            Fn::Base64: |-
              #!/bin/bash
              yum update -y
              echo somenthing:khskhsdh >> test.txt
    

    The Fn::Sub: in the question is redundant and causing an error.