Skip to content

Commit cacecab

Browse files
Restrict API Lambda invocation to the CloudFront distribution (#574)
* Adding fix to limit function inovke to Cloudfront distribution * Removing comments * Removing cloudfront OAC changes since we can't implement it due to lambda taking POST requests * Formatting * Changing the url auth type to NONE
1 parent 3b0bae0 commit cacecab

3 files changed

Lines changed: 22 additions & 17 deletions

File tree

terragrunt/aws/api/lambda.tf

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,4 @@ module "api" {
2323
resource "aws_lambda_function_url" "api" {
2424
function_name = module.api.function_name
2525
authorization_type = "NONE"
26-
}
27-
28-
resource "aws_lambda_permission" "api_invoke_function_url" {
29-
statement_id = "AllowInvokeFunctionUrl"
30-
action = "lambda:InvokeFunctionUrl"
31-
function_name = module.api.function_name
32-
function_url_auth_type = "NONE"
33-
principal = "*"
34-
}
35-
36-
resource "aws_lambda_permission" "api_invoke_function" {
37-
statement_id = "AllowInvokeFunction"
38-
action = "lambda:InvokeFunction"
39-
function_name = module.api.function_name
40-
principal = "*"
41-
}
26+
}

terragrunt/aws/cloudfront/cloudfront.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,4 @@ resource "aws_cloudfront_response_headers_policy" "security_headers_api" {
120120
protection = true
121121
}
122122
}
123-
}
123+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Restrict invocation of the API Lambda function to this CloudFront distribution.
2+
# These permissions live in the CloudFront module (rather than the API module) to
3+
# avoid a circular Terragrunt dependency: the CloudFront distribution depends on
4+
# the API function URL/name, and these permissions depend on the distribution ARN.
5+
resource "aws_lambda_permission" "api_invoke_function_url" {
6+
statement_id = "AllowInvokeFunctionUrl"
7+
action = "lambda:InvokeFunctionUrl"
8+
function_name = var.api_function_name
9+
function_url_auth_type = "NONE"
10+
principal = "cloudfront.amazonaws.com"
11+
source_arn = aws_cloudfront_distribution.api.arn
12+
}
13+
14+
resource "aws_lambda_permission" "api_invoke_function" {
15+
statement_id = "AllowInvokeFunction"
16+
action = "lambda:InvokeFunction"
17+
function_name = var.api_function_name
18+
principal = "cloudfront.amazonaws.com"
19+
source_arn = aws_cloudfront_distribution.api.arn
20+
}

0 commit comments

Comments
 (0)