diff --git a/ecr-policies.tf b/ecr-policies.tf index 14b3b51..a9402dd 100644 --- a/ecr-policies.tf +++ b/ecr-policies.tf @@ -1,66 +1,73 @@ + resource "aws_ecr_repository_policy" "default" { repository = aws_ecr_repository.default.name + policy = data.aws_iam_policy_document.default.json +} + +data "aws_iam_policy_document" "default" { + dynamic "statement" { + for_each = length(try(var.trust_accounts, [])) > 0 ? [1] : [] + + content { + sid = "AllowPull" + effect = "Allow" + + principals { + type = "AWS" + identifiers = formatlist("arn:aws:iam::%s:root", var.trust_accounts) + } - policy = < 0 ? [1] : [] + + content { + sid = "LambdaECRImageCrossAccountRetrievalPolicy" + effect = "Allow" + + principals { + type = "Service" + identifiers = ["lambda.amazonaws.com"] + } + + actions = [ "ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage" - ], - "Condition": { - "StringLike": { - "aws:sourceArn": [ - ${join(",", formatlist("\"arn:aws:lambda:%s:%s:function:*\"", data.aws_region.current.name, var.trust_accounts))} - ] - } + ] + + condition { + test = "StringLike" + variable = "aws:sourceArn" + values = formatlist("arn:aws:lambda:%s:%s:function:*", data.aws_region.current.name, var.trust_accounts) } } - ] -} -EOF - - depends_on = [aws_ecr_repository.default] + } }