@@ -432,7 +432,12 @@ func resourceNATGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta
432432 case awstypes .AvailabilityModeZonal :
433433 switch awstypes .ConnectivityType (d .Get ("connectivity_type" ).(string )) {
434434 case awstypes .ConnectivityTypePrivate :
435- if d .HasChanges ("secondary_private_ip_addresses" ) {
435+ countRaw := d .GetRawConfig ().GetAttr ("secondary_private_ip_address_count" )
436+ countConfigured := countRaw .IsKnown () && ! countRaw .IsNull ()
437+ addressesRaw := d .GetRawConfig ().GetAttr ("secondary_private_ip_addresses" )
438+ addressesConfigured := addressesRaw .IsKnown () && ! addressesRaw .IsNull ()
439+
440+ if addressesConfigured && d .HasChange ("secondary_private_ip_addresses" ) {
436441 o , n := d .GetChange ("secondary_private_ip_addresses" )
437442 os , ns := o .(* schema.Set ), n .(* schema.Set )
438443
@@ -474,6 +479,57 @@ func resourceNATGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta
474479 }
475480 }
476481 }
482+
483+ if countConfigured && d .HasChange ("secondary_private_ip_address_count" ) {
484+ o , n := d .GetChange ("secondary_private_ip_address_count" )
485+ oldCount , newCount := o .(int ), n .(int )
486+
487+ delta := newCount - oldCount
488+
489+ if delta > 0 {
490+ input := & ec2.AssignPrivateNatGatewayAddressInput {
491+ NatGatewayId : aws .String (d .Id ()),
492+ PrivateIpAddressCount : aws .Int32 (int32 (delta )),
493+ }
494+
495+ _ , err := conn .AssignPrivateNatGatewayAddress (ctx , input )
496+
497+ if err != nil {
498+ return sdkdiag .AppendErrorf (diags , "assigning EC2 NAT Gateway (%s) private IP address count: %s" , d .Id (), err )
499+ }
500+
501+ if _ , err := waitNATGatewaySecondaryPrivateIPAddressCount (ctx , conn , d .Id (), newCount , d .Timeout (schema .TimeoutUpdate )); err != nil {
502+ return sdkdiag .AppendErrorf (diags , "waiting for EC2 NAT Gateway (%s) secondary private IP address count (%d): %s" , d .Id (), newCount , err )
503+ }
504+ }
505+
506+ if delta < 0 {
507+ removeCount := - delta
508+
509+ sIPs , _ := d .GetChange ("secondary_private_ip_addresses" )
510+ secondaryPrivateIPs := sIPs .(* schema.Set ).List ()
511+
512+ privateIPsToUnassign := secondaryPrivateIPs [:removeCount ]
513+
514+ input := & ec2.UnassignPrivateNatGatewayAddressInput {
515+ NatGatewayId : aws .String (d .Id ()),
516+ PrivateIpAddresses : flex .ExpandStringValueList (privateIPsToUnassign ),
517+ MaxDrainDurationSeconds : aws .Int32 (50 ),
518+ }
519+
520+ _ , err := conn .UnassignPrivateNatGatewayAddress (ctx , input )
521+
522+ if err != nil {
523+ return sdkdiag .AppendErrorf (diags , "unassigning EC2 NAT Gateway (%s) private IP addresses: %s" , d .Id (), err )
524+ }
525+
526+ for _ , privateIP := range flex .ExpandStringValueList (privateIPsToUnassign ) {
527+ if _ , err := waitNATGatewayAddressUnassigned (ctx , conn , d .Id (), privateIP , d .Timeout (schema .TimeoutUpdate )); err != nil {
528+ return sdkdiag .AppendErrorf (diags , "waiting for EC2 NAT Gateway (%s) private IP address (%s) unassign: %s" , d .Id (), privateIP , err )
529+ }
530+ }
531+ }
532+ }
477533 case awstypes .ConnectivityTypePublic :
478534 if ! d .GetRawConfig ().GetAttr ("secondary_allocation_ids" ).IsNull () && d .HasChanges ("secondary_allocation_ids" ) {
479535 o , n := d .GetChange ("secondary_allocation_ids" )
@@ -654,15 +710,18 @@ func resourceNATGatewayCustomizeDiff(ctx context.Context, diff *schema.ResourceD
654710 return fmt .Errorf (`secondary_allocation_ids is not supported with connectivity_type = "%s"` , connectivityType )
655711 }
656712
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- }
713+ countRaw := diff .GetRawConfig ().GetAttr ("secondary_private_ip_address_count" )
714+ countConfigured := countRaw .IsKnown () && ! countRaw .IsNull ()
715+ addressesRaw := diff .GetRawConfig ().GetAttr ("secondary_private_ip_addresses" )
716+ addressesConfigured := addressesRaw .IsKnown () && ! addressesRaw .IsNull ()
717+
718+ if diff .Id () != "" && countConfigured && diff .HasChange ("secondary_private_ip_address_count" ) {
719+ if err := diff .SetNewComputed ("secondary_private_ip_addresses" ); err != nil {
720+ return fmt .Errorf ("setting secondary_private_ip_addresses to Computed: %w" , err )
662721 }
663722 }
664723
665- if diff .Id () != "" && diff .HasChange ("secondary_private_ip_addresses" ) {
724+ if diff .Id () != "" && addressesConfigured && diff .HasChange ("secondary_private_ip_addresses" ) {
666725 if err := diff .SetNewComputed ("secondary_private_ip_address_count" ); err != nil {
667726 return fmt .Errorf ("setting secondary_private_ip_address_count to Computed: %w" , err )
668727 }
0 commit comments