Created
June 12, 2012 13:55
-
-
Save garnaat/2917662 to your computer and use it in GitHub Desktop.
Example using boto to create an IAM role and associate it with an EC2 instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: policy = """{ | |
...: "Statement":[{ | |
...: "Effect":"Allow", | |
...: "Action":["s3:*"], | |
...: "Resource":["arn:aws:s3:::mybucket"]}]}""" | |
In [2]: import boto | |
In [4]: c = boto.connect_iam() | |
In [5]: instance_profile = c.create_instance_profile('myinstanceprofile') | |
In [6]: role = c.create_role('myrole') | |
In [7]: c.add_role_to_instance_profile('myinstanceprofile', 'myrole') | |
Out[7]: {u'add_role_to_instance_profile_response': {u'response_metadata': {u'request_id': u'2221d92c-b437-11e1-86e5-c9c4f3b58653'}}} | |
In [8]: c.put_role_policy('myrole', 'mypolicy', policy) | |
Out[8]: {u'put_role_policy_response': {u'response_metadata': {u'request_id': u'2b878c93-b437-11e1-86e5-c9c4f3b58653'}}} | |
In [9]: c = boto.connect_ec2() | |
In [10]: c.run_instances('ami-e565ba8c', key_name='mykeyname', security_groups=['mysecuritygroup'], instance_type='t1.micro', instance_profile_name='myinstanceprofile') | |
As this example is not compatible with boto3
, i wrote the equivalent with this newer boto version https://github.com/iMilnb/awstools/blob/master/platforms/roles/mkrole.py
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this , it still works :)