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]: ECR repository policy creation fails when using newly created IAM role #39064

Closed
imre-kerr-sb1 opened this issue Aug 28, 2024 · 5 comments · Fixed by #39190
Closed

[Bug]: ECR repository policy creation fails when using newly created IAM role #39064

imre-kerr-sb1 opened this issue Aug 28, 2024 · 5 comments · Fixed by #39190
Labels
bug Addresses a defect in current functionality. service/ecr Issues and PRs that pertain to the ecr service. service/iam Issues and PRs that pertain to the iam service.
Milestone

Comments

@imre-kerr-sb1
Copy link

Terraform Core Version

1.9.5

AWS Provider Version

5.64.0

Affected Resource(s)

  • aws_ecr_repository_policy
  • aws_iam_role

Expected Behavior

terraform apply should succeed.

Actual Behavior

Applying the repository policy fails the first time. Running it a second time succeeds.

Relevant Error/Panic Output Snippet

aws_ecr_repository.test: Creating...
aws_iam_role.test: Creating...
aws_ecr_repository.test: Creation complete after 1s [id=test-ecr-attach-policy-bug]
aws_iam_role.test: Creation complete after 1s [id=test-ecr-attach-policy-bug]
data.aws_iam_policy_document.describe_ecr_images: Reading...
data.aws_iam_policy_document.describe_ecr_images: Read complete after 0s [id=1275846024]
aws_ecr_repository_policy.test: Creating...
╷
│ Error: putting ECR Repository Policy (test-ecr-attach-policy-bug): operation error ECR: SetRepositoryPolicy, https response error StatusCode: 400, RequestID: ef06680f-271a-4909-99d3-62e8cedad7c1, InvalidParameterException: Invalid parameter at 'PolicyText' failed to satisfy constraint: 'Principal not found'
│ 
│   with aws_ecr_repository_policy.test,
│   on main.tf line 16, in resource "aws_ecr_repository_policy" "test":
│   16: resource "aws_ecr_repository_policy" "test" {
│ 
╵

Terraform Configuration Files

terraform {
  required_version = ">= 1.9.5"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.64.0"
    }
  }
}

resource "aws_ecr_repository" "test" {
  name = "test-ecr-attach-policy-bug"
}

resource "aws_ecr_repository_policy" "test" {
  repository = "${aws_ecr_repository.test.name}"
  policy     = "${data.aws_iam_policy_document.describe_ecr_images.json}"
}

data "aws_iam_policy_document" "describe_ecr_images" {
  statement {
    sid    = "DescribeImages"
    effect = "Allow"

    principals {
      type        = "AWS"
      identifiers = ["${aws_iam_role.test.arn}"]
    }

    actions = [
      "ecr:DescribeImages"
    ]
  }
}

resource "aws_iam_role" "test" {
  name = "test-ecr-attach-policy-bug"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Effect": "Allow",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      }
    }
  ]
}
EOF
}

Steps to Reproduce

terraform apply

Debug Output

https://gist.github.com/imre-kerr-sb1/79968926a388f95da1bd6c994eaf57a1

Panic Output

No response

Important Factoids

No response

References

This is a regression of #1164, which was fixed in #1165. The retry logic checks the error message:

	_, err = tfresource.RetryWhenIsAErrorMessageContains[*types.InvalidParameterException](ctx, propagationTimeout, func() (interface{}, error) {
		return conn.SetRepositoryPolicy(ctx, input)
	}, "Invalid repository policy provided")

But AWS has since changed the error message to be more specific. ('Invalid repository policy provided' replaced with 'Principal not found').

Would you like to implement a fix?

None

@imre-kerr-sb1 imre-kerr-sb1 added the bug Addresses a defect in current functionality. label Aug 28, 2024
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 service/ecr Issues and PRs that pertain to the ecr service. service/iam Issues and PRs that pertain to the iam service. labels Aug 28, 2024
@terraform-aws-provider terraform-aws-provider bot added the needs-triage Waiting for first response or review from a maintainer. label Aug 28, 2024
@justinretzolk justinretzolk removed the needs-triage Waiting for first response or review from a maintainer. label Aug 28, 2024
@asw28
Copy link

asw28 commented Sep 5, 2024

I have a similar problem with Lightsail Container and ECR. ECR repo created through terraform, Lightsail through console. Connecting the container to the private repo via AWS management console produces this error:


SetRepositoryPolicyCommand[eu-central-1]

Invalid parameter at 'PolicyText' failed to satisfy constraint: 'Principal not found'

400

Copy link

github-actions bot commented Sep 9, 2024

Warning

This issue has been closed, meaning that any additional comments are hard for our team to see. Please assume that the maintainers will not see them.

Ongoing conversations amongst community members are welcome, however, the issue will be locked after 30 days. Moving conversations to another venue, such as the AWS Provider forum, is recommended. If you have additional concerns, please open a new issue, referencing this one where needed.

@github-actions github-actions bot added this to the v5.67.0 milestone Sep 9, 2024
Copy link

This functionality has been released in v5.67.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 Oct 13, 2024
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/ecr Issues and PRs that pertain to the ecr service. service/iam Issues and PRs that pertain to the iam service.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@justinretzolk @asw28 @imre-kerr-sb1 and others