diff --git a/builtin/providers/aws/resource_aws_eip_association.go b/builtin/providers/aws/resource_aws_eip_association.go index ce31cf710..b3db8655d 100644 --- a/builtin/providers/aws/resource_aws_eip_association.go +++ b/builtin/providers/aws/resource_aws_eip_association.go @@ -119,7 +119,9 @@ func resourceAwsEipAssociationRead(d *schema.ResourceData, meta interface{}) err } if response.Addresses == nil || len(response.Addresses) == 0 { - return fmt.Errorf("Unable to find EIP Association: %s", d.Id()) + log.Printf("[INFO] EIP Association ID Not Found. Refreshing from state") + d.SetId("") + return nil } return readAwsEipAssociation(d, response.Addresses[0]) diff --git a/builtin/providers/aws/resource_aws_eip_association_test.go b/builtin/providers/aws/resource_aws_eip_association_test.go index ef0e7f1fa..ded6ae654 100644 --- a/builtin/providers/aws/resource_aws_eip_association_test.go +++ b/builtin/providers/aws/resource_aws_eip_association_test.go @@ -39,6 +39,42 @@ func TestAccAWSEIPAssociation_basic(t *testing.T) { }) } +func TestAccAWSEIPAssociation_disappears(t *testing.T) { + var a ec2.Address + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEIPAssociationDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSEIPAssociationConfigDisappears, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSEIPExists( + "aws_eip.bar", &a), + testAccCheckAWSEIPAssociationExists( + "aws_eip_association.by_allocation_id", &a), + testAccCheckEIPAssociationDisappears(&a), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckEIPAssociationDisappears(address *ec2.Address) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).ec2conn + opts := &ec2.DisassociateAddressInput{ + AssociationId: address.AssociationId, + } + if _, err := conn.DisassociateAddress(opts); err != nil { + return err + } + return nil + } +} + func testAccCheckAWSEIPAssociationExists(name string, res *ec2.Address) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] @@ -150,3 +186,29 @@ resource "aws_network_interface" "baz" { } } ` + +const testAccAWSEIPAssociationConfigDisappears = ` +resource "aws_vpc" "main" { + cidr_block = "192.168.0.0/24" +} +resource "aws_subnet" "sub" { + vpc_id = "${aws_vpc.main.id}" + cidr_block = "192.168.0.0/25" + availability_zone = "us-west-2a" +} +resource "aws_internet_gateway" "igw" { + vpc_id = "${aws_vpc.main.id}" +} +resource "aws_instance" "foo" { + ami = "ami-21f78e11" + availability_zone = "us-west-2a" + instance_type = "t1.micro" + subnet_id = "${aws_subnet.sub.id}" +} +resource "aws_eip" "bar" { + vpc = true +} +resource "aws_eip_association" "by_allocation_id" { + allocation_id = "${aws_eip.bar.id}" + instance_id = "${aws_instance.foo.id}" +}`