@@ -434,7 +434,12 @@ func resourceNATGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta
434434 case awstypes .AvailabilityModeZonal :
435435 switch awstypes .ConnectivityType (d .Get ("connectivity_type" ).(string )) {
436436 case awstypes .ConnectivityTypePrivate :
437- if d .HasChanges ("secondary_private_ip_addresses" ) {
437+ countRaw := d .GetRawConfig ().GetAttr ("secondary_private_ip_address_count" )
438+ countConfigured := countRaw .IsKnown () && ! countRaw .IsNull ()
439+ addressesRaw := d .GetRawConfig ().GetAttr ("secondary_private_ip_addresses" )
440+ addressesConfigured := addressesRaw .IsKnown () && ! addressesRaw .IsNull ()
441+
442+ if addressesConfigured && d .HasChange ("secondary_private_ip_addresses" ) {
438443 o , n := d .GetChange ("secondary_private_ip_addresses" )
439444 os , ns := o .(* schema.Set ), n .(* schema.Set )
440445
@@ -476,6 +481,57 @@ func resourceNATGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta
476481 }
477482 }
478483 }
484+
485+ if countConfigured && d .HasChange ("secondary_private_ip_address_count" ) {
486+ o , n := d .GetChange ("secondary_private_ip_address_count" )
487+ oldCount , newCount := o .(int ), n .(int )
488+
489+ delta := newCount - oldCount
490+
491+ if delta > 0 {
492+ input := & ec2.AssignPrivateNatGatewayAddressInput {
493+ NatGatewayId : aws .String (d .Id ()),
494+ PrivateIpAddressCount : aws .Int32 (int32 (delta )),
495+ }
496+
497+ _ , err := conn .AssignPrivateNatGatewayAddress (ctx , input )
498+
499+ if err != nil {
500+ return sdkdiag .AppendErrorf (diags , "assigning EC2 NAT Gateway (%s) private IP address count: %s" , d .Id (), err )
501+ }
502+
503+ if _ , err := waitNATGatewaySecondaryPrivateIPAddressCount (ctx , conn , d .Id (), newCount , d .Timeout (schema .TimeoutUpdate )); err != nil {
504+ return sdkdiag .AppendErrorf (diags , "waiting for EC2 NAT Gateway (%s) secondary private IP address count (%d): %s" , d .Id (), newCount , err )
505+ }
506+ }
507+
508+ if delta < 0 {
509+ removeCount := - delta
510+
511+ sIPs , _ := d .GetChange ("secondary_private_ip_addresses" )
512+ secondaryPrivateIPs := sIPs .(* schema.Set ).List ()
513+
514+ privateIPsToUnassign := secondaryPrivateIPs [:removeCount ]
515+
516+ input := & ec2.UnassignPrivateNatGatewayAddressInput {
517+ NatGatewayId : aws .String (d .Id ()),
518+ PrivateIpAddresses : flex .ExpandStringValueList (privateIPsToUnassign ),
519+ MaxDrainDurationSeconds : aws .Int32 (50 ),
520+ }
521+
522+ _ , err := conn .UnassignPrivateNatGatewayAddress (ctx , input )
523+
524+ if err != nil {
525+ return sdkdiag .AppendErrorf (diags , "unassigning EC2 NAT Gateway (%s) private IP addresses: %s" , d .Id (), err )
526+ }
527+
528+ for _ , privateIP := range flex .ExpandStringValueList (privateIPsToUnassign ) {
529+ if _ , err := waitNATGatewayAddressUnassigned (ctx , conn , d .Id (), privateIP , d .Timeout (schema .TimeoutUpdate )); err != nil {
530+ return sdkdiag .AppendErrorf (diags , "waiting for EC2 NAT Gateway (%s) private IP address (%s) unassign: %s" , d .Id (), privateIP , err )
531+ }
532+ }
533+ }
534+ }
479535 case awstypes .ConnectivityTypePublic :
480536 if ! d .GetRawConfig ().GetAttr ("secondary_allocation_ids" ).IsNull () && d .HasChanges ("secondary_allocation_ids" ) {
481537 o , n := d .GetChange ("secondary_allocation_ids" )
@@ -656,15 +712,18 @@ func resourceNATGatewayCustomizeDiff(ctx context.Context, diff *schema.ResourceD
656712 return fmt .Errorf (`secondary_allocation_ids is not supported with connectivity_type = "%s"` , connectivityType )
657713 }
658714
659- if diff .Id () != "" && diff .HasChange ("secondary_private_ip_address_count" ) {
660- if v := diff .GetRawConfig ().GetAttr ("secondary_private_ip_address_count" ); v .IsKnown () && ! v .IsNull () {
661- if err := diff .ForceNew ("secondary_private_ip_address_count" ); err != nil {
662- return fmt .Errorf ("setting secondary_private_ip_address_count to ForceNew: %w" , err )
663- }
715+ countRaw := diff .GetRawConfig ().GetAttr ("secondary_private_ip_address_count" )
716+ countConfigured := countRaw .IsKnown () && ! countRaw .IsNull ()
717+ addressesRaw := diff .GetRawConfig ().GetAttr ("secondary_private_ip_addresses" )
718+ addressesConfigured := addressesRaw .IsKnown () && ! addressesRaw .IsNull ()
719+
720+ if diff .Id () != "" && countConfigured && diff .HasChange ("secondary_private_ip_address_count" ) {
721+ if err := diff .SetNewComputed ("secondary_private_ip_addresses" ); err != nil {
722+ return fmt .Errorf ("setting secondary_private_ip_addresses to Computed: %w" , err )
664723 }
665724 }
666725
667- if diff .Id () != "" && diff .HasChange ("secondary_private_ip_addresses" ) {
726+ if diff .Id () != "" && addressesConfigured && diff .HasChange ("secondary_private_ip_addresses" ) {
668727 if err := diff .SetNewComputed ("secondary_private_ip_address_count" ); err != nil {
669728 return fmt .Errorf ("setting secondary_private_ip_address_count to Computed: %w" , err )
670729 }
0 commit comments