Skip to content

Commit 8131e65

Browse files
authored
Merge pull request #23 from dwhswenson/fix-lambda-permissions
Add permissions for lambda downloads to containers
2 parents 627d5c6 + 4eb4dab commit 8131e65

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

modules/lambda-image-build/main.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,48 @@ resource "aws_ecr_lifecycle_policy" "cleanup" {
5353
})
5454
}
5555

56+
resource "aws_ecr_repository_policy" "self_access" {
57+
repository = aws_ecr_repository.lambda_image.name
58+
59+
policy = jsonencode({
60+
Version = "2012-10-17"
61+
Statement = [
62+
{
63+
Sid = "AllowAccountPushPull"
64+
Effect = "Allow"
65+
Action = [
66+
"ecr:GetDownloadUrlForLayer",
67+
"ecr:BatchGetImage",
68+
"ecr:BatchCheckLayerAvailability",
69+
"ecr:PutImage",
70+
"ecr:InitiateLayerUpload",
71+
"ecr:UploadLayerPart",
72+
"ecr:CompleteLayerUpload",
73+
]
74+
Principal = {
75+
AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
76+
}
77+
},
78+
{
79+
Sid = "AllowLambdaImagePull"
80+
Effect = "Allow"
81+
Action = [
82+
"ecr:BatchGetImage",
83+
"ecr:GetDownloadUrlForLayer",
84+
]
85+
Principal = {
86+
Service = "lambda.amazonaws.com"
87+
}
88+
Condition = {
89+
StringLike = {
90+
"aws:sourceArn" = "arn:aws:lambda:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:function:*"
91+
}
92+
}
93+
}
94+
]
95+
})
96+
}
97+
5698
resource "null_resource" "build_and_push" {
5799
triggers = {
58100
image_tag = var.image_tag
@@ -75,6 +117,7 @@ resource "null_resource" "build_and_push" {
75117

76118
depends_on = [
77119
aws_ecr_lifecycle_policy.cleanup,
120+
aws_ecr_repository_policy.self_access,
78121
]
79122
}
80123

modules/lambda-image-republish/main.tf

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,34 @@ resource "aws_ecr_repository_policy" "self_access" {
4747
{
4848
Sid = "AllowAccountPushPull"
4949
Effect = "Allow"
50-
Action = ["ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage", "ecr:BatchCheckLayerAvailability", "ecr:PutImage", "ecr:InitiateLayerUpload", "ecr:UploadLayerPart", "ecr:CompleteLayerUpload"]
50+
Action = [
51+
"ecr:GetDownloadUrlForLayer",
52+
"ecr:BatchGetImage",
53+
"ecr:BatchCheckLayerAvailability",
54+
"ecr:PutImage",
55+
"ecr:InitiateLayerUpload",
56+
"ecr:UploadLayerPart",
57+
"ecr:CompleteLayerUpload",
58+
]
5159
Principal = {
5260
AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
5361
}
62+
},
63+
{
64+
Sid = "AllowLambdaImagePull"
65+
Effect = "Allow"
66+
Action = [
67+
"ecr:BatchGetImage",
68+
"ecr:GetDownloadUrlForLayer",
69+
]
70+
Principal = {
71+
Service = "lambda.amazonaws.com"
72+
}
73+
Condition = {
74+
StringLike = {
75+
"aws:sourceArn" = "arn:aws:lambda:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:function:*"
76+
}
77+
}
5478
}
5579
]
5680
})

0 commit comments

Comments
 (0)