繁体   English   中英

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

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

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

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

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')

注意 :有些Boto3版本接受要么 Arn Name ,但所有版本接受Name 我建议仅使用角色名称。

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

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

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

IamInstanceProfile={ 'Name': 'ExampleInstanceProfile' }

适合我,但没有使用

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

我正在使用boto3版本1.4.4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM