diff --git a/aws/resource_aws_eip.go b/aws/resource_aws_eip.go index c85c0655c96..67c99a909b2 100644 --- a/aws/resource_aws_eip.go +++ b/aws/resource_aws_eip.go @@ -73,6 +73,8 @@ func resourceAwsEip() *schema.Resource { Type: schema.TypeString, Optional: true, }, + + "tags": tagsSchema(), }, } } @@ -111,6 +113,13 @@ func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error { } log.Printf("[INFO] EIP ID: %s (domain: %v)", d.Id(), *allocResp.Domain) + + if _, ok := d.GetOk("tags"); ok { + if err := setTags(ec2conn, d); err != nil { + return fmt.Errorf("Error creating EIP tags: %s", err) + } + } + return resourceAwsEipUpdate(d, meta) } @@ -206,6 +215,8 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error { d.SetId(*address.AllocationId) } + d.Set("tags", tagsToMap(address.Tags)) + return nil } @@ -270,6 +281,12 @@ func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error { } } + if _, ok := d.GetOk("tags"); ok { + if err := setTags(ec2conn, d); err != nil { + return fmt.Errorf("Error updating EIP tags: %s", err) + } + } + return resourceAwsEipRead(d, meta) } diff --git a/aws/resource_aws_eip_test.go b/aws/resource_aws_eip_test.go index 053f9432f75..9f65f31bceb 100644 --- a/aws/resource_aws_eip_test.go +++ b/aws/resource_aws_eip_test.go @@ -9,6 +9,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) @@ -265,6 +266,42 @@ func TestAccAWSEIPAssociate_not_associated(t *testing.T) { }) } +func TestAccAWSEIP_tags(t *testing.T) { + var conf ec2.Address + resourceName := "aws_eip.bar" + rName1 := fmt.Sprintf("%s-%d", t.Name(), acctest.RandInt()) + rName2 := fmt.Sprintf("%s-%d", t.Name(), acctest.RandInt()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_eip.bar", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEIPDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSEIPConfig_tags(rName1, t.Name()), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSEIPExists(resourceName, &conf), + testAccCheckAWSEIPAttributes(&conf), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.RandomName", rName1), + resource.TestCheckResourceAttr(resourceName, "tags.TestName", t.Name()), + ), + }, + resource.TestStep{ + Config: testAccAWSEIPConfig_tags(rName2, t.Name()), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSEIPExists(resourceName, &conf), + testAccCheckAWSEIPAttributes(&conf), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), + resource.TestCheckResourceAttr(resourceName, "tags.RandomName", rName2), + resource.TestCheckResourceAttr(resourceName, "tags.TestName", t.Name()), + ), + }, + }, + }) +} + func testAccCheckAWSEIPDisappears(v *ec2.Address) resource.TestCheckFunc { return func(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).ec2conn @@ -402,6 +439,17 @@ resource "aws_eip" "bar" { } ` +func testAccAWSEIPConfig_tags(rName, testName string) string { + return fmt.Sprintf(` +resource "aws_eip" "bar" { + tags { + RandomName = "%[1]s" + TestName = "%[2]s" + } +} +`, rName, testName) +} + const testAccAWSEIPInstanceEc2Classic = ` provider "aws" { region = "us-east-1" diff --git a/website/docs/r/eip.html.markdown b/website/docs/r/eip.html.markdown index e828b11c36b..374bcf11ff9 100644 --- a/website/docs/r/eip.html.markdown +++ b/website/docs/r/eip.html.markdown @@ -92,6 +92,7 @@ The following arguments are supported: * `associate_with_private_ip` - (Optional) A user specified primary or secondary private IP address to associate with the Elastic IP address. If no private IP address is specified, the Elastic IP address is associated with the primary private IP address. +* `tags` - (Optional) A mapping of tags to assign to the resource. ~> **NOTE:** You can specify either the `instance` ID or the `network_interface` ID, but not both. Including both will **not** return an error from the AWS API, but will @@ -100,7 +101,7 @@ more information. ## Attributes Reference -The following attributes are exported: +The following additional attributes are exported: * `id` - Contains the EIP allocation ID. * `private_ip` - Contains the private IP address (if in VPC). @@ -110,7 +111,6 @@ The following attributes are exported: * `instance` - Contains the ID of the attached instance. * `network_interface` - Contains the ID of the attached network interface. - ## Import EIPs in a VPC can be imported using their Allocation ID, e.g.