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

标签 python amazon-web-services amazon-ec2 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 

在创建实例时调用 api 时出错,我已经验证其他操作(如 describe_images)工作正常,所以 key 是正确的。

我错过了什么吗?

最佳答案

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')

关于python - boto3 : AttributeError: 'EC2' object has no attribute 'create_instances' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34411402/

相关文章:

python 管理.py runserver - 错误 : no module named registration

python - os.system ('sudo shutdown now -P' ) 不适用于 cloud-init

hadoop - 如何在 aws emr 中将 ec2-user 添加到 hadoop

mysql - 在 ec2 上将 mysql 5.5 更新到 5.6

amazon-web-services - 限制AWS中的实例类型和请求区域

python - 如何调用具有多个参数的方法

python - 它有效,但为什么需要通过减去 1 来改变索引

python - AWS Lambda Python S3读取文件错误

amazon-web-services - 使用 Laravel 5.3 的 Amazon SES 403 Forbidden SignatureDoesNotMatch

python子进程编码