如何使用Boto3启动具有IAM角色的EC2实例? [英] How do I use Boto3 to launch an EC2 instance with an IAM role?

查看:293
本文介绍了如何使用Boto3启动具有IAM角色的EC2实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用指定的IAM角色在Boto3中启动EC2实例。

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


推荐答案

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

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

如果您的个人资料名称为 ExampleInstanceProfile ,并且ARN为 arn:aws:iam :: 123456789012:instance-profile / ExampleInstanceProfile

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

这篇关于如何使用Boto3启动具有IAM角色的EC2实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆