python boto3 attach/replace IAM role to ec2

2020-04-14 07:19发布

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.

1条回答
手持菜刀,她持情操
2楼-- · 2020-04-14 07:45

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.

查看更多
登录 后发表回答