Search code examples
pythonamazon-web-servicesamazon-ec2boto3amazon-ami

How to use boto3 to create an AMI from an Amazon EBS Snapshot


While creating an image (AMI) in AWS using boto3 lib, providing the following parameters:

ec2.create_image(Name=name, BlockDeviceMappings=[{'DeviceName':device_name,'Ebs':{'SnapshotId':snapshot_id, 'DeleteOnTermination': delete_on_term,
            'VolumeSize':10, 'VolumeType':'gp2'}}])

I'm getting this error: Missing required parameter in input: "InstanceId" But when I create an image from the User Interface, no Instance Id is required.

In the picture below, you can see that I can create the image from 'Snapshots' page, without specifying instance Id.

enter image description here

Is anyone aware of a workaround? Thanks.


Solution

  • use this function register_image to create image from snapshots

    response = client.register_image(
        ImageLocation='string',
        Architecture='i386'|'x86_64'|'arm64',
        BlockDeviceMappings=[
            {
                'DeviceName': 'string',
                'VirtualName': 'string',
                'Ebs': {
                    'DeleteOnTermination': True|False,
                    'Iops': 123,
                    'SnapshotId': 'string',
                    'VolumeSize': 123,
                    'VolumeType': 'standard'|'io1'|'gp2'|'sc1'|'st1',
                    'Encrypted': True|False,
                    'KmsKeyId': 'string'
                },
                'NoDevice': 'string'
            },
        ],
        Description='string',
        DryRun=True|False,
        EnaSupport=True|False,
        KernelId='string',
        Name='string',
        BillingProducts=[
            'string',
        ],
        RamdiskId='string',
        RootDeviceName='string',
        SriovNetSupport='string',
        VirtualizationType='string'
    )