Python boto3:点实例创建
我正在尝试使用boto3创建一个spot实例。虽然我遵循了,但我收到了一个无法理解的异常。我使用的代码是:Python boto3:点实例创建,python,amazon-web-services,amazon-ec2,boto,boto3,Python,Amazon Web Services,Amazon Ec2,Boto,Boto3,我正在尝试使用boto3创建一个spot实例。虽然我遵循了,但我收到了一个无法理解的异常。我使用的代码是: import boto3 import datetime client = boto3.client('ec2') response = client.request_spot_instances( DryRun=False, SpotPrice='0.10', ClientToken='string', InstanceCount=1, Type=
import boto3
import datetime
client = boto3.client('ec2')
response = client.request_spot_instances(
DryRun=False,
SpotPrice='0.10',
ClientToken='string',
InstanceCount=1,
Type='one-time',
LaunchSpecification={
'ImageId': 'ami-fce3c696',
'KeyName': 'awskey.pem',
'SecurityGroups': ['sg-709f8709'],
'InstanceType': 'm4.large',
'Placement': {
'AvailabilityZone': 'us-east-1a',
},
'BlockDeviceMappings': [
{
'Ebs': {
'SnapshotId': 'snap-f70deff0',
'VolumeSize': 100,
'DeleteOnTermination': True,
'VolumeType': 'gp2',
'Iops': 300,
'Encrypted': False
},
},
],
'EbsOptimized': True,
'Monitoring': {
'Enabled': True
},
'SecurityGroupIds': [
'sg-709f8709',
]
}
)
我收到以下例外情况:
botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when calling the RequestSpotInstances operation: Value () for parameter groupId is invalid. The value cannot be empty
问题是在中的请求中没有groupId参数
我遗漏了什么吗?虽然API文档中没有指定,但显然“SecurityGroups”参数需要安全组的名称,而不是ID 更改组名解决了此问题
首先感谢所有费心阅读此问题的人。尝试使用安全组ID,它工作正常
#/usr/bin/python3.6
进口boto3
session=boto3.session.session(profile_name=“*******”)
ec2instances=session.resource('ec2',region_name=“*******”)
新建\u实例=EC2实例。创建\u实例(
ImageId='ami-04125d804acca5692',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro',
KeyName='XXXXXXXXXXXXXX',
SecurityGroupId=['sg-05dec40ce8b91a8c8'],
SubnetId='subnet-01ca807d148d9e328'
)
打印(新实例)
这节省了我几个小时。您是否同时需要SecurityGroups
和securitygroupid
?为什么同时指定SecurityGroups
和securitygroupid
?在传递sg id而不是名称时,您似乎只需要securitygroupid
。最初的问题是关于即时请求,而不是按需请求。