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
を受け入れますorName
ですが、すべてのバージョンが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
と組み合わせて使用することはできません。1つのキーしか許可されていません。両方を試して、
IamInstanceProfile={ 'Name': 'ExampleInstanceProfile' }
私にとっては機能しますが、使用しません
IamInstanceProfile={'Arn':'arn:aws:iam::123456789012:instanceprofile/ExampleInstanceProfile'}
Boto3バージョン1.4.4を使用しています