Last active
October 26, 2017 08:26
-
-
Save ktrysmt/839ef4b56e363f944ae74b82d2992c93 to your computer and use it in GitHub Desktop.
apply ecr policies and repositories as dynamic in terraform (v0.8.7)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))}" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
] | |
}] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
] | |
}] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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