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

add support for private ipv4 addresses in subnet mapping of ELB V2 #11404

Merged
merged 6 commits into from Jul 31, 2020
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
13 changes: 13 additions & 0 deletions aws/resource_aws_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func resourceAwsLb() *schema.Resource {
Optional: true,
ForceNew: true,
},
"private_ipv4_address": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add validation here ValidateFunc: validation.IsIPv4Address,

},
},
},
Set: func(v interface{}) int {
Expand All @@ -124,6 +129,9 @@ func resourceAwsLb() *schema.Resource {
if m["allocation_id"] != "" {
buf.WriteString(fmt.Sprintf("%s-", m["allocation_id"].(string)))
}
if m["private_ipv4_address"] != "" {
buf.WriteString(fmt.Sprintf("%s-", m["private_ipv4_address"].(string)))
}
return hashcode.String(buf.String())
},
},
Expand Down Expand Up @@ -276,6 +284,10 @@ func resourceAwsLbCreate(d *schema.ResourceData, meta interface{}) error {
if subnetMap["allocation_id"].(string) != "" {
elbOpts.SubnetMappings[i].AllocationId = aws.String(subnetMap["allocation_id"].(string))
}

if subnetMap["private_ipv4_address"].(string) != "" {
elbOpts.SubnetMappings[i].PrivateIPv4Address = aws.String(subnetMap["private_ipv4_address"].(string))
}
}
}

Expand Down Expand Up @@ -689,6 +701,7 @@ func flattenSubnetMappingsFromAvailabilityZones(availabilityZones []*elbv2.Avail

for _, loadBalancerAddress := range availabilityZone.LoadBalancerAddresses {
m["allocation_id"] = aws.StringValue(loadBalancerAddress.AllocationId)
m["private_ipv4_address"] = aws.StringValue(loadBalancerAddress.PrivateIPv4Address)
}

l = append(l, m)
Expand Down
74 changes: 74 additions & 0 deletions aws/resource_aws_lb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,40 @@ func TestAccAWSLB_networkLoadbalancerEIP(t *testing.T) {
})
}

func TestAccAWSLB_NLB_privateipv4address(t *testing.T) {
var conf elbv2.LoadBalancer
lbName := fmt.Sprintf("testaccawslb-pipv4a-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
resourceName := "aws_lb.lb_test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLBDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLBConfig_networkLoadBalancerPrivateIPV4Address(lbName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists(resourceName, &conf),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most of these check are irrelevant to the specific case, lets leave only the relevant check

resource.TestCheckResourceAttr(resourceName, "internal", "true"),
resource.TestCheckResourceAttr(resourceName, "load_balancer_type", "network"),
resource.TestCheckResourceAttr(resourceName, "subnet_mapping.#", "1"),

resource.TestCheckResourceAttr(resourceName, "access_logs.#", "1"),
resource.TestCheckResourceAttr(resourceName, "access_logs.0.enabled", "false"),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "elasticloadbalancing", regexp.MustCompile(fmt.Sprintf("loadbalancer/net/%s/.+", lbName))),
resource.TestCheckResourceAttrSet(resourceName, "dns_name"),
resource.TestCheckResourceAttr(resourceName, "enable_deletion_protection", "false"),
resource.TestCheckResourceAttr(resourceName, "internal", "true"),
resource.TestCheckResourceAttr(resourceName, "ip_address_type", "ipv4"),
resource.TestCheckResourceAttr(resourceName, "load_balancer_type", "network"),
resource.TestCheckResourceAttr(resourceName, "name", lbName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", "TestAccAWSALB_privateipv4address"),
resource.TestCheckResourceAttrSet(resourceName, "zone_id"),
resource.TestCheckResourceAttr(resourceName, "subnet_mapping.#", "1"),
),
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add import step:

			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},

},
})
}

func TestAccAWSLBBackwardsCompatibility(t *testing.T) {
var conf elbv2.LoadBalancer
lbName := fmt.Sprintf("testaccawslb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
Expand Down Expand Up @@ -1770,6 +1804,46 @@ resource "aws_eip" "lb" {
`, lbName)
}

func testAccAWSLBConfig_networkLoadBalancerPrivateIPV4Address(lbName string) string {
return fmt.Sprintf(`
resource "aws_lb" "lb_test" {
name = "%s"
internal = true
load_balancer_type = "network"

enable_deletion_protection = false

subnet_mapping {
subnet_id = "${aws_subnet.alb_test.id}"
private_ipv4_address = "10.10.0.15"
}

tags = {
Name = "TestAccAWSALB_privateipv4address"
}
}

resource "aws_vpc" "alb_test" {
cidr_block = "10.10.0.0/16"

tags = {
Name = "TestAccAWSALB_privateipv4address"
}
}

resource "aws_subnet" "alb_test" {
vpc_id = "${aws_vpc.alb_test.id}"
cidr_block = "10.10.0.0/21"
map_public_ip_on_launch = true
availability_zone = "us-west-2a"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make test region agnostic and to avoid environment specific failures, can please add the following to the test config:

data "aws_availability_zones" "available" {
  state = "available"

  filter {
    name   = "opt-in-status"
    values = ["opt-in-not-required"]
  }
}

and change the hardcoded az to "${data.aws_availability_zones.available.names[0]}"


tags = {
Name = "TestAccAWSALB_privateipv4address"
}
}
`, lbName)
}

func testAccAWSLBConfigBackwardsCompatibility(lbName string) string {
return fmt.Sprintf(`
resource "aws_alb" "lb_test" {
Expand Down
20 changes: 20 additions & 0 deletions website/docs/r/lb.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ resource "aws_lb" "example" {
}
```

### Specifying private IP addresses for an internal-facing load balancer

```hcl
resource "aws_lb" "example" {
name = "example"
load_balancer_type = "network"

subnet_mapping {
subnet_id = "${aws_subnet.example1.id}"
private_ipv4_address = "10.0.1.15"
}

subnet_mapping {
subnet_id = "${aws_subnet.example2.id}"
private_ipv4_address = "10.0.2.15"
}
}
```

## Argument Reference

~> **NOTE:** Please note that internal LBs can only use `ipv4` as the ip_address_type. You can only change to `dualstack` ip_address_type if the selected subnets are IPv6 enabled.
Expand Down Expand Up @@ -114,6 +133,7 @@ Subnet Mapping (`subnet_mapping`) blocks support the following:

* `subnet_id` - (Required) The id of the subnet of which to attach to the load balancer. You can specify only one subnet per Availability Zone.
* `allocation_id` - (Optional) The allocation ID of the Elastic IP address.
* `private_ipv4_address` - (Optional) A private ipv4 address within the subnet to assign to the internal-facing load balancer.

## Attributes Reference

Expand Down