【问题标题】:How do I use Boto3 to launch an EC2 instance with an IAM role?如何使用 Boto3 启动具有 IAM 角色的 EC2 实例?
【发布时间】:2017-05-21 23:05:11
【问题描述】:

我不知道如何在 Boto3 中使用指定的 IAM 角色启动 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')

【问题讨论】:

    标签: python amazon-web-services amazon-ec2 boto3 amazon-iam


    【解决方案1】:

    注意:某些 Boto3 版本接受 either 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'
                         })
    

    【讨论】:

    • 成功了,谢谢!只是一个注释,上面写着:The parameter 'iamInstanceProfile.name' may not be used in combination with 'iamInstanceProfile.arn'
    【解决方案2】:

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

    IamInstanceProfile={ 'Name': 'ExampleInstanceProfile' }
    

    对我有用,但不使用

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

    我使用的是 boto3 版本 1.4.4

    【讨论】:

      猜你喜欢
      • 2018-04-11
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多