Skip to content

Instantly share code, notes, and snippets.

@sebsto
Last active December 16, 2019 17:51
Show Gist options
  • Save sebsto/9a958ff1c761b8c7c90d to your computer and use it in GitHub Desktop.
Save sebsto/9a958ff1c761b8c7c90d to your computer and use it in GitHub Desktop.
Create IAM User and Attach a Policy using Boto and JSON
import json, boto
# Connect to IAM with boto
iam = boto.connect_iam(ACCESS_KEY, SECRET_KEY)
# Create user
user_response = iam.create_user('aws-user')
# Create Policy
policy = { 'Version' : '2012-10-17'}
policy['Statement'] = [{'Sid' : 'AwsIamUserPython',
'Effect': 'Allow',
'Action': 's3:*',
'Resource': 'arn:aws:s3:::class-rocks/*'}]
policy_json = json.dumps(policy, indent=2)
iam.put_user_policy('aws-user', 'allow_access_class-rocks', policy_json)
# Generate new access key pair for 'aws-user'
key_response = iam.create_access_key('aws-user')
@vbasavar
Copy link

@stormm2138

policy['Statement'][0]['Resource']=['arn:aws:s3:::class-rocks','arn:aws:s3:::class-rocks/*']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment