【问题标题】:boto3 : AttributeError: 'EC2' object has no attribute 'create_instances'boto3:AttributeError:“EC2”对象没有属性“create_instances”
【发布时间】:2016-03-28 10:55:58
【问题描述】:
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 之类的其他操作工作正常,所以键是正确的。

我错过了什么吗?

【问题讨论】:

  • 第 1 行不应该是 boto3.resource('ec2'... 而不是 boto3.client('ec2'...

标签: python amazon-web-services amazon-ec2 boto3


【解决方案1】:

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

【讨论】:

  • 嗯,我收到了AttributeError: 'ec2.ServiceResource' object has no attribute 'release_address'。我正在使用ec2 = session.resource('ec2') 创建对象。
【解决方案2】:
猜你喜欢
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2021-04-19
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
相关资源
最近更新 更多