This template associates an existing ElastiCache user with an existing user group and handles changes in user IDs with lifecycle rules.

Terraform Template

resource "aws_elasticache_user" "default" {
  access_string = "on ~app::* -@all +@read +@hash +@bitmap +@geo -setbit -bitfield -hset -hsetnx -hmset -hincrby -hincrbyfloat -hdel -bitop -geoadd -georadius -georadiusbymember"
  engine = "REDIS"
  passwords = ["password123456789"]
  user_id = "defaultUserID"
  user_name = "default"
}

resource "aws_elasticache_user" "example" {
  access_string = "on ~app::* -@all +@read +@hash +@bitmap +@geo -setbit -bitfield -hset -hsetnx -hmset -hincrby -hincrbyfloat -hdel -bitop -geoadd -georadius -georadiusbymember"
  engine = "REDIS"
  passwords = ["password123456789"]
  user_id = "exampleUserID"
  user_name = "exampleuser"
}

resource "aws_elasticache_user_group" "example" {
  engine = "REDIS"

  lifecycle {
    ignore_changes = ["user_ids"]
  }
  user_group_id = "userGroupId"
  user_ids = [aws_elasticache_user.default.user_id]
}

resource "aws_elasticache_user_group_association" "example" {
  user_group_id = aws_elasticache_user_group.example.user_group_id
  user_id = aws_elasticache_user.example.user_id
}