@@ -455,16 +455,24 @@ func (p *RedisProvider) TagElasticacheNode(ctx context.Context, elasticacheClien
455455 // need arn in the following format arn:aws:elasticache:us-east-1:1234567890:cluster:my-mem-cluster
456456 arn := fmt .Sprintf ("arn:aws:elasticache:%s:%s:cluster:%s" , region , * id .Account , * cache .CacheClusterId )
457457
458+ // Get default tags
458459 cacheTags , clusterID , err := p .getDefaultElasticacheTags (ctx , r )
459460 if err != nil {
460461 msg := "Failed to build default tags"
461462 return croType .StatusMessage (msg ), errorUtil .Wrapf (err , msg )
462463 }
463464
464- // add tags
465+ // Filter out already applied tags
466+ filteredTags , err := filterAlreadyAppliedTags (ctx , elasticacheClient , arn , cacheTags )
467+ if err != nil {
468+ msg := "Failed to filter already applied tags"
469+ return croType .StatusMessage (msg ), errorUtil .Wrapf (err , msg )
470+ }
471+
472+ // Add only new/changed tags
465473 _ , err = elasticacheClient .AddTagsToResource (ctx , & elasticache.AddTagsToResourceInput {
466474 ResourceName : aws .String (arn ),
467- Tags : cacheTags ,
475+ Tags : filteredTags ,
468476 })
469477 if err != nil {
470478 msg := "failed to add tags to aws elasticache :"
@@ -1278,3 +1286,32 @@ func validServiceUpdateStates(status string) bool {
12781286 }
12791287 return false
12801288}
1289+
1290+ // filterAlreadyAppliedTags removes tags from `desired` that already exist (same key and value) on the resource.
1291+ func filterAlreadyAppliedTags (ctx context.Context , client ElastiCacheAPI , resourceARN string , desired []elasticachetypes.Tag ) ([]elasticachetypes.Tag , error ) {
1292+ // List current tags on the resource
1293+ resp , err := client .ListTagsForResource (ctx , & elasticache.ListTagsForResourceInput {
1294+ ResourceName : aws .String (resourceARN ),
1295+ })
1296+ if err != nil {
1297+ return nil , err
1298+ }
1299+
1300+ // Build a map of current tags
1301+ currentTags := make (map [string ]string )
1302+ for _ , tag := range resp .TagList {
1303+ currentTags [* tag .Key ] = * tag .Value
1304+ }
1305+
1306+ // Filter out tags that are already applied with same value
1307+ var filtered []elasticachetypes.Tag
1308+ for _ , tag := range desired {
1309+ val , exists := currentTags [* tag .Key ]
1310+ if ! exists || val != * tag .Value {
1311+ // Either tag doesn't exist or value is different — keep it
1312+ filtered = append (filtered , tag )
1313+ }
1314+ }
1315+
1316+ return filtered , nil
1317+ }
0 commit comments