title: How do I use the IAM role assigned to an ec2 instance with boto3?
body: <p>I'm trying to run a python script on an EC2 instance that is meant to upload files to s3. I have created an IAM role that has full permissions to access and alter that S3 bucket. If I run an aws cli command on the ec2 instance, it works fine, for example:</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ aws s3 ls my-bucket
  2020-03-03 11:00:46       4100 myfile.json
  </code></pre>
  
  <p>But, when I try to use boto3 to write to the bucket, it fails:</p>
  
  <pre class="lang-py prettyprint-override"><code>session = boto3.session.Session()
  s3_client = session.client('s3')
  s3_client.upload_file(path, 'my-bucket', 'myfile.json')
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
  </code></pre>
  
  <p>How do I tell boto3 to use the EC2 instance's IAM profile, which should give it permission?</p>