From 9709ac5fd58662c228a93077ed3a014395fbe110 Mon Sep 17 00:00:00 2001
From: Tom Wilkie <tomwilkie@users.noreply.github.com>
Date: Thu, 1 Dec 2016 13:53:14 +0000
Subject: [PATCH] Do not return a root device for instance store backed AMIs.
 (#9483)

* Do not return a root device for instance store backed AMIs.

* Add root EC2 instance store acceptance test.
---
 resource_aws_instance.go      |  5 +++++
 resource_aws_instance_test.go | 40 +++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/resource_aws_instance.go b/resource_aws_instance.go
index 331c469d870..ed96912510c 100644
--- a/resource_aws_instance.go
+++ b/resource_aws_instance.go
@@ -859,6 +859,11 @@ func fetchRootDeviceName(ami string, conn *ec2.EC2) (*string, error) {
 	image := res.Images[0]
 	rootDeviceName := image.RootDeviceName
 
+	// Instance store backed AMIs do not provide a root device name.
+	if *image.RootDeviceType == ec2.DeviceTypeInstanceStore {
+		return nil, nil
+	}
+
 	// Some AMIs have a RootDeviceName like "/dev/sda1" that does not appear as a
 	// DeviceName in the BlockDeviceMapping list (which will instead have
 	// something like "/dev/sda")
diff --git a/resource_aws_instance_test.go b/resource_aws_instance_test.go
index 5c9028066da..38c2dccfc80 100644
--- a/resource_aws_instance_test.go
+++ b/resource_aws_instance_test.go
@@ -245,6 +245,46 @@ func TestAccAWSInstance_blockDevices(t *testing.T) {
 	})
 }
 
+func TestAccAWSInstance_rootInstanceStore(t *testing.T) {
+	var v ec2.Instance
+
+	resource.Test(t, resource.TestCase{
+		PreCheck:      func() { testAccPreCheck(t) },
+		IDRefreshName: "aws_instance.foo",
+		Providers:     testAccProviders,
+		CheckDestroy:  testAccCheckInstanceDestroy,
+		Steps: []resource.TestStep{
+			resource.TestStep{
+				Config: `
+					resource "aws_instance" "foo" {
+						# us-west-2
+						# Amazon Linux HVM Instance Store 64-bit (2016.09.0)
+						# https://aws.amazon.com/amazon-linux-ami
+						ami = "ami-44c36524"
+
+						# Only certain instance types support ephemeral root instance stores.
+						# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html
+						instance_type = "m3.medium"
+					}`,
+				Check: resource.ComposeTestCheckFunc(
+					testAccCheckInstanceExists(
+						"aws_instance.foo", &v),
+					resource.TestCheckResourceAttr(
+						"aws_instance.foo", "ami", "ami-44c36524"),
+					resource.TestCheckResourceAttr(
+						"aws_instance.foo", "ebs_block_device.#", "0"),
+					resource.TestCheckResourceAttr(
+						"aws_instance.foo", "ebs_optimized", "false"),
+					resource.TestCheckResourceAttr(
+						"aws_instance.foo", "instance_type", "m3.medium"),
+					resource.TestCheckResourceAttr(
+						"aws_instance.foo", "root_block_device.#", "0"),
+				),
+			},
+		},
+	})
+}
+
 func TestAccAWSInstance_sourceDestCheck(t *testing.T) {
 	var v ec2.Instance