简体   繁体   中英

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 

getting error while making api call to create instance, I have verified other operation like describe_images is working fine so keys are proper.

am I missing something ?

EC2.Client does not provide create_instances , as the error message indicates.

Instead, it is EC2.ServiceResource that provides it, according to the boto3 documentation

You need to update the first instruction:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM