Expands AWS tagging to cover EC2 instances

This commit is contained in:
Sean Herron 2014-10-13 16:55:59 -04:00
parent 5a3f80559c
commit 3e16190e69
3 changed files with 64 additions and 0 deletions

View File

@ -124,6 +124,7 @@ func resourceAwsInstance() *schema.Resource {
ForceNew: true, ForceNew: true,
Optional: true, Optional: true,
}, },
"tags": tagsSchema(),
}, },
} }
} }
@ -247,6 +248,12 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
// persist the change... // persist the change...
} }
if err := setTags(ec2conn, d); err != nil {
return err
} else {
d.SetPartial("tags")
}
return nil return nil
} }
@ -322,6 +329,7 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("private_ip", instance.PrivateIpAddress) d.Set("private_ip", instance.PrivateIpAddress)
d.Set("subnet_id", instance.SubnetId) d.Set("subnet_id", instance.SubnetId)
d.Set("ebs_optimized", instance.EbsOptimized) d.Set("ebs_optimized", instance.EbsOptimized)
d.Set("tags", tagsToMap(instance.Tags))
// Determine whether we're referring to security groups with // Determine whether we're referring to security groups with
// IDs or names. We use a heuristic to figure this out. By default, // IDs or names. We use a heuristic to figure this out. By default,

View File

@ -64,6 +64,8 @@ func TestAccAWSInstance_normal(t *testing.T) {
}) })
} }
func TestAccAWSInstance_sourceDestCheck(t *testing.T) { func TestAccAWSInstance_sourceDestCheck(t *testing.T) {
var v ec2.Instance var v ec2.Instance
@ -122,6 +124,34 @@ func TestAccAWSInstance_vpc(t *testing.T) {
}) })
} }
func TestAccInstance_tags(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: testAccCheckInstanceConfigTags,
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists("aws_instance.foo", &v),
testAccCheckTags(&v.Tags, "foo", "bar"),
),
},
resource.TestStep{
Config: testAccCheckInstanceConfigTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists("aws_instance.foo", &v),
testAccCheckTags(&v.Tags, "foo", ""),
testAccCheckTags(&v.Tags, "bar", "baz"),
),
},
},
})
}
func testAccCheckInstanceDestroy(s *terraform.State) error { func testAccCheckInstanceDestroy(s *terraform.State) error {
conn := testAccProvider.ec2conn conn := testAccProvider.ec2conn
@ -261,3 +291,18 @@ resource "aws_instance" "foo" {
associate_public_ip_address = true associate_public_ip_address = true
} }
` `
const testAccCheckInstanceConfigTags = `
resource "aws_instance" "foo" {
tags {
foo = "bar"
}
}
`
const testAccCheckInstanceConfigTagsUpdate = `
resource "aws_vpc" "foo" {
tags {
bar = "baz"
}
}

View File

@ -19,6 +19,16 @@ resource "aws_instance" "web" {
} }
``` ```
# Create a new instance of the ami-1234 on an m1.small node with an AWS Tag naming it "HelloWorld"
resource "aws_instance" "web" {
ami = "ami-1234"
instance_type = "m1.small"
tags {
Name = "HelloWorld"
}
}
```
## Argument Reference ## Argument Reference
The following arguments are supported: The following arguments are supported:
@ -41,6 +51,7 @@ The following arguments are supported:
* `user_data` - (Optional) The user data to provide when launching the instance. * `user_data` - (Optional) The user data to provide when launching the instance.
* `iam_instance_profile` - (Optional) The IAM Instance Profile to * `iam_instance_profile` - (Optional) The IAM Instance Profile to
launch the instance with. launch the instance with.
* `tags` - (Optional) A mapping of tags to assign to the resource.
## Attributes Reference ## Attributes Reference