Description
aws_iam_policy_attachment manages all users, groups and roles attached to one managed policy. When moving users to a group, keep every other attachment you intend to retain in the resource.
Potential impact
Omitting an identity can revoke its access. Moving a broad policy unchanged can grant those permissions to every group member.
Remediation
Limit the policy to required actions and resources. Add approved users to the group and verify access through it before removing the direct attachment. Review the plan for unintended removals.
Examples
The example changes users to groups. Define the user and group separately. Both excerpts still allow all EC2, S3, Lambda and CloudWatch actions on all resources.
Before
hcl
resource "aws_iam_policy_attachment" "example" {
name = "excess_policy"
users = [aws_iam_user.user.name]
policy_arn = aws_iam_policy.example.arn
}
resource "aws_iam_policy" "example" {
name = "excess_policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:*",
"s3:*",
"lambda:*",
"cloudwatch:*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
After
hcl
resource "aws_iam_policy_attachment" "example" {
name = "excess_policy"
groups = [aws_iam_group.group.name]
policy_arn = aws_iam_policy.example.arn
}
resource "aws_iam_policy" "example" {
name = "excess_policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:*",
"s3:*",
"lambda:*",
"cloudwatch:*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}