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

IPv6 subnet mapping for lb #17229

Merged
merged 5 commits into from
Feb 4, 2021
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
4 changes: 4 additions & 0 deletions aws/data_source_aws_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func dataSourceAwsLb() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"ipv6_address": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
Expand Down
11 changes: 11 additions & 0 deletions aws/resource_aws_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ func resourceAwsLb() *schema.Resource {
Required: true,
ForceNew: true,
},
"ipv6_address": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IsIPv6Address,
},
"outpost_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -298,6 +304,10 @@ func resourceAwsLbCreate(d *schema.ResourceData, meta interface{}) error {
if subnetMap["private_ipv4_address"].(string) != "" {
elbOpts.SubnetMappings[i].PrivateIPv4Address = aws.String(subnetMap["private_ipv4_address"].(string))
}

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

Expand Down Expand Up @@ -668,6 +678,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)
m["ipv6_address"] = aws.StringValue(loadBalancerAddress.IPv6Address)
}

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

func TestAccAWSLB_IPv6SubnetMapping(t *testing.T) {
var conf elbv2.LoadBalancer
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_lb.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckAWSLBDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLBConfig_IPv6(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBExists(resourceName, &conf),
resource.TestMatchTypeSetElemNestedAttrs(resourceName, "subnet_mapping.*", map[string]*regexp.Regexp{
"ipv6_address": regexp.MustCompile("[a-f0-6]+:[a-f0-6:]+"),
}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"drop_invalid_header_fields",
"enable_http2",
"idle_timeout",
},
},
},
})
}

func TestAccAWSLB_LoadBalancerType_Gateway_EnableCrossZoneLoadBalancing(t *testing.T) {
var conf elbv2.LoadBalancer
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -1956,6 +1989,57 @@ resource "aws_lb" "test" {
`, rName))
}

func testAccAWSLBConfig_IPv6(rName string) string {
return composeConfig(
testAccAvailableAZsNoOptInConfig(),
fmt.Sprintf(`
resource "aws_vpc" "test" {
assign_generated_ipv6_cidr_block = true
cidr_block = "10.10.10.0/25"

tags = {
Name = "tf-acc-test-load-balancer"
}
}

resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.test.id

tags = {
Name = "main"
}
}

resource "aws_subnet" "test" {
availability_zone = data.aws_availability_zones.available.names[0]
cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 2, 0)
ipv6_cidr_block = cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, 16)
vpc_id = aws_vpc.test.id

tags = {
Name = "tf-acc-test-load-balancer"
}
}

resource "aws_lb" "test" {
name = %[1]q
load_balancer_type = "network"
enable_deletion_protection = false

subnet_mapping {
subnet_id = aws_subnet.test.id
ipv6_address = cidrhost(cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, 16), 5)
}

tags = {
Name = "TestAccAWSALB_ipv6address"
}

depends_on = [aws_internet_gateway.gw]
}
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
`, rName))
}

func testAccAWSLBConfig_LoadBalancerType_Gateway_EnableCrossZoneLoadBalancing(rName string, enableCrossZoneLoadBalancing bool) string {
return composeConfig(
testAccAvailableAZsNoOptInConfig(),
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/lb.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,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.
* `ipv6_address` - (Optional) An ipv6 address within the subnet to assign to the internet-facing load balancer.

## Attributes Reference

Expand Down