providers/aws/aws_instance: can bring up in VPC

This commit is contained in:
Mitchell Hashimoto 2014-07-14 13:46:32 -07:00
parent 7a1c40855e
commit 88dd1baafe
2 changed files with 48 additions and 0 deletions

View File

@ -26,6 +26,7 @@ func resource_aws_instance_create(
runOpts := &ec2.RunInstances{ runOpts := &ec2.RunInstances{
ImageId: rs.Attributes["ami"], ImageId: rs.Attributes["ami"],
InstanceType: rs.Attributes["instance_type"], InstanceType: rs.Attributes["instance_type"],
SubnetId: rs.Attributes["subnet_id"],
} }
log.Printf("[DEBUG] Run configuration: %#v", runOpts) log.Printf("[DEBUG] Run configuration: %#v", runOpts)
runResp, err := ec2conn.RunInstances(runOpts) runResp, err := ec2conn.RunInstances(runOpts)
@ -108,6 +109,7 @@ func resource_aws_instance_diff(
"ami": diff.AttrTypeCreate, "ami": diff.AttrTypeCreate,
"availability_zone": diff.AttrTypeCreate, "availability_zone": diff.AttrTypeCreate,
"instance_type": diff.AttrTypeCreate, "instance_type": diff.AttrTypeCreate,
"subnet_id": diff.AttrTypeCreate,
}, },
ComputedAttrs: []string{ ComputedAttrs: []string{
@ -163,6 +165,15 @@ func resource_aws_instance_update_state(
s.Attributes["public_ip"] = instance.PublicIpAddress s.Attributes["public_ip"] = instance.PublicIpAddress
s.Attributes["private_dns"] = instance.PrivateDNSName s.Attributes["private_dns"] = instance.PrivateDNSName
s.Attributes["private_ip"] = instance.PrivateIpAddress 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 return s, nil
} }

View File

@ -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 { func testAccCheckInstanceDestroy(s *terraform.State) error {
conn := testAccProvider.ec2conn conn := testAccProvider.ec2conn
@ -94,3 +113,21 @@ resource "aws_instance" "foo" {
instance_type = "m1.small" 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}"
}
`