How do I use Boto3 to launch an EC2 instance with an IAM role?

后端 未结 2 1714
迷失自我
迷失自我 2021-02-19 02:01

I can not figure out how to launch an EC2 instance in Boto3 with a specified IAM role.

Here is some sampe code of how I have been able to successfully create an instanc

2条回答
  •  [愿得一人]
    2021-02-19 02:42

    Note: Some Boto3 versions accept either Arn or Name but all versions accept Name. I suggest using the role name only.

    IamInstanceProfile={
        'Arn': 'string',
        'Name': 'string'
    }
    

    If your profile name is ExampleInstanceProfile and the ARN is arn:aws:iam::123456789012:instance-profile/ExampleInstanceProfile

    ec2.create_instances(ImageId='ami-1e299d7e',
                         InstanceType='t2.micro',
                         MinCount=1, MaxCount=1,
                         SecurityGroupIds=['Mysecuritygroup'],
                         KeyName='mykeyname',
                         IamInstanceProfile={
                                'Arn': 'arn:aws:iam::123456789012:instanceprofile/ExampleInstanceProfile'
                                'Name': 'ExampleInstanceProfile'
                         })
    

提交回复
热议问题