Terraform AWS IAM Users Added and Removed from IAM Groups on Alternate terraform apply commands

Viewed 652

I have an oddity with a bunch of IAM Users, who are added and alternatively removed from an IAM Group on subsequent terraform apply operations. Even though nothing else has changed.

I know that these are not fully functioning IAM users, as the login profiles still need to be added, but the plan was just to save some effort and complete the user set-up manually.

Using terraform 13.5, on ubuntu 20.1.

# main.tf
# Create a group for them
resource "aws_iam_group" "proj" {
    name = "proj-AdminGroup-Nov-2020"
}

# Create the individual users
resource "aws_iam_user" "example" {
  count = length(var.iam_users)
  name = element(var.iam_users,count.index)
  force_destroy = true
}

# Attach the standard AdministratorAccess policy to the group
# Use 'data' to get the arn of the policy
data "aws_iam_policy" "AdministratorAccess" {
    arn = "arn:aws:iam::aws:policy/AdministratorAccess"
}
# Attach the policy to the group
resource "aws_iam_group_policy_attachment" "test-attach" {
  group      = aws_iam_group.proj.name
  policy_arn = data.aws_iam_policy.AdministratorAccess.arn
}
# Assign the Users to the group
resource "aws_iam_group_membership" "team" {
  name = "tf-proj-group-membership"
  count = length(var.iam_users)

  users = [
      element(aws_iam_user.example.*.name, count.index)
      ]

  group = aws_iam_group.proj.name
}

# variables.tf
variable "iam_users" {
    type = list
    default =[
        "user1", 
        "user2"
    ]
}

Status after the first terraform apply. enter image description here

Status after the second.

enter image description here

The 2nd terraform apply output:

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # aws_iam_group_membership.team[0] will be updated in-place
  ~ resource "aws_iam_group_membership" "team" {
        group = "proj-AdminGroup-Nov-2020"
        id    = "tf-proj-group-membership"
        name  = "tf-proj-group-membership"
      ~ users = [
            "user1",
          - "user2",
        ]
    }

  # aws_iam_group_membership.team[1] will be updated in-place
  ~ resource "aws_iam_group_membership" "team" {
        group = "proj-AdminGroup-Nov-2020"
        id    = "tf-proj-group-membership"
        name  = "tf-proj-group-membership"
      ~ users = [
          - "user1",
            "user2",
        ]
    }

Plan: 0 to add, 2 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes
2 Answers

I'm not really sure why you are using count = length(var.iam_users) in your aws_iam_group_membership. Normally, you would just do this this way:

resource "aws_iam_group_membership" "team" {
  name = "tf-proj-group-membership"

  users = aws_iam_user.example[*].name

  group = aws_iam_group.proj.name
}

The above does work as expected and does not show the behavior you are describing.

Related