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

aws_ecr_lifecycle_policy not detecting change in countNumber #22641

Closed
forzagreen opened this issue Jan 18, 2022 · 6 comments · Fixed by #22665
Closed

aws_ecr_lifecycle_policy not detecting change in countNumber #22641

forzagreen opened this issue Jan 18, 2022 · 6 comments · Fixed by #22665
Labels
bug Addresses a defect in current functionality. service/ecr Issues and PRs that pertain to the ecr service.
Milestone

Comments

@forzagreen
Copy link

forzagreen commented Jan 18, 2022

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • 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
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform CLI and Terraform AWS Provider Version

Output of terraform -v:

Terraform v1.1.3
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v3.71.0

Affected Resource(s)

  • aws_ecr_lifecycle_policy

Terraform Configuration Files

Testing with the example from aws_ecr_lifecycle_policy docs:

resource "aws_ecr_repository" "testrepo" {
  name = "test-repo"
}
resource "aws_ecr_lifecycle_policy" "testrepo" {
  repository = aws_ecr_repository.testrepo.name

  policy = <<EOF
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Expire old images",
            "selection": {
                "tagStatus": "untagged",
                "countType": "sinceImagePushed",
                "countUnit": "days",
                "countNumber": 14
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
EOF
}

Expected Behavior

When we modify countNumber , the aws_ecr_lifecycle_policy resource must get modified because policy changed.

Actual Behavior

Terraform does not detect the change in countNumber (unless Description is modified too).

Steps to Reproduce

  1. Prepare a Terraform file with the resources above.
  2. terraform apply
  3. Modify "countNumber": 14 to another value, e.g. "countNumber": 7
  4. terraform apply -> No changes.

Workaround

In addition of modifying countNumber, modify Description too.

References

@github-actions github-actions bot added needs-triage Waiting for first response or review from a maintainer. service/ecr Issues and PRs that pertain to the ecr service. labels Jan 18, 2022
@josh-m-sharpe
Copy link

maybe worth bringing @YakDriver into this conversation?

@kamilturek
Copy link
Collaborator

@josh-m-sharpe @forzagreen
I think I found the bug. I prepared fix #22665.

@ewbankkit ewbankkit added bug Addresses a defect in current functionality. and removed needs-triage Waiting for first response or review from a maintainer. labels Jan 20, 2022
@github-actions github-actions bot added this to the v3.73.0 milestone Jan 20, 2022
@github-actions
Copy link

This functionality has been released in v3.73.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!

@josh-m-sharpe
Copy link

@kamilturek I don't believe this is fixed. I have tried the suggestion here: #22639 (comment)

...which should result in 4 rules, but only results in 1.

variable "expiration_prefixes" {
  default = {
    1 = "phonetrac_app",
    2 = "phonetrac_base",
    3 = "rpm_builder",
    4 = "core"
  }
}

resource "aws_ecr_lifecycle_policy" "default" {
  for_each = var.expiration_prefixes
  repository = aws_ecr_repository.default.name

  policy = <<EOF
    {
      "rules": [
        {
          "rulePriority": ${each.key},
          "description": "Keep last 2 images",
          "selection": {
            "tagStatus": "tagged",
            "tagPrefixList": ["${each.value}"],
            "countType": "imageCountMoreThan",
            "countNumber": 2
          },
          "action": {
            "type": "expire"
          }
        }
      ]
    }
  EOF
}

plan/apply:


Terraform will perform the following actions:

  # module.container_repository[0].aws_ecr_lifecycle_policy.default["1"] must be replaced
-/+ resource "aws_ecr_lifecycle_policy" "default" {
      ~ id          = "ninja-amzn2-ctm" -> (known after apply)
      ~ policy      = jsonencode(
          ~ {
              ~ rules = [
                  ~ {
                      ~ rulePriority = 3 -> 1
                      ~ selection    = {
                          ~ tagPrefixList = [
                              - "core",
                              + "phonetrac_app",
                            ]
                            # (3 unchanged elements hidden)
                        }
                        # (2 unchanged elements hidden)
                    },
                ]
            } # forces replacement
        )
      ~ registry_id = "1234" -> (known after apply)
        # (1 unchanged attribute hidden)
    }

  # module.container_repository[0].aws_ecr_lifecycle_policy.default["2"] must be replaced
-/+ resource "aws_ecr_lifecycle_policy" "default" {
      ~ id          = "ninja-amzn2-ctm" -> (known after apply)
      ~ policy      = jsonencode(
          ~ {
              ~ rules = [
                  ~ {
                      ~ rulePriority = 3 -> 2
                      ~ selection    = {
                          ~ tagPrefixList = [
                              - "core",
                              + "phonetrac_base",
                            ]
                            # (3 unchanged elements hidden)
                        }
                        # (2 unchanged elements hidden)
                    },
                ]
            } # forces replacement
        )
      ~ registry_id = "1234" -> (known after apply)
        # (1 unchanged attribute hidden)
    }

  # module.container_repository[0].aws_ecr_lifecycle_policy.default["3"] must be replaced
-/+ resource "aws_ecr_lifecycle_policy" "default" {
      ~ id          = "ninja-amzn2-ctm" -> (known after apply)
      ~ policy      = jsonencode(
          ~ {
              ~ rules = [
                  ~ {
                      ~ selection    = {
                          ~ tagPrefixList = [
                              - "core",
                              + "rpm_builder",
                            ]
                            # (3 unchanged elements hidden)
                        }
                        # (3 unchanged elements hidden)
                    },
                ]
            } # forces replacement
        )
      ~ registry_id = "1234" -> (known after apply)
        # (1 unchanged attribute hidden)
    }

  # module.container_repository[0].aws_ecr_lifecycle_policy.default["4"] will be created
  + resource "aws_ecr_lifecycle_policy" "default" {
      + id          = (known after apply)
      + policy      = jsonencode(
            {
              + rules = [
                  + {
                      + action       = {
                          + type = "expire"
                        }
                      + description  = "Keep last 2 images"
                      + rulePriority = 4
                      + selection    = {
                          + countNumber   = 2
                          + countType     = "imageCountMoreThan"
                          + tagPrefixList = [
                              + "core",
                            ]
                          + tagStatus     = "tagged"
                        }
                    },
                ]
            }
        )
      + registry_id = (known after apply)
      + repository  = "ninja-amzn2-ctm"
    }

Plan: 4 to add, 0 to change, 3 to destroy.

Only kept one of them:
Amazon_ECR

Ran apply again:

Terraform will perform the following actions:

  # module.container_repository[0].aws_ecr_lifecycle_policy.default["1"] must be replaced
-/+ resource "aws_ecr_lifecycle_policy" "default" {
      ~ id          = "ninja-amzn2-ctm" -> (known after apply)
      ~ policy      = jsonencode(
          ~ {
              ~ rules = [
                  ~ {
                      ~ rulePriority = 3 -> 1
                      ~ selection    = {
                          ~ tagPrefixList = [
                              - "rpm_builder",
                              + "phonetrac_app",
                            ]
                            # (3 unchanged elements hidden)
                        }
                        # (2 unchanged elements hidden)
                    },
                ]
            } # forces replacement
        )
      ~ registry_id = "1234" -> (known after apply)
        # (1 unchanged attribute hidden)
    }

  # module.container_repository[0].aws_ecr_lifecycle_policy.default["2"] must be replaced
-/+ resource "aws_ecr_lifecycle_policy" "default" {
      ~ id          = "ninja-amzn2-ctm" -> (known after apply)
      ~ policy      = jsonencode(
          ~ {
              ~ rules = [
                  ~ {
                      ~ rulePriority = 3 -> 2
                      ~ selection    = {
                          ~ tagPrefixList = [
                              - "rpm_builder",
                              + "phonetrac_base",
                            ]
                            # (3 unchanged elements hidden)
                        }
                        # (2 unchanged elements hidden)
                    },
                ]
            } # forces replacement
        )
      ~ registry_id = "1234" -> (known after apply)
        # (1 unchanged attribute hidden)
    }

  # module.container_repository[0].aws_ecr_lifecycle_policy.default["4"] must be replaced
-/+ resource "aws_ecr_lifecycle_policy" "default" {
      ~ id          = "ninja-amzn2-ctm" -> (known after apply)
      ~ policy      = jsonencode(
          ~ {
              ~ rules = [
                  ~ {
                      ~ rulePriority = 3 -> 4
                      ~ selection    = {
                          ~ tagPrefixList = [
                              - "rpm_builder",
                              + "core",
                            ]
                            # (3 unchanged elements hidden)
                        }
                        # (2 unchanged elements hidden)
                    },
                ]
            } # forces replacement
        )
      ~ registry_id = "1234" -> (known after apply)
        # (1 unchanged attribute hidden)
    }

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

still kept only one, but a different rule:
Amazon_ECR

@josh-m-sharpe
Copy link

I'm using latest 3.73.0:

Initializing provider plugins...
- Finding latest version of hashicorp/local...
- Finding latest version of hashicorp/time...
- Finding hashicorp/aws versions matching "~> 3.73.0"...
- Finding latest version of hashicorp/null...
- Finding latest version of hashicorp/template...
- Using previously-installed hashicorp/template v2.2.0
- Using previously-installed hashicorp/local v2.1.0
- Using previously-installed hashicorp/time v0.7.2
- Using previously-installed hashicorp/aws v3.73.0
- Using previously-installed hashicorp/null v3.1.0
- ```

@github-actions
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 May 18, 2022
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants