I normally use resource based policies for services like ECR or S3 when I need to allow access for all the accounts within the organization.
This is achieved by using the "AWS:PrincipalOrgID" IAM condition, here is a read-only access example for it.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadonlyAccess",
"Effect": "Allow",
"Principal": "*",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:DescribeImageScanFindings",
"ecr:DescribeImages",
"ecr:DescribeRepositories",
"ecr:GetAuthorizationToken",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:ListImages"
],
"Condition": {
"StringLike": {
"aws:PrincipalOrgID": "<YOUR-ORGANIZATION-ID>"
}
}
}
]
}
And here is the full Terraform equivalent of how to use achieve this
resource "aws_ecr_repository" "example" {
name = "example"
}
data "aws_iam_policy_document" "ecr_readonly_access" {
statement {
sid = "ReadonlyAccess"
effect = "Allow"
principals {
type = "*"
identifiers = ["*"]
}
condition {
test = "StringLike"
variable = "aws:PrincipalOrgID"
# This is our organization-wide identifier which can be found after
# log-in to AWS: <https://console.aws.amazon.com/organizations/home>
values = [aws_organizations_organization.this.id]
}
actions = [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:DescribeImages",
"ecr:BatchGetImage",
"ecr:DescribeImageScanFindings",
]
}
}
resource "aws_ecr_repository_policy" "ecr" {
repository = aws_ecr_repository.example.name
policy = data.aws_iam_policy_document.ecr_readonly_access.json
}