Skip to content

Instantly share code, notes, and snippets.

@jufemaiz
Created July 23, 2020 08:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jufemaiz/4b8810483fe59ab190782af6fa9eed36 to your computer and use it in GitHub Desktop.
Save jufemaiz/4b8810483fe59ab190782af6fa9eed36 to your computer and use it in GitHub Desktop.
Basic terraform for remote account ECR access by an ECS
resource "aws_ecr_repository" "account_a" {
name = var.ecr_name
tags = local.tags
}
resource "aws_ecr_repository_policy" "account_a" {
repository = aws_ecr_repository.this.name
policy = data.aws_iam_policy_document.account_a.json
}
data "aws_iam_policy_document" "account_a" {
statement {
sid = "RemoteAccessPolicyDocument"
actions = [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:DescribeImages",
"ecr:BatchGetImage",
]
effect = "Allow"
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${var.account_b_id}:root"
]
}
}
}
data "aws_iam_policy_document" "execution" {
statement {
sid = "AllowECRAuth"
actions = [
"ecr:GetAuthorizationToken",
]
resources = [
"*"
]
}
statement {
sid = "AllowECRAccess"
actions = [
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
]
resources = [
var.account_a_ecr_arn
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment