Skip to content

Instantly share code, notes, and snippets.

@Karunamon
Created February 8, 2019 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Karunamon/16b0ec04f6131b4ac8395745a60f7ae3 to your computer and use it in GitHub Desktop.
Save Karunamon/16b0ec04f6131b4ac8395745a60f7ae3 to your computer and use it in GitHub Desktop.
Boto3 with IAM roles
#!/usr/bin/env python3
import boto3
sts = bot3.client('sts')
role = sts.assume_role(
RoleArn="arn:aws:iam::ACCOUNT NUMBER:role/ROLENAME",
RoleSessionName="AssumeRoleSession1"
)
credentials = assumed_role_object['Credentials']
s3 = boto3.resource(
's3',
aws_access_key_id=credentials['AccessKeyId'],
aws_secret_access_key=credentials['SecretAccessKey'],
aws_session_token=credentials['SessionToken'],
)
for bucket in s3_resource.buckets.all():
print(bucket.name)
# use S3 appropriately from here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment