Skip to content

Commit e79573d

Browse files
feat: Added support for lambda permissions when the target is a lambda function (#240)
1 parent 596cc0f commit e79573d

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.67.0
3+
rev: v1.71.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ module "alb" {
3333
backend_protocol = "HTTP"
3434
backend_port = 80
3535
target_type = "instance"
36-
targets = [
37-
{
36+
targets = {
37+
my_target = {
3838
target_id = "i-0123456789abcdefg"
3939
port = 80
40-
},
41-
{
40+
}
41+
my_other_target = {
4242
target_id = "i-a1b2c3d4e5f6g7h8i"
4343
port = 8080
4444
}
45-
]
45+
}
4646
}
4747
]
4848
@@ -311,6 +311,7 @@ No modules.
311311

312312
| Name | Type |
313313
|------|------|
314+
| [aws_lambda_permission.lb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
314315
| [aws_lb.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb) | resource |
315316
| [aws_lb_listener.frontend_http_tcp](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener) | resource |
316317
| [aws_lb_listener.frontend_https](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener) | resource |

examples/complete-alb/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Note that this example may create resources which cost money. Run `terraform des
3838
|------|--------|---------|
3939
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | ~> 3.0 |
4040
| <a name="module_alb"></a> [alb](#module\_alb) | ../../ | n/a |
41-
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 3.0 |
41+
| <a name="module_lambda_with_allowed_triggers"></a> [lambda\_with\_allowed\_triggers](#module\_lambda\_with\_allowed\_triggers) | terraform-aws-modules/lambda/aws | ~> 3.0 |
42+
| <a name="module_lambda_without_allowed_triggers"></a> [lambda\_without\_allowed\_triggers](#module\_lambda\_without\_allowed\_triggers) | terraform-aws-modules/lambda/aws | ~> 3.0 |
4243
| <a name="module_lb_disabled"></a> [lb\_disabled](#module\_lb\_disabled) | ../../ | n/a |
4344
| <a name="module_security_group"></a> [security\_group](#module\_security\_group) | terraform-aws-modules/security-group/aws | ~> 4.0 |
4445

examples/complete-alb/main.tf

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,18 @@ module "alb" {
405405
target_type = "lambda"
406406
lambda_multi_value_headers_enabled = true
407407
targets = {
408-
# Lambda function permission should be granted before
409-
# it is used. There can be an error:
410-
# NB: Error registering targets with target group:
411-
# AccessDenied: elasticloadbalancing principal does not
412-
# have permission to invoke ... from target group ...
413-
my_lambda = {
414-
target_id = module.lambda_function.lambda_function_arn
408+
lambda_with_allowed_triggers = {
409+
target_id = module.lambda_with_allowed_triggers.lambda_function_arn
410+
}
411+
}
412+
},
413+
{
414+
name_prefix = "l2-"
415+
target_type = "lambda"
416+
targets = {
417+
lambda_without_allowed_triggers = {
418+
target_id = module.lambda_without_allowed_triggers.lambda_function_arn
419+
attach_lambda_permission = true
415420
}
416421
}
417422
},
@@ -500,12 +505,12 @@ resource "null_resource" "download_package" {
500505
}
501506
}
502507

503-
module "lambda_function" {
508+
module "lambda_with_allowed_triggers" {
504509
source = "terraform-aws-modules/lambda/aws"
505510
version = "~> 3.0"
506511

507-
function_name = "${random_pet.this.id}-lambda"
508-
description = "My awesome lambda function"
512+
function_name = "${random_pet.this.id}-with-allowed-triggers"
513+
description = "My awesome lambda function (with allowed triggers)"
509514
handler = "index.lambda_handler"
510515
runtime = "python3.8"
511516

@@ -523,3 +528,23 @@ module "lambda_function" {
523528

524529
depends_on = [null_resource.download_package]
525530
}
531+
532+
module "lambda_without_allowed_triggers" {
533+
source = "terraform-aws-modules/lambda/aws"
534+
version = "~> 3.0"
535+
536+
function_name = "${random_pet.this.id}-without-allowed-triggers"
537+
description = "My awesome lambda function (without allowed triggers)"
538+
handler = "index.lambda_handler"
539+
runtime = "python3.8"
540+
541+
publish = true
542+
543+
create_package = false
544+
local_existing_package = local.downloaded
545+
546+
# Allowed triggers will be managed by ALB module
547+
allowed_triggers = {}
548+
549+
depends_on = [null_resource.download_package]
550+
}

main.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,28 @@ locals {
133133
if k == "targets"
134134
]
135135
])...)
136+
137+
# Filter out the attachments for lambda functions. The ALB target group needs permission to forward a request on to
138+
# the specified lambda function. This filtered list is used to create those permission resources
139+
target_group_attachments_lambda = {
140+
for k, v in local.target_group_attachments :
141+
(k) => merge(v, { lambda_function_name = split(":", v.target_id)[6] })
142+
if try(v.attach_lambda_permission, false)
143+
}
144+
}
145+
146+
resource "aws_lambda_permission" "lb" {
147+
for_each = var.create_lb && local.target_group_attachments_lambda != null ? local.target_group_attachments_lambda : {}
148+
149+
function_name = each.value.lambda_function_name
150+
qualifier = try(each.value.lambda_qualifier, null)
151+
152+
statement_id = try(each.value.lambda_statement_id, "AllowExecutionFromLb")
153+
action = try(each.value.lambda_action, "lambda:InvokeFunction")
154+
principal = try(each.value.lambda_principal, "elasticloadbalancing.amazonaws.com")
155+
source_arn = aws_lb_target_group.main[each.value.tg_index].arn
156+
source_account = try(each.value.lambda_source_account, null)
157+
event_source_token = try(each.value.lambda_event_source_token, null)
136158
}
137159

138160
resource "aws_lb_target_group_attachment" "this" {
@@ -142,6 +164,8 @@ resource "aws_lb_target_group_attachment" "this" {
142164
target_id = each.value.target_id
143165
port = lookup(each.value, "port", null)
144166
availability_zone = lookup(each.value, "availability_zone", null)
167+
168+
depends_on = [aws_lambda_permission.lb]
145169
}
146170

147171
resource "aws_lb_listener_rule" "https_listener_rule" {

0 commit comments

Comments
 (0)