Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_eip: dissociate EIP on update #878

Merged
merged 5 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions aws/resource_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ func resourceAwsEipUpdate(d *schema.ResourceData, meta interface{}) error {
v_instance, ok_instance := d.GetOk("instance")
v_interface, ok_interface := d.GetOk("network_interface")

// If we are updating an EIP that is not newly created, and we are attached to
// an instance or interface, detach first.
if (d.Get("instance").(string) != "" || d.Get("association_id").(string) != "") && !d.IsNewResource() {
if err := disassociateEip(d, meta); err != nil {
return err
}
}

if ok_instance || ok_interface {
instanceId := v_instance.(string)
networkInterfaceId := v_interface.(string)
Expand Down Expand Up @@ -256,30 +264,7 @@ func resourceAwsEipDelete(d *schema.ResourceData, meta interface{}) error {

// If we are attached to an instance or interface, detach first.
if d.Get("instance").(string) != "" || d.Get("association_id").(string) != "" {
log.Printf("[DEBUG] Disassociating EIP: %s", d.Id())
var err error
switch resourceAwsEipDomain(d) {
case "vpc":
_, err = ec2conn.DisassociateAddress(&ec2.DisassociateAddressInput{
AssociationId: aws.String(d.Get("association_id").(string)),
})
case "standard":
_, err = ec2conn.DisassociateAddress(&ec2.DisassociateAddressInput{
PublicIp: aws.String(d.Get("public_ip").(string)),
})
}

if err != nil {
// First check if the association ID is not found. If this
// is the case, then it was already disassociated somehow,
// and that is okay. The most commmon reason for this is that
// the instance or ENI it was attached it was destroyed.
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidAssociationID.NotFound" {
err = nil
}
}

if err != nil {
if err := disassociateEip(d, meta); err != nil {
return err
}
}
Expand Down Expand Up @@ -324,3 +309,28 @@ func resourceAwsEipDomain(d *schema.ResourceData) string {

return "standard"
}

func disassociateEip(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn
log.Printf("[DEBUG] Disassociating EIP: %s", d.Id())
var err error
switch resourceAwsEipDomain(d) {
case "vpc":
_, err = ec2conn.DisassociateAddress(&ec2.DisassociateAddressInput{
AssociationId: aws.String(d.Get("association_id").(string)),
})
case "standard":
_, err = ec2conn.DisassociateAddress(&ec2.DisassociateAddressInput{
PublicIp: aws.String(d.Get("public_ip").(string)),
})
}

// First check if the association ID is not found. If this
// is the case, then it was already disassociated somehow,
// and that is okay. The most commmon reason for this is that
// the instance or ENI it was attached it was destroyed.
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidAssociationID.NotFound" {
err = nil
}
return err
}
104 changes: 104 additions & 0 deletions aws/resource_aws_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,40 @@ func TestAccAWSEIP_associated_user_private_ip(t *testing.T) {
})
}

// Regression test for https:/hashicorp/terraform/issues/3429 (now
// https:/terraform-providers/terraform-provider-aws/issues/42)
func TestAccAWSEIP_classic_disassociate(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEIPDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSEIP_classic_disassociate("ami-408c7f28"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"aws_eip.ip.0",
"instance"),
resource.TestCheckResourceAttrSet(
"aws_eip.ip.1",
"instance"),
),
},
resource.TestStep{
Config: testAccAWSEIP_classic_disassociate("ami-8c6ea9e4"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"aws_eip.ip.0",
"instance"),
resource.TestCheckResourceAttrSet(
"aws_eip.ip.1",
"instance"),
),
},
},
})
}

func testAccCheckAWSEIPDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

Expand Down Expand Up @@ -308,6 +342,9 @@ provider "aws" {
resource "aws_instance" "foo" {
ami = "ami-5469ae3c"
instance_type = "m1.small"
tags {
Name = "testAccAWSEIPInstanceEc2Classic"
}
}

resource "aws_eip" "bar" {
Expand Down Expand Up @@ -544,3 +581,70 @@ resource "aws_eip" "two" {
associate_with_private_ip = "10.0.0.11"
}
`

func testAccAWSEIP_classic_disassociate(ami string) string {
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
}

variable "server_count" {
default = 2
}

resource "aws_eip" "ip" {
count = "${var.server_count}"
instance = "${element(aws_instance.example.*.id, count.index)}"
vpc = true
}

resource "aws_instance" "example" {
count = "${var.server_count}"

ami = "%s"
instance_type = "m1.small"
associate_public_ip_address = true
subnet_id = "${aws_subnet.us-east-1b-public.id}"
availability_zone = "${aws_subnet.us-east-1b-public.availability_zone}"

tags {
Name = "testAccAWSEIP_classic_disassociate"
}

lifecycle {
create_before_destroy = true
}
}

resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
tags {
Name = "TestAccAWSEIP_classic_disassociate"
}
}

resource "aws_internet_gateway" "example" {
vpc_id = "${aws_vpc.example.id}"
}

resource "aws_subnet" "us-east-1b-public" {
vpc_id = "${aws_vpc.example.id}"

cidr_block = "10.0.0.0/24"
availability_zone = "us-east-1b"
}

resource "aws_route_table" "us-east-1-public" {
vpc_id = "${aws_vpc.example.id}"

route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.example.id}"
}
}

resource "aws_route_table_association" "us-east-1b-public" {
subnet_id = "${aws_subnet.us-east-1b-public.id}"
route_table_id = "${aws_route_table.us-east-1-public.id}"
}`, ami)
}