Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python boto3 attach/replace IAM role to ec2

I could not find a way to attach/replace an IAM role to the EC2 instance via boto3.
The documentation at cloudhackers provides a method to run image with IAM role, but not to attach.

Is it possible with boto? Otherwise, I need to do it manual.

like image 641
jaks Avatar asked Jan 28 '23 02:01

jaks


1 Answers

Read the docs here

associate_iam_instance_profile(**kwargs)
Associates an IAM instance profile with a running or stopped instance. You cannot associate more than one IAM instance profile with an instance.

Request Syntax

response = client.associate_iam_instance_profile(
    IamInstanceProfile={
        'Arn': 'string',
        'Name': 'string'
    },
    InstanceId='string'
)

Response Syntax

{
    'IamInstanceProfileAssociation': {
        'AssociationId': 'string',
        'InstanceId': 'string',
        'IamInstanceProfile': {
            'Arn': 'string',
            'Id': 'string'
        },
        'State': 'associating'|'associated'|'disassociating'|'disassociated',
        'Timestamp': datetime(2015, 1, 1)
    }
}

By the way, the link you gave has a banner on top saying

Note You are viewing the documentation for an older version of boto (boto2). Boto3, the next version of Boto, is now stable and recommended for general use.

like image 87
raevilman Avatar answered Jan 31 '23 10:01

raevilman