@@ -474,6 +474,72 @@ func resourceNATGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta
474474 }
475475 }
476476 }
477+
478+ if v := d .GetRawConfig ().GetAttr ("secondary_private_ip_address_count" ); v .IsKnown () && ! v .IsNull () && d .GetRawConfig ().GetAttr ("secondary_private_ip_addresses" ).IsNull () {
479+ desiredCount64 , _ := v .AsBigFloat ().Int64 ()
480+ desiredCount := int (desiredCount64 )
481+
482+ natGateway , err := findNATGatewayByID (ctx , conn , d .Id ())
483+ if err != nil {
484+ return sdkdiag .AppendErrorf (diags , "reading EC2 NAT Gateway (%s): %s" , d .Id (), err )
485+ }
486+
487+ var secondaryPrivateIPs []string
488+ for _ , natGatewayAddress := range natGateway .NatGatewayAddresses {
489+ if ! aws .ToBool (natGatewayAddress .IsPrimary ) {
490+ if privateIP := aws .ToString (natGatewayAddress .PrivateIp ); privateIP != "" {
491+ secondaryPrivateIPs = append (secondaryPrivateIPs , privateIP )
492+ }
493+ }
494+ }
495+
496+ currentCount := len (secondaryPrivateIPs )
497+ delta := desiredCount - currentCount
498+
499+ if delta > 0 {
500+ input := & ec2.AssignPrivateNatGatewayAddressInput {
501+ NatGatewayId : aws .String (d .Id ()),
502+ PrivateIpAddressCount : aws .Int32 (int32 (delta )),
503+ }
504+
505+ _ , err := conn .AssignPrivateNatGatewayAddress (ctx , input )
506+
507+ if err != nil {
508+ return sdkdiag .AppendErrorf (diags , "assigning EC2 NAT Gateway (%s) private IP address count: %s" , d .Id (), err )
509+ }
510+ } else if delta < 0 {
511+ removeCount := - delta
512+
513+ if len (secondaryPrivateIPs ) < removeCount {
514+ return sdkdiag .AppendErrorf (diags , "cannot unassign %d private IP addresses from EC2 NAT Gateway (%s), only %d secondary addresses are available" , removeCount , d .Id (), len (secondaryPrivateIPs ))
515+ }
516+
517+ privateIPsToUnassign := secondaryPrivateIPs [:removeCount ]
518+
519+ input := & ec2.UnassignPrivateNatGatewayAddressInput {
520+ NatGatewayId : aws .String (d .Id ()),
521+ PrivateIpAddresses : privateIPsToUnassign ,
522+ }
523+
524+ _ , err := conn .UnassignPrivateNatGatewayAddress (ctx , input )
525+
526+ if err != nil {
527+ return sdkdiag .AppendErrorf (diags , "unassigning EC2 NAT Gateway (%s) private IP addresses: %s" , d .Id (), err )
528+ }
529+
530+ for _ , privateIP := range privateIPsToUnassign {
531+ if _ , err := waitNATGatewayAddressUnassigned (ctx , conn , d .Id (), privateIP , d .Timeout (schema .TimeoutUpdate )); err != nil {
532+ return sdkdiag .AppendErrorf (diags , "waiting for EC2 NAT Gateway (%s) private IP address (%s) unassign: %s" , d .Id (), privateIP , err )
533+ }
534+ }
535+ }
536+
537+ if delta != 0 {
538+ if _ , err := waitNATGatewaySecondaryPrivateIPAddressCount (ctx , conn , d .Id (), desiredCount , d .Timeout (schema .TimeoutUpdate )); err != nil {
539+ return sdkdiag .AppendErrorf (diags , "waiting for EC2 NAT Gateway (%s) secondary private IP address count (%d): %s" , d .Id (), desiredCount , err )
540+ }
541+ }
542+ }
477543 case awstypes .ConnectivityTypePublic :
478544 if ! d .GetRawConfig ().GetAttr ("secondary_allocation_ids" ).IsNull () && d .HasChanges ("secondary_allocation_ids" ) {
479545 o , n := d .GetChange ("secondary_allocation_ids" )
@@ -654,17 +720,11 @@ func resourceNATGatewayCustomizeDiff(ctx context.Context, diff *schema.ResourceD
654720 return fmt .Errorf (`secondary_allocation_ids is not supported with connectivity_type = "%s"` , connectivityType )
655721 }
656722
657- if diff .Id () != "" && diff .HasChange ("secondary_private_ip_address_count" ) {
658- if v := diff .GetRawConfig ().GetAttr ("secondary_private_ip_address_count" ); v .IsKnown () && ! v .IsNull () {
659- if err := diff .ForceNew ("secondary_private_ip_address_count" ); err != nil {
660- return fmt .Errorf ("setting secondary_private_ip_address_count to ForceNew: %w" , err )
661- }
662- }
663- }
664-
665723 if diff .Id () != "" && diff .HasChange ("secondary_private_ip_addresses" ) {
666- if err := diff .SetNewComputed ("secondary_private_ip_address_count" ); err != nil {
667- return fmt .Errorf ("setting secondary_private_ip_address_count to Computed: %w" , err )
724+ if v := diff .GetRawConfig ().GetAttr ("secondary_private_ip_addresses" ); v .IsKnown () && ! v .IsNull () {
725+ if err := diff .SetNewComputed ("secondary_private_ip_address_count" ); err != nil {
726+ return fmt .Errorf ("setting secondary_private_ip_address_count to Computed: %w" , err )
727+ }
668728 }
669729 }
670730 case awstypes .ConnectivityTypePublic :
0 commit comments