Invalid parameter at 'PolicyText' failed to satisfy constraint: 'Invalid registry policy provided?

0

Please help i get this error while trying to create a ECRRegistryPolicy Via Cloudformation

Error Resource handler returned message: "Invalid parameter at 'PolicyText' failed to satisfy constraint: 'Invalid registry policy provided' (Service: Ecr, Status Code: 400, Request ID: 068ec4b0-e54e-4f1d-be3d-fca305f898f0)" (RequestToken: df80f2df-2e36-e96b-bcbb-0084f0172e70, HandlerErrorCode: InvalidRequest)

  ECRRepositoryPolicy:
    Type: AWS::ECR::RegistryPolicy
    DeletionPolicy: Delete
    Properties:
      PolicyText:
        Version: "2012-10-17"
        Statement:
        # Allow Tooling Account to push and manage the repository
        - Sid: AllowToolingAccountPush
          Effect: Allow
          Principal:
            AWS:
            - !Sub "arn:aws:iam::${AccountA}:root"
            - !Sub "arn:aws:iam::${AccountA}:role/DeploymentRole"
          Action:
          - "ecr:GetAuthorizationToken"
          - "ecr:BatchCheckLayerAvailability"
          - "ecr:CompleteLayerUpload"
          - "ecr:InitiateLayerUpload"
          - "ecr:PutImage"
          - "ecr:UploadLayerPart"
          - "ecr:BatchGetImage"
          - "ecr:GetDownloadUrlForLayer"
          Resource: !GetAtt ECRRepository.Arn
2 Answers
2
Accepted Answer

The reason for the error is that the actions you've specified aren't permitted in private registry policies. The actions allowed are explained in this documentation article: https://docs.aws.amazon.com/AmazonECR/latest/userguide/registry-permissions.html and don't include any of the permissions you've specified.

I think you might be meaning to specify a repository policy rather than a registry policy. You can do that with the RepositoryPolicyText property of AWS::ECR::Repository: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecr-repository.html#cfn-ecr-repository-repositorypolicytext

EXPERT
Leo K
answered 4 days ago
profile picture
EXPERT
reviewed 3 days ago
profile picture
EXPERT
A_J
reviewed 3 days ago
1

The error message you're encountering indicates that there is an issue with the format of your ECR registry policy in the CloudFormation template. Here’s how you can resolve the issue:

Steps to Troubleshoot Validate JSON Structure: Ensure the policy JSON structure is correct. PolicyText must be a properly formatted JSON string.

Correct Indentation and Formatting: Ensure proper indentation and formatting of the CloudFormation YAML. YAML is sensitive to indentation, and incorrect indentation can lead to parsing issues.

Stringify PolicyText: The PolicyText property should be treated as a JSON string. In YAML, you can use the Fn::Sub function to handle complex substitutions and stringification.

Corrected CloudFormation Template Here is the corrected version of your CloudFormation template:

  ECRRepositoryPolicy:
    Type: AWS::ECR::RepositoryPolicy
    DeletionPolicy: Delete
    Properties:
      RepositoryName: !Ref ECRRepository
      PolicyText: !Sub |
        {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Sid": "AllowToolingAccountPush",
              "Effect": "Allow",
              "Principal": {
                "AWS": [
                  "arn:aws:iam::${AccountA}:root",
                  "arn:aws:iam::${AccountA}:role/DeploymentRole"
                ]
              },
              "Action": [
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:CompleteLayerUpload",
                "ecr:InitiateLayerUpload",
                "ecr:PutImage",
                "ecr:UploadLayerPart",
                "ecr:BatchGetImage",
                "ecr:GetDownloadUrlForLayer"
              ],
              "Resource": "${ECRRepository.Arn}"
            }
          ]
        }

Key Changes Made: Type Correction: Ensure the Type of the resource is AWS::ECR::RepositoryPolicy instead of AWS::ECR::RegistryPolicy.

YAML Stringification: Used !Sub with | to properly format the PolicyText as a JSON string.

RepositoryName Property: Added the RepositoryName property to link the policy to the specific ECR repository.

Substitution for Resource ARN: Ensured that ${ECRRepository.Arn} is correctly substituted in the policy.

Explanation: RepositoryName: Ensures the policy is applied to the correct ECR repository. !Sub |: Allows multiline strings and ensures correct JSON formatting and substitution within YAML. PolicyText: Properly formatted JSON string with embedded substitutions for AccountA and ECRRepository.Arn. Apply the Changes: Deploy this corrected template via the AWS CloudFormation console or CLI, and it should resolve the "Invalid registry policy provided" error.

profile picture
EXPERT
A_J
answered 4 days ago