Skip to content

Commit c8a7efb

Browse files
authored
Merge pull request #26716 from hashicorp/b-eip-no-vpc
resource/aws_eip: Default to default domain when `vpc` not set
2 parents 5187c3e + 6339566 commit c8a7efb

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

.changelog/26716.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/aws_eip: Defaults to default regional `domain` when `vpc` not set
3+
```

internal/service/ec2/ec2_eip.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,10 @@ func resourceEIPCreate(d *schema.ResourceData, meta interface{}) error {
131131
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
132132
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))
133133

134-
if _, ok := d.GetOk("vpc"); !ok {
135-
return errors.New(`with the retirement of EC2-Classic no new non-VPC EC2 EIPs can be created`)
136-
}
134+
input := &ec2.AllocateAddressInput{}
137135

138-
input := &ec2.AllocateAddressInput{
139-
Domain: aws.String(ec2.DomainTypeVpc),
136+
if v := d.Get("vpc"); v != nil && v.(bool) {
137+
input.Domain = aws.String(ec2.DomainTypeVpc)
140138
}
141139

142140
if v, ok := d.GetOk("address"); ok {

internal/service/ec2/ec2_eip_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,34 @@ func TestAccEC2EIP_disappears(t *testing.T) {
6868
})
6969
}
7070

71+
func TestAccEC2EIP_noVPC(t *testing.T) {
72+
var conf ec2.Address
73+
resourceName := "aws_eip.test"
74+
75+
resource.ParallelTest(t, resource.TestCase{
76+
PreCheck: func() { acctest.PreCheck(t) },
77+
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
78+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
79+
CheckDestroy: testAccCheckEIPDestroy,
80+
Steps: []resource.TestStep{
81+
{
82+
Config: testAccEIPConfig_noVPC,
83+
Check: resource.ComposeTestCheckFunc(
84+
testAccCheckEIPExists(resourceName, &conf),
85+
resource.TestCheckResourceAttr(resourceName, "domain", "vpc"),
86+
resource.TestCheckResourceAttrSet(resourceName, "public_ip"),
87+
testAccCheckEIPPublicDNS(resourceName),
88+
),
89+
},
90+
{
91+
ResourceName: resourceName,
92+
ImportState: true,
93+
ImportStateVerify: true,
94+
},
95+
},
96+
})
97+
}
98+
7199
func TestAccEC2EIP_tags(t *testing.T) {
72100
var conf ec2.Address
73101
resourceName := "aws_eip.test"
@@ -745,6 +773,11 @@ resource "aws_eip" "test" {
745773
}
746774
`
747775

776+
const testAccEIPConfig_noVPC = `
777+
resource "aws_eip" "test" {
778+
}
779+
`
780+
748781
func testAccEIPConfig_tags1(tagKey1, tagValue1 string) string {
749782
return fmt.Sprintf(`
750783
resource "aws_eip" "test" {

website/docs/r/eip.html.markdown

+5-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ The following arguments are supported:
103103
* `instance` - (Optional) EC2 instance ID.
104104
* `network_border_group` - (Optional) Location from which the IP address is advertised. Use this parameter to limit the address to this location.
105105
* `network_interface` - (Optional) Network interface ID to associate with.
106-
* `public_ipv4_pool` - (Optional) EC2 IPv4 address pool identifier or `amazon`. This option is only available for VPC EIPs.
106+
* `public_ipv4_pool` - (Optional) EC2 IPv4 address pool identifier or `amazon`.
107+
This option is only available for VPC EIPs.
107108
* `tags` - (Optional) Map of tags to assign to the resource. Tags can only be applied to EIPs in a VPC. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
108109
* `vpc` - (Optional) Boolean if the EIP is in a VPC or not.
110+
Defaults to `true` unless the region supports EC2-Classic.
109111

110112
~> **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 have undefined behavior. See the relevant [AssociateAddress API Call][1] for more information.
111113

@@ -120,7 +122,7 @@ In addition to all arguments above, the following attributes are exported:
120122
* `association_id` - ID representing the association of the address with an instance in a VPC.
121123
* `carrier_ip` - Carrier IP address.
122124
* `customer_owned_ip` - Customer owned IP.
123-
* `domain` - Indicates if this EIP is for use in VPC (`vpc`) or EC2 Classic (`standard`).
125+
* `domain` - Indicates if this EIP is for use in VPC (`vpc`) or EC2-Classic (`standard`).
124126
* `id` - Contains the EIP allocation ID.
125127
* `private_dns` - The Private DNS associated with the Elastic IP address (if in VPC).
126128
* `private_ip` - Contains the private IP address (if in VPC).
@@ -146,7 +148,7 @@ EIPs in a VPC can be imported using their Allocation ID, e.g.,
146148
$ terraform import aws_eip.bar eipalloc-00a10e96
147149
```
148150

149-
EIPs in EC2 Classic can be imported using their Public IP, e.g.,
151+
EIPs in EC2-Classic can be imported using their Public IP, e.g.,
150152

151153
```
152154
$ terraform import aws_eip.bar 52.0.0.0

0 commit comments

Comments
 (0)