Creates an AWS ECR repository and attaches a policy document to it, allowing specified actions for a given AWS principal.

Terraform Template

data "aws_iam_policy_document" "example" {

  statement {
    actions = ["ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage", "ecr:BatchCheckLayerAvailability", "ecr:PutImage", "ecr:InitiateLayerUpload", "ecr:UploadLayerPart", "ecr:CompleteLayerUpload", "ecr:DescribeRepositories", "ecr:GetRepositoryPolicy", "ecr:ListImages", "ecr:DeleteRepository", "ecr:BatchDeleteImage", "ecr:SetRepositoryPolicy", "ecr:DeleteRepositoryPolicy"]
    effect = "Allow"

    principals {
      identifiers = ["123456789012"]
      type = "AWS"
    }
    sid = "new policy"
  }
}

resource "aws_ecr_repository" "example" {
  name = "example-repo"
}

resource "aws_ecr_repository_policy" "example" {
  policy = data.aws_iam_policy_document.example.json
  repository = aws_ecr_repository.example.name
}