Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Assigning multiple role policies using aws_iam_role_policy_attachment fails with ConcurrentModification error #34371

Closed
jandryse opened this issue Nov 13, 2023 · 18 comments · Fixed by #34378
Labels
bug Addresses a defect in current functionality. service/iam Issues and PRs that pertain to the iam service.
Milestone

Comments

@jandryse
Copy link

jandryse commented Nov 13, 2023

Terraform Core Version

1.6.2

AWS Provider Version

5.25.0

Affected Resource(s)

aws_iam_role_policy_attachment

Expected Behavior

It should be possible to assign multiple policies to the same role.

Actual Behavior

When multiple policies are assigned, it very often fails with ConcurrentModification exception. It may be related to some changes in AWS, since the same code, with the same version of terraform and aws provider was working last week.

Relevant Error/Panic Output Snippet

Error: attaching policy arn:aws:iam::12545454:policy/test-policy-5 to IAM Role test-role: ConcurrentModification: Another request updating the entity is in progress. Please try again later.
│ 	status code: 409, request id: aaaaa-bbbbb-ccccc-ddddd-fea34e635b6a
│
│   with aws_iam_role_policy_attachment.test_attach[5],
│   on init.tf line 101, in resource "aws_iam_role_policy_attachment" "test_attach":
│  101: resource "aws_iam_role_policy_attachment" "test_attach" {

Terraform Configuration Files

variable "profile" {}
variable "region" {}

provider "aws" {
  profile = var.profile
  region  = var.region
}

terraform {
  required_version = ">= 1.0"
  backend "s3" {
    key = "spikes/role-benchmark/terraform.tfstate"
  }
  required_providers {
    aws = {
      source ="hashicorp/aws"
      version = "= 5.25.0"
    }

  }
}


data "aws_iam_policy_document" "assume_role" {
  statement {
    effect = "Allow"

    principals {
      type        = "Service"
      identifiers = ["ec2.amazonaws.com"]
    }

    actions = ["sts:AssumeRole"]
  }
}

resource "aws_iam_role" "role" {
  name               = "test-role"
  assume_role_policy = data.aws_iam_policy_document.assume_role.json
}

data "aws_iam_policy_document" "policy" {
  statement {
    effect    = "Allow"
    actions   = ["ec2:Describe*"]
    resources = ["*"]
  }
}

resource "aws_iam_policy" "policy" {
  count = 8
  name        = "test-policy-${count.index}"
  description = "A test policy"
  policy      = data.aws_iam_policy_document.policy.json
}


resource "aws_iam_role_policy_attachment" "test_attach" {
  count = 8
  role       = aws_iam_role.role.name
  policy_arn = aws_iam_policy.policy[count.index].arn
}

Steps to Reproduce

apply the provided terraform. Or create new terraform, which assigns 8 polices to a role

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

@jandryse jandryse added the bug Addresses a defect in current functionality. label Nov 13, 2023
Copy link

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@github-actions github-actions bot added the service/iam Issues and PRs that pertain to the iam service. label Nov 13, 2023
@terraform-aws-provider terraform-aws-provider bot added the needs-triage Waiting for first response or review from a maintainer. label Nov 13, 2023
@flichtenheld
Copy link
Contributor

I updated last week from 5.20.0 to 5.24.0 and started to see these errors. So I think this was introduced earlier than 5.25.0

@dezren39
Copy link

I switched
from (which pulled 5.25.0)

      version = "~>5.0"

to

      version = "5.24.0"

and apply now succeeds.

@flichtenheld
Copy link
Contributor

This does not fail reliably for me. So it seems to depend on a race condition. Something to keep in mind when trying to bisect.

@flichtenheld
Copy link
Contributor

This does not fail reliably for me. So it seems to depend on a race condition. Something to keep in mind when trying to bisect.

Sorry, to clarify: It does not fail reliably for me with our actual code. Tested the example given in the report and that fails always. Probably the high amount of attachments makes it easier to hit the race condition.

@rhowe
Copy link

rhowe commented Nov 13, 2023

I have started seeing this with version 3.76.1. I suspect AWS IAM behaviour has changed.

@jarpat
Copy link

jarpat commented Nov 13, 2023

Just wanted to report I am also seeing this with:

bash-4.2# terraform version
Terraform v1.4.5
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v5.4.0

as of today.

@flichtenheld
Copy link
Contributor

I have started seeing this with version 3.76.1. I suspect AWS IAM behaviour has changed.

Yes, I tried bisecting this. But found no version with which it actually works. So it looks indeed like AWS-side change.

@flichtenheld
Copy link
Contributor

terraform apply -parallelism=1 works around the problem.

flichtenheld added a commit to flichtenheld/terraform-provider-aws that referenced this issue Nov 13, 2023
Fixes hashicorp#34371

Signed-off-by: Frank Lichtenheld <frank@lichtenheld.com>
@mgusiew-guide
Copy link
Contributor

I haven't changed Terraform or Terraform AWS version recently and I started to see this today. I also observe the problem on older branches. So this is regression on AWS side. I agree that retries on conflict may be needed to solve this problem.

@venkatamutyala
Copy link

venkatamutyala commented Nov 13, 2023

We had a regression suite run on our end early Friday without this issue. Saturday/Sunday have been failures. We run our regressions against us-west-2 but given IAM is a global service region specifics may not matter in this case. I also made some changes late yesterday to run against us-east-1 and was able to replicate the issue there to.

We are using 1.5.5 of terraform and the latest AWS provider. Downgrading a few minors didn't help at all. At the moment we just re-run the terraform and it's working.

@flichtenheld
Copy link
Contributor

Tried to implement some testing for my patch. But now I can't reproduce the issue anymore. Maybe AWS fixed their stuff?

@justinretzolk justinretzolk removed the needs-triage Waiting for first response or review from a maintainer. label Nov 13, 2023
@venkatamutyala
Copy link

@flichtenheld thanks for making the PR. I just tried recreating the issue twice this evening (Pacific Time) and was unsuccessful. My team's infra-regression suites are working fine this afternoon, which is great. Hopefully, your PR can get merged in soon so that we can avoid this issue in the future.

@r0bnet
Copy link

r0bnet commented Nov 14, 2023

We have the same issue but with aws_iam_user_policy_attachment and it started last week on Friday (10.11.2023). It's flaky and doesn't seem to appear all the times. It didn't happen for us anymore during the last 24 hours so maybe it's fixed on AWS side? Anyway thanks @flichtenheld for the PR.

@mgusiew-guide
Copy link
Contributor

FTR the error went away yesterday afternoon so I contacted AWS to get some explanation. I was told that recently AWS made changes in IAM API, in particular AttachRolePolicy, AttachUserPolicy, and AttachGroupPolicy started to throw ConcurrentModificationException in case of concurrent requests. After the change AWS noticed elevated ConcurrentModification errors when calling IAM APIs. Because of that the issue has been resolved and the service is operating normally. Will try to clarify if "resolved" means that the feature was rolled back or something else.

@github-actions github-actions bot added this to the v5.26.0 milestone Nov 14, 2023
@mgusiew-guide
Copy link
Contributor

Got some updates from AWS side:

  • The ConcurrentModificationException related change was rolled back
  • Currently, it is not planned to roll out the changes in the future since it is impacting the customer's environment. There will be an announcement or notification regarding this if it is further planned to be implemented in the future

Hope that helps

Copy link

This functionality has been released in v5.26.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/iam Issues and PRs that pertain to the iam service.
Projects
None yet
9 participants