title: AWS CloudFormation UserData EC2 Environment Variable
body: <p>I'm working on an infrastructure with CloudFormation.
  That is my own infrastructure code shown below .</p>
  <pre class="lang-yaml prettyprint-override"><code> AWSRegionArch2AMI:
       us-east-1:
         HVM64: ami-0ff8a91507f77f867
         HVMG2: ami-0a584ac55a7631c0c
   ...
   ..
   .
  
   Resources:
   EC2Instance:
   Type: AWS::EC2::Instance
   Properties:
   InstanceType:
      Ref: InstanceType
    SecurityGroups:
    - Ref: InstanceSecurityGroup
    KeyName:
      Ref: KeyName
    UserData:
      Fn::Base64: !Sub |
        #!/bin/bash
        export MY_AMI_NAME=${ !GetAtt AWSRegionArch2AMI.us-east-1.HVM64 }
  
        echo $MY_AMI_NAME &gt; $HOME/user_data.txt
  </code></pre>
  <p>I want to set the variable to the user_data file but it is empty, How I can get the environment variable to inside my user data field and use it my own application side How I can do it.</p>
  <p>Please help !</p>