diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 10e58ccea..c1f9261fb 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -124,6 +124,7 @@ func resourceAwsInstance() *schema.Resource { ForceNew: true, Optional: true, }, + "tags": tagsSchema(), }, } } @@ -247,6 +248,12 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error { // persist the change... } + if err := setTags(ec2conn, d); err != nil { + return err + } else { + d.SetPartial("tags") + } + return nil } @@ -322,6 +329,7 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error { d.Set("private_ip", instance.PrivateIpAddress) d.Set("subnet_id", instance.SubnetId) d.Set("ebs_optimized", instance.EbsOptimized) + d.Set("tags", tagsToMap(instance.Tags)) // Determine whether we're referring to security groups with // IDs or names. We use a heuristic to figure this out. By default, diff --git a/builtin/providers/aws/resource_aws_instance_test.go b/builtin/providers/aws/resource_aws_instance_test.go index ec679fe1b..58952c84d 100644 --- a/builtin/providers/aws/resource_aws_instance_test.go +++ b/builtin/providers/aws/resource_aws_instance_test.go @@ -64,6 +64,8 @@ func TestAccAWSInstance_normal(t *testing.T) { }) } + + func TestAccAWSInstance_sourceDestCheck(t *testing.T) { 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 { conn := testAccProvider.ec2conn @@ -261,3 +291,18 @@ resource "aws_instance" "foo" { associate_public_ip_address = true } ` + +const testAccCheckInstanceConfigTags = ` +resource "aws_instance" "foo" { + tags { + foo = "bar" + } +} +` + +const testAccCheckInstanceConfigTagsUpdate = ` +resource "aws_vpc" "foo" { + tags { + bar = "baz" + } +} diff --git a/website/source/docs/providers/aws/r/instance.html.markdown b/website/source/docs/providers/aws/r/instance.html.markdown index 169c0eef6..794e17c19 100644 --- a/website/source/docs/providers/aws/r/instance.html.markdown +++ b/website/source/docs/providers/aws/r/instance.html.markdown @@ -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 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. * `iam_instance_profile` - (Optional) The IAM Instance Profile to launch the instance with. +* `tags` - (Optional) A mapping of tags to assign to the resource. ## Attributes Reference