diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 6ab161015..888877f2b 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -26,6 +26,7 @@ func resource_aws_instance_create( runOpts := &ec2.RunInstances{ ImageId: rs.Attributes["ami"], InstanceType: rs.Attributes["instance_type"], + SubnetId: rs.Attributes["subnet_id"], } log.Printf("[DEBUG] Run configuration: %#v", runOpts) runResp, err := ec2conn.RunInstances(runOpts) @@ -108,6 +109,7 @@ func resource_aws_instance_diff( "ami": diff.AttrTypeCreate, "availability_zone": diff.AttrTypeCreate, "instance_type": diff.AttrTypeCreate, + "subnet_id": diff.AttrTypeCreate, }, ComputedAttrs: []string{ @@ -163,6 +165,15 @@ func resource_aws_instance_update_state( s.Attributes["public_ip"] = instance.PublicIpAddress s.Attributes["private_dns"] = instance.PrivateDNSName s.Attributes["private_ip"] = instance.PrivateIpAddress + s.Attributes["subnet_id"] = instance.SubnetId + s.Dependencies = nil + + if instance.SubnetId != "" { + s.Dependencies = append(s.Dependencies, + terraform.ResourceDependency{ID: instance.SubnetId}, + ) + } + return s, nil } diff --git a/builtin/providers/aws/resource_aws_instance_test.go b/builtin/providers/aws/resource_aws_instance_test.go index 59fbd3068..1dc8e9e6d 100644 --- a/builtin/providers/aws/resource_aws_instance_test.go +++ b/builtin/providers/aws/resource_aws_instance_test.go @@ -28,6 +28,25 @@ func TestAccAWSInstance(t *testing.T) { }) } +func TestAccAWSInstance_vpc(t *testing.T) { + var v ec2.Instance + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccInstanceConfigVPC, + Check: resource.ComposeTestCheckFunc( + testAccCheckInstanceExists( + "aws_instance.foo", &v), + ), + }, + }, + }) +} + func testAccCheckInstanceDestroy(s *terraform.State) error { conn := testAccProvider.ec2conn @@ -94,3 +113,21 @@ resource "aws_instance" "foo" { instance_type = "m1.small" } ` + +const testAccInstanceConfigVPC = ` +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_subnet" "foo" { + cidr_block = "10.1.1.0/24" + vpc_id = "${aws_vpc.foo.id}" +} + +resource "aws_instance" "foo" { + # us-west-2 + ami = "ami-4fccb37f" + instance_type = "m1.small" + subnet_id = "${aws_subnet.foo.id}" +} +`