gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:28:05 26 4
gpt4 key购买 nike

我不知道如何使用指定的 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 版本接受 either Arn or 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'
})

关于python - 如何使用 Boto3 启动具有 IAM 角色的 EC2 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41518334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com