aws 

Send to Kindle
home » snippets » aws



Users and Groups

Snippets

Creating an instance backed AMI

# From: http://serverfault.com/questions/360882/ebs-backed-ami-become-instance-store-ami-when-migrate-across-region
ec2-bundle-vol \
    -k pk-xxx.pem \
    -u xxx \
    -c cert-xxx.pem

# If you want to change regions.
ec2-migrate-manifest \
    -m /tmp/image.manifest.xml \
    -c cert-xxx.pem \
    -k pk-xxx.pem \
    -a xxx \
    -s xxx \
    --region ap-southeast-1

 ec2-upload-bundle \
    -b my-sg-bucket \
    -m /tmp/image.manifest.xml \
    -a xxx \
    -s xxx \
    --location ap-southeast-1

 ec2-register \
    -K pk-xxx.pem \
    -C cert-xxx.pem \
    --region ap-southeast-1 \
    my-sg-bucket/image.manifest.xml \
    --name my-ami

Manipulating with boto

import getpass
import pprint
pp = pprint.pprint

import ck_3p
import boto.iam

AWS_ID = "..."

# Create a new connection
C = boto.iam.connection.IAMConnection(AWS_ID, getpass.getpass())

# Create the Administrators group.
C.create_group("Administrators")

# Print all existing groups.
pprint(C.get_all_groups())

# Create a group policy for the Administrators group.
policy_json='''
{
   "Statement":[{
      "Effect":"Allow",
      "Action":"*",
      "Resource":"*"
      }
   ]
}
'''
C.put_group_policy("Administrators", "AdministratorGroupPolicy", policy_json)

# Create a new user and add to the Administrators group.
u = C.create_user("ckadmin")
C.add_user_to_group("Administrators", "ckadmin")  # username is a string.

def display_response(response):
    interested_map = response.itervalues().next().itervalues().next().itervalues().next()
    for k, v in interested_map.items():
        print "%s: %s" % (k, v)

# Create access keys for the user.
response = C.create_access_key("ckadmin")
display_response(response)

# git2 user and group

# Create the user.
U = C.create_user("git2")

# Create access key.
response = C.create_access_key("git2")
display_response(response)

# Create group.
response = C.create_group("git2_users")
display_response(response)

# Add user to group.
C.add_user_to_group("git2_users", "git2")

# Add a policy.
policy=r'''{
   "Statement":[{
      "Effect":"Allow",
      "Action":["s3:*"],
      "Resource":[
      "arn:aws:s3:::ck2.git.chirayuk.com",
      "arn:aws:s3:::ck2.git.chirayuk.com/*"] 
      },
      {
      "Effect":"Deny",
      "Action":["s3:*"],
      "NotResource":[
      "arn:aws:s3:::ck2.git.chirayuk.com",
      "arn:aws:s3:::ck2.git.chirayuk.com/*"] 
      }
   ]
}
'''

C.put_group_policy("git2_users", "git2_policy", policy)