Skip to content

Commit

Permalink
allow to add subnets/ mappings to a lb
Browse files Browse the repository at this point in the history
  • Loading branch information
elchead committed Aug 28, 2023
1 parent ff9ecfe commit 525b02a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .changelog/33205.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_lb: Allow to add subnets without forcing recreation
```
28 changes: 21 additions & 7 deletions internal/service/elbv2/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,15 @@ func ResourceLoadBalancer() *schema.Resource {
Type: schema.TypeSet,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allocation_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"ipv6_address": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IsIPv6Address,
},
"outpost_id": {
Expand All @@ -237,13 +234,11 @@ func ResourceLoadBalancer() *schema.Resource {
"private_ipv4_address": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IsIPv4Address,
},
"subnet_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
},
Expand Down Expand Up @@ -604,6 +599,19 @@ func resourceLoadBalancerUpdate(ctx context.Context, d *schema.ResourceData, met
return sdkdiag.AppendErrorf(diags, "failure Setting LB Subnets: %s", err)
}
}
if d.HasChange("subnet_mapping") && !d.IsNewResource() {
params := &elbv2.SetSubnetsInput{
LoadBalancerArn: aws.String(d.Id()),
}
if v, ok := d.GetOk("subnet_mapping"); ok && v.(*schema.Set).Len() > 0 {
params.SubnetMappings = expandSubnetMappings(v.(*schema.Set).List())
}

_, err := conn.SetSubnetsWithContext(ctx, params)
if err != nil {
return sdkdiag.AppendErrorf(diags, "failure Setting LB Subnet Mapping: %s", err)
}
}

if d.HasChange("ip_address_type") {
params := &elbv2.SetIpAddressTypeInput{
Expand Down Expand Up @@ -1040,12 +1048,18 @@ func customizeDiffNLB(_ context.Context, diff *schema.ResourceDiff, v interface{
// Get diff for subnets.
o, n := diff.GetChange("subnets")
os, ns := o.(*schema.Set), n.(*schema.Set)

if add, del := ns.Difference(os).List(), os.Difference(ns).List(); len(del) > 0 || len(add) > 0 {
if del := os.Difference(ns).List(); len(del) > 0 {
if err := diff.ForceNew("subnets"); err != nil {
return err
}
}
o, n = diff.GetChange("subnet_mapping")
os, ns = o.(*schema.Set), n.(*schema.Set)
if del := os.Difference(ns).List(); len(del) > 0 {
if err := diff.ForceNew("subnet_mapping"); err != nil {
return err
}
}

// Get diff for security groups.
o, n = diff.GetChange("security_groups")
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/lb.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Terraform will autogenerate a name beginning with `tf-lb`.
* `preserve_host_header` - (Optional) Indicates whether the Application Load Balancer should preserve the Host header in the HTTP request and send it to the target without any change. Defaults to `false`.
* `subnet_mapping` - (Optional) A subnet mapping block as documented below.
* `subnets` - (Optional) A list of subnet IDs to attach to the LB. Subnets
cannot be updated for Load Balancers of type `network`. Changing this value
can only be added for Load Balancers of type `network` (see [Availablity Zones](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/network-load-balancers.html#availability-zones)). Deleting a subnet
for load balancers of type `network` will force a recreation of the resource.
* `xff_header_processing_mode` - (Optional) Determines how the load balancer modifies the `X-Forwarded-For` header in the HTTP request before sending the request to the target. The possible values are `append`, `preserve`, and `remove`. Only valid for Load Balancers of type `application`. The default is `append`.
* `tags` - (Optional) A map of tags to assign to the resource. 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.
Expand Down

0 comments on commit 525b02a

Please sign in to comment.