boto3 : AttributeError: 'EC2' object has no attribute 'create_instances'

boto3 : AttributeError: 'EC2' object has no attribute 'create_instances'

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 

调用 api 创建实例时出现错误,我已验证其他操作如 describe_images 工作正常,因此密钥正确。

我是不是漏掉了什么?

EC2.Client 不提供 create_instances,如错误消息所示。

相反,根据 the boto3 documentation

,它是 EC2.ServiceResource 提供的

您需要更新第一条指令:

client = boto3.resource('ec2', 
    aws_access_key_id=key,
    aws_secret_access_key=secret,
    region_name='ap-southeast-1')

您尝试过使用 run_instances https://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client.run_instances