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:
$ aws s3 ls my-bucket
2020-03-03 11:00:46 4100 myfile.json
But, when I try to use boto3 to write to the bucket, it fails:
session = boto3.session.Session()
s3_client = session.client('s3')
s3_client.upload_file(path, 'my-bucket', 'myfile.json')
I get this error:
botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
How do I tell boto3 to use the EC2 instance's IAM profile, which should give it permission?