title: AWS CloudFormation: Nested Sub with Dynamic References using {{resolve}} causes error and doesn't execute resolve to get value from Parameter Store
body: <p>I am trying to use AWS CloudFormation Template to create an EC2 Instance with some userdata generated using dynamic references and cross-stack reference in the template . There is a parameter stored in AWS Systems Manager Parameter Store with <code>Name:/MyCustomParameter</code> and <code>Value:Test1</code>.</p>
  
  <p>The idea is to pass a parameter to the template stack (Stack A) which refers to another cloudformation stack (StackB). Stack B exports a variable with reference "StackB::ParameterStoreName". Stack A uses <code>Fn::ImportValue: 'StackB::ParameterStoreName'</code> to get it's value so that it can be used with dynamic references method to get it's value from AWS SSM Parameter Store using <code>{{resolve:ssm:/MyCustomParameter:1}}</code> and pass it's value to the UserData field in the template. I am facing difficulties while trying to use nested <code>Fn::Sub:</code> function with this use-case. </p>
  
  <p>I tried removing the <code>|</code> pipe and using double quotes with escaped new line character but that doesn't work. </p>
  
  <p>I also tried using a different type of resource and it's properties where is worked. Below is an example of the code that worked.</p>
  
  <pre><code>Resources:
    TestBucket:
      Type: 'AWS::S3::Bucket'
      Properties:
        BucketName: 
          Fn::Sub:
            - '${SSMParameterValue}-12345'
            - SSMParameterValue: 
                Fn::Sub:
                  - '{{resolve:ssm:${SSMParameterName}:1}}'
                  - SSMParameterName: 
                      Fn::ImportValue:
                        !Sub '${CustomStack}::ParameterStoreName'
  </code></pre>
  
  <p>Below is an extract of the current code I have:</p>
  
  <pre><code>Parameters:
    CustomStack:
      Type: "String"
      Default: "StackB"
  Resources:
    MyCustomInstance:  
      Type: 'AWS::EC2::Instance'
          Properties:
          UserData:
              Fn::Base64:
              Fn::Sub:
                  - |
                  #!/bin/bash -e 
                  #
                  # Bootstrap and join the cluster 
                  /etc/eks/bootstrap.sh --b64-cluster-ca '${SSMParameterValue}' --apiserver-endpoint '${Endpoint}' '${ClusterName}'"
                  - SSMParameterValue:
                      Fn::Sub:
                      - '{{resolve:ssm:/${SSMParameterName}:1}}'
                      - SSMParameterName: 
                          Fn::ImportValue:
                              !Sub '${CustomStack}::ParameterStoreName'
                  Endpoint:
                      Fn::ImportValue:
                      !Sub '${CustomStack}::Endpoint'
                  ClusterName: 
                      Fn::ImportValue:
                      !Sub '${CustomStack}::ClusterStackName'
  </code></pre>
  
  <p>Current Output:</p>
  
  <pre><code>#!/bin/bash -e 
  # 
  # Bootstrap and join the cluster 
  /etc/eks/bootstrap.sh --b64-cluster-ca `{{resolve:ssm:MyCustomParameter:1}}` --apiserver-endpoint 'https://04F1597P0HJ11FQ54K0YFM9P19.gr7.us-east-1.eks.amazonaws.com' 'eks-cluster-1'
  </code></pre>
  
  <p>Expected Output:</p>
  
  <pre><code>#!/bin/bash -e 
  # 
  # Bootstrap and join the cluster 
  /etc/eks/bootstrap.sh --b64-cluster-ca `Test1` --apiserver-endpoint 'https://04F1597P0HJ11FQ54K0YFM9P19.gr7.us-east-1.eks.amazonaws.com' 'eks-cluster-1'
  </code></pre>