我遇到了调试错误的问题。
botocore.exceptions.ClientError: An error occurred (InvalidParameterCombination) when calling the RunInstances operation: Network interfaces and an instance-level subnet ID may not be specified on the same request
这就是代码。
# Make EC2s with AWS Ubuntu 20
instances = subnet.create_instances(ImageId='ami-0885b1f6bd170450c',
InstanceType='m1.small',
MaxCount=num,
MinCount=num,
Monitoring={'Enabled': True},
KeyName=key_name,
IamInstanceProfile={
'Arn': 'arn goes here',
},
NetworkInterfaces=[{
'DeviceIndex': 0,
'SubnetId': subnet.subnet_id,
'AssociatePublicIpAddress': True,
'Groups': [security_group.group_id]
}])
让我困惑的是,我没有指定顶级子网id。即使我完全删除了子网id,我也会收到错误。
我的猜测是,有些东西是默认设置的,但如果是这样的话,我不知道如何停止。
您的subnet
可能是boto3
中Subnet类的一个实例。因此,通过使用它,您隐式设置实例级子网,从而导致错误。
因此,如果您想操作网络接口,我认为您应该考虑使用boto3.resource('ec2')
中的create_instances,而不是subnet.create_instances
:
ec2 = session.resource('ec2') # or boto3.resource('ec2')
instances = ec2.create_instances(
ImageId='ami-0885b1f6bd170450c',
InstanceType='m1.small',
MaxCount=num,
MinCount=num,
Monitoring={'Enabled': True},
KeyName=key_name,
IamInstanceProfile={
'Arn': 'arn goes here',
},
NetworkInterfaces=[{
'DeviceIndex': 0,
'SubnetId': subnet.subnet_id,
'AssociatePublicIpAddress': True,
'Groups': [security_group.group_id]
}])
print(instances)