diff --git a/builtin/providers/aws/resource_aws_eip.go b/builtin/providers/aws/resource_aws_eip.go index eb38f6124..a9758f5f8 100644 --- a/builtin/providers/aws/resource_aws_eip.go +++ b/builtin/providers/aws/resource_aws_eip.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "net" "strings" "time" @@ -127,12 +128,12 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error { } log.Printf( - "[DEBUG] EIP describe configuration: %#v (domain: %s)", + "[DEBUG] EIP describe configuration: %s (domain: %s)", req, domain) describeAddresses, err := ec2conn.DescribeAddresses(req) if err != nil { - if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidAllocationID.NotFound" { + if ec2err, ok := err.(awserr.Error); ok && (ec2err.Code() == "InvalidAllocationID.NotFound" || ec2err.Code() == "InvalidAddress.NotFound") { d.SetId("") return nil } @@ -173,6 +174,13 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error { d.Set("domain", address.Domain) + // Force ID to be an Allocation ID if we're on a VPC + // This allows users to import the EIP based on the IP if they are in a VPC + if *address.Domain == "vpc" && net.ParseIP(id) != nil { + log.Printf("[DEBUG] Re-assigning EIP ID (%s) to it's Allocation ID (%s)", d.Id(), *address.AllocationId) + d.SetId(*address.AllocationId) + } + return nil } diff --git a/website/source/docs/providers/aws/r/eip.html.markdown b/website/source/docs/providers/aws/r/eip.html.markdown index a00e24b56..f268a7f52 100644 --- a/website/source/docs/providers/aws/r/eip.html.markdown +++ b/website/source/docs/providers/aws/r/eip.html.markdown @@ -107,4 +107,19 @@ 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. + +``` +$ terraform import aws_eip.bar eipalloc-00a10e96 +``` + +EIPs in EC2 Classic can be imported using their Public IP, e.g. + +``` +$ terraform import aws_eip.bar 52.0.0.0 +``` + [1]: https://docs.aws.amazon.com/fr_fr/AWSEC2/latest/APIReference/API_AssociateAddress.html