Skip to content

Instantly share code, notes, and snippets.

@ktrysmt
Last active October 26, 2017 08:26
Show Gist options
  • Save ktrysmt/839ef4b56e363f944ae74b82d2992c93 to your computer and use it in GitHub Desktop.
Save ktrysmt/839ef4b56e363f944ae74b82d2992c93 to your computer and use it in GitHub Desktop.
apply ecr policies and repositories as dynamic in terraform (v0.8.7)
resource "aws_ecr_repository" "ecr_repositories" {
count = "${length(var.list_of_images)}"
name = "${var.list_of_images[ count.index ]}"
}
resource "aws_ecr_repository_policy" "ecr_policies" {
count = "${ length(var.list_of_images) * length(var.list_of_allowed_iam_users) }"
repository = "${var.list_of_images[ count.index % length(var.list_of_images) ]}"
policy = "${data.template_file.ecr_policy_allowed_iam_users.*.rendered[ count.index ]}"
}
data "template_file" "ecr_policy_allowed_iam_users" {
count = "${ length(var.list_of_images) * length(var.list_of_allowed_iam_users) }"
template = "${file("${path.module}/policy-${element(var.list_of_allowed_iam_users, count.index % length(var.list_of_allowed_iam_users))}.json")}"
vars {
arn = "arn:aws:iam::${var.aws_account}:user/${element(var.list_of_allowed_iam_users, count.index % length(var.list_of_allowed_iam_users))}"
}
}
{
"Version": "2008-10-17",
"Statement": [{
"Sid": "AllowPowerUser",
"Effect": "Allow",
"Principal": {
"AWS": "${arn}"
},
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:CompleteLayerUpload",
"ecr:DescribeImages",
"ecr:DescribeRepositories",
"ecr:GetAuthorizationToken",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:InitiateLayerUpload",
"ecr:ListImages",
"ecr:PutImage",
"ecr:UploadLayerPart"
]
}]
}
{
"Version": "2008-10-17",
"Statement": [{
"Sid": "AllowPushPull",
"Effect": "Allow",
"Principal": {
"AWS": "${arn}"
},
"Action": [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability",
"ecr:PutImage",
"ecr:DescribeImages",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload"
]
}]
}
aws_account = "1111111111111"
list_of_images = ["repo1", "repo2"]
list_of_allowed_iam_users = ["ecr-power-user", "ecr-pullpush-user"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment