title: I am not able to copy S3 file to EC2 instance using Userdata in CloudFormation
body: <p>I am unable to download an S3 file to my EC2 instance using CloudFormation Userdata property. I  have assigned an IAM role but still not able to get it resolved.</p>
  
  <p>I assigned the role inside the template.</p>
  
  <p>I tried passing Access Key and Secret Access Key - same result.</p>
  
  <pre><code>"Parameters": {       
      "VpcId": {
          "Type": "AWS::EC2::VPC::Id",
          "Description": "Id of an existing VPC to use for "
      },
      "SubnetId": {
          "Type": "AWS::EC2::Subnet::Id",
          "Description": "Id of an existing subnet id to use for "
      },
  
      "SecurityGroupIds": {
          "Description": "Security groups ",
          "Type": "List&lt;AWS::EC2::SecurityGroup::Id&gt;",
          "ConstraintDescription": "using existing security  be list of EC2 security group ids"
      },
      "instanceType": {
          "Type": "String",
          "Default": "t2.micro",
          "AllowedValues": [
              "t2.micro"
  
          ],
          "Description": "Enter Instance Type "
      },
      "AWSREGION": {
          "Type": "String",
          "Default": "us-east-1",
          "AllowedValues": [
              "us-east-1"
          ],
          "Description": "Enter AWS_REGION."
      }
  },
  "Resources": {
      "InstanceRole": {
        "Type": "AWS::IAM::Role",
        "Properties": {
          "AssumeRolePolicyDocument": {
            "Version": "2012-10-17",
            "Statement": [
              {
                "Effect": "Allow",
                "Principal": { "Service": [ "ec2.amazonaws.com" ] },
                "Action": [ "sts:AssumeRole" ]
              }
            ]
          },
          "Path": "/",
          "Policies": [
            { 
              "PolicyName": "S3_Access",
              "PolicyDocument": {
                "Statement": [
                  {
                    "Effect": "Allow",
                   "Action": [
                      "s3:PutObject",
                      "s3:GetObject",
                      "s3:DeleteObject"
                    ],
                    "Resource": ["arn:aws:s3:::mybucketlocation/*"]
                  }
                ]
              }
            }
          ]
        }
      },
      "InstanceProfile": {
        "Type": "AWS::IAM::InstanceProfile",
        "Properties": {
          "Path": "/",
          "Roles": [ { "Ref": "InstanceRole" }
          ]
        }
      },
      "EdgeNode": {
          "Type": "AWS::EC2::Instance",
          "Properties": {
              "IamInstanceProfile": { "Ref": "InstanceProfile" },
              "InstanceType": { "Ref" : "instanceType" },
              "ImageId": "ami-0cc96feef8c6bbff3",
              "SubnetId": { "Ref" : "SubnetId" },
              "KeyName": "my-key",
              "SecurityGroupIds": {
                  "Ref": "SecurityGroupIds"
              },
              "UserData": {
                  "Fn::Base64": {
                      "Fn::Join": [
                          "\n",
                          [
                              "#!/bin/bash",
                              "echo \"\" &gt; /home/xyz/index.txt", 
                              {
                                  "Fn::Join": [
                                      "",
                                      [
                                          "echo \"AWS_REGION: ",
                                          {
                                              "Ref": "AWSREGION"
                                          },
                                          "\" &gt;&gt; /home/xyz/index.txt"
                                      ]
                                  ]
  
                              },
                              {
                                   "Fn::Join": ["", [
                                      "&lt;script&gt;\n",
                                      "cfn-init.exe -v -s ", { "Ref" : "AWS::StackId" }, " -r Instance --region ", { "Ref" : "AWS::Region" }, "\n",
                                      "&lt;/script&gt;"
                                    ] ] 
                              }
  
  
  
                          ]
                      ]
  
                  }
              }
          },
  
          "Metadata": {
              "AWS::CloudFormation::Init": {
                "config": {
                  "commands" : {
                    "Pullcode" : {
                      "command" : "aws s3 sync s3://mybucketlocation /home/xyz/ --debug"
                    }
                  }
                } 
              },
              "AWS::CloudFormation::Designer": {
                  "id": "e37a9183-9f81c2fbd39"
  
  
              }
          }
      }
  }
  </code></pre>
  
  <p>In <code>cloud-init-output.log</code> I got this:</p>
  
  <blockquote>
    <p>/var/lib/cloud/instance/scripts/part-001: line 7: syntax error near unexpected token <code>newline'
    /var/lib/cloud/instance/scripts/part-001: line 7:</code>'
    Jun 21 11:45:05 cloud-init[4071]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [2]
    Jun 21 11:45:05 cloud-init[4071]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
    Jun 21 11:45:05 cloud-init[4071]: util.py[WARNING]: Running module scripts-</p>
  </blockquote>