如何使用Boto3启动具有IAM角色的EC2实例?

时间:2021-10-26 20:37:23

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

我无法弄清楚如何在具有指定IAM角色的Boto3中启动EC2实例。

Here is some sampe code of how I have been able to successfully create an instance so far:

下面是一些示例代码,说明了到目前为止我是如何成功创建实例的:

import boto3
ec2 = boto3.resource('ec2', region_name='us-west-2')
ec2.create_instances(ImageId='ami-1e299d7e', InstanceType='t2.micro',\
MinCount=1, MaxCount=1, SecurityGroupIds=['Mysecuritygroup'], KeyName='mykeyname')

2 个解决方案

#1


10  

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

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

如果您的配置文件名称是ExampleInstanceProfile并且ARN是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'
                     })

#2


4  

Just an addition to the great answer by helloV (I can not comment due to reputation limitations). I encountered the same error message of "The parameter 'iamInstanceProfile.name' may not be used in combination with 'iamInstanceProfile.arn'". So only one key is allowed. I experimented with both and using

只是helloV给出了一个很好的答案(由于声誉限制我无法发表评论)。我遇到了相同的错误消息“参数'iamInstanceProfile.name'可能不会与'iamInstanceProfile.arn'结合使用”。所以只允许一个键。我试验了两者并使用了

IamInstanceProfile={ 'Name': 'ExampleInstanceProfile' }

IamInstanceProfile = {'Name':'ExampleInstanceProfile'}

works for me, but not using

适合我,但没有使用

IamInstanceProfile={'Arn':'arn:aws:iam::123456789012:instanceprofile/ExampleInstanceProfile' }

IamInstanceProfile = {'Arn':'arn:aws:iam :: 123456789012:instanceprofile / ExampleInstanceProfile'}

I am using boto3 version 1.4.4

我正在使用boto3版本1.4.4

#1


10  

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

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

如果您的配置文件名称是ExampleInstanceProfile并且ARN是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'
                     })

#2


4  

Just an addition to the great answer by helloV (I can not comment due to reputation limitations). I encountered the same error message of "The parameter 'iamInstanceProfile.name' may not be used in combination with 'iamInstanceProfile.arn'". So only one key is allowed. I experimented with both and using

只是helloV给出了一个很好的答案(由于声誉限制我无法发表评论)。我遇到了相同的错误消息“参数'iamInstanceProfile.name'可能不会与'iamInstanceProfile.arn'结合使用”。所以只允许一个键。我试验了两者并使用了

IamInstanceProfile={ 'Name': 'ExampleInstanceProfile' }

IamInstanceProfile = {'Name':'ExampleInstanceProfile'}

works for me, but not using

适合我,但没有使用

IamInstanceProfile={'Arn':'arn:aws:iam::123456789012:instanceprofile/ExampleInstanceProfile' }

IamInstanceProfile = {'Arn':'arn:aws:iam :: 123456789012:instanceprofile / ExampleInstanceProfile'}

I am using boto3 version 1.4.4

我正在使用boto3版本1.4.4