ran*_*cao 5 python amazon-ec2 amazon-web-services boto3
client = boto3.client('ec2',
aws_access_key_id=key,
aws_secret_access_key=secret,
region_name='ap-southeast-1')
response = client.create_instances(
DryRun=True,
ImageId=ami1,
MinCount=1,
MaxCount=1,
KeyName='my-key',
SecurityGroupIds=[sg1, sg2],
InstanceType='m3.medium',
Placement={
'AvailabilityZone': 'ap-southeast-1a'
},
SubnetId=sb1,
NetworkInterfaces=[
{
'NetworkInterfaceId': vpc1,
'SubnetId': sb1,
'Description': 'Description'
}
]
)
print response
Run Code Online (Sandbox Code Playgroud)
在api调用创建实例时遇到错误,我已经验证了像describe_images这样的其他操作正常工作,所以键是正确的.
我错过了什么吗?
小智 7
EC2.Client没有提供create_instances,如错误消息所示.
相反,它是EC2.ServiceResource根据boto3文档提供的
您需要更新第一条指令:
client = boto3.resource('ec2',
aws_access_key_id=key,
aws_secret_access_key=secret,
region_name='ap-southeast-1')
Run Code Online (Sandbox Code Playgroud)
小智 2
您是否尝试过使用 run_instances https://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.run_instances