mitchellh / goamz

Golang Amazon Library
Other
672 stars 216 forks source link

ec2.CreateImage with NoDevice return InvalidBlockDeviceMapping #251

Open jsternberg opened 9 years ago

jsternberg commented 9 years ago

I ran into this issue when using packer in issue mitchellh/packer#1500. When I attempt to create an image from an EBS volume using ec2.CreateImage, it fails with an error like this:

Either snapshot ID or volume size must be provided for: /dev/sdb, /dev/sdc (InvalidBlockDeviceMapping)

Here's the reproducer code:

package main

import (
    "fmt"

    "github.com/mitchellh/goamz/aws"
    "github.com/mitchellh/goamz/ec2"
)

func main() {
    blockDevices := []ec2.BlockDeviceMapping{
        ec2.BlockDeviceMapping{
            DeviceName: "/dev/sdb",
            NoDevice:   true,
        },
        ec2.BlockDeviceMapping{
            DeviceName: "/dev/sdc",
            NoDevice:   true,
        },
    }
    options := ec2.CreateImage{
        InstanceId:   "<instance-id>",
        Name:         "amitest-build",
        BlockDevices: blockDevices,
    }

    auth, err := aws.EnvAuth()
    if err != nil {
        panic(err)
    }
    ec2conn := ec2.New(auth, aws.USEast)
    if _, err := ec2conn.CreateImage(&options); err != nil {
        fmt.Println(err)
    }
}

Launch an instance using the Ubuntu 14.04 AMI ami-f63b3e9e (the latest ebs:hvm image at the time of me writing) and replace <instance-id> above with your instance.

In an attempt to see if this was an issue with how this was invoked, I tried the same with boto.

#!/usr/bin/env python

import boto.ec2
from boto.ec2.blockdevicemapping import BlockDeviceMapping, BlockDeviceType

ec2_conn = boto.ec2.connect_to_region('us-east-1')
bdm = BlockDeviceMapping()
bdm['/dev/sdb'] = BlockDeviceType(no_device=True)
bdm['/dev/sdc'] = BlockDeviceType(no_device=True)
print ec2_conn.create_image('<instance-id>', 'test-image-build', block_device_mapping=bdm)

This code works perfectly.