Skip to content

Commit 004f13d

Browse files
authored
feat: Extended trusted_entities variable to support multiple types (#143)
1 parent 0a1a1a3 commit 004f13d

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ No modules.
727727
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to assign to resources. | `map(string)` | `{}` | no |
728728
| <a name="input_timeout"></a> [timeout](#input\_timeout) | The amount of time your Lambda Function has to run in seconds. | `number` | `3` | no |
729729
| <a name="input_tracing_mode"></a> [tracing\_mode](#input\_tracing\_mode) | Tracing mode of the Lambda Function. Valid value can be either PassThrough or Active. | `string` | `null` | no |
730-
| <a name="input_trusted_entities"></a> [trusted\_entities](#input\_trusted\_entities) | Lambda Function additional trusted entities for assuming roles (trust relationship) | `list(string)` | `[]` | no |
730+
| <a name="input_trusted_entities"></a> [trusted\_entities](#input\_trusted\_entities) | List of additional trusted entities for assuming Lambda Function role (trust relationship) | `any` | `[]` | no |
731731
| <a name="input_use_existing_cloudwatch_log_group"></a> [use\_existing\_cloudwatch\_log\_group](#input\_use\_existing\_cloudwatch\_log\_group) | Whether to use an existing CloudWatch log group or create new | `bool` | `false` | no |
732732
| <a name="input_vpc_security_group_ids"></a> [vpc\_security\_group\_ids](#input\_vpc\_security\_group\_ids) | List of security group ids when Lambda Function should run in the VPC. | `list(string)` | `null` | no |
733733
| <a name="input_vpc_subnet_ids"></a> [vpc\_subnet\_ids](#input\_vpc\_subnet\_ids) | List of subnet ids when Lambda Function should run in the VPC. Usually private or intra subnets. | `list(string)` | `null` | no |

examples/complete/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Note that this example may create resources which cost money. Run `terraform des
4141
| <a name="module_lambda_function_existing_package_local"></a> [lambda\_function\_existing\_package\_local](#module\_lambda\_function\_existing\_package\_local) | ../../ | |
4242
| <a name="module_lambda_layer_local"></a> [lambda\_layer\_local](#module\_lambda\_layer\_local) | ../../ | |
4343
| <a name="module_lambda_layer_s3"></a> [lambda\_layer\_s3](#module\_lambda\_layer\_s3) | ../../ | |
44+
| <a name="module_lambda_with_mixed_trusted_entities"></a> [lambda\_with\_mixed\_trusted\_entities](#module\_lambda\_with\_mixed\_trusted\_entities) | ../../ | |
4445
| <a name="module_lambda_with_provisioned_concurrency"></a> [lambda\_with\_provisioned\_concurrency](#module\_lambda\_with\_provisioned\_concurrency) | ../../ | |
4546
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | |
4647

examples/complete/main.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,37 @@ module "lambda_with_provisioned_concurrency" {
227227
provisioned_concurrent_executions = -1 # 2
228228
}
229229

230+
###############################################
231+
# Lambda Function with mixed trusted entities
232+
###############################################
233+
234+
module "lambda_with_mixed_trusted_entities" {
235+
source = "../../"
236+
237+
function_name = "${random_pet.this.id}-lambda-mixed-trusted-entities"
238+
handler = "index.lambda_handler"
239+
runtime = "python3.8"
240+
241+
source_path = "${path.module}/../fixtures/python3.8-app1"
242+
243+
trusted_entities = [
244+
"appsync.amazonaws.com",
245+
{
246+
type = "AWS",
247+
identifiers = [
248+
"arn:aws:iam::307990089504:root",
249+
]
250+
},
251+
{
252+
type = "Service",
253+
identifiers = [
254+
"codedeploy.amazonaws.com",
255+
"ecs.amazonaws.com"
256+
]
257+
}
258+
]
259+
}
260+
230261
###########
231262
# Disabled
232263
###########

iam.tf

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ locals {
1212
# for #83 that will allow one to import resources without receiving an error from coalesce.
1313
# @see https://github.com/terraform-aws-modules/terraform-aws-lambda/issues/83
1414
role_name = local.create_role ? coalesce(var.role_name, var.function_name, "*") : null
15+
16+
# IAM Role trusted entities is a list of any (allow strings (services) and maps (type+identifiers))
17+
trusted_entities_services = distinct(compact(concat(
18+
slice(["lambda.amazonaws.com", "edgelambda.amazonaws.com"], 0, var.lambda_at_edge ? 2 : 1),
19+
[for service in var.trusted_entities : try(tostring(service), "")]
20+
)))
21+
22+
trusted_entities_principals = [
23+
for principal in var.trusted_entities : {
24+
type = principal.type
25+
identifiers = tolist(principal.identifiers)
26+
}
27+
if !can(tostring(principal))
28+
]
1529
}
1630

1731
###########
@@ -27,7 +41,15 @@ data "aws_iam_policy_document" "assume_role" {
2741

2842
principals {
2943
type = "Service"
30-
identifiers = distinct(concat(slice(["lambda.amazonaws.com", "edgelambda.amazonaws.com"], 0, var.lambda_at_edge ? 2 : 1), var.trusted_entities))
44+
identifiers = local.trusted_entities_services
45+
}
46+
47+
dynamic "principals" {
48+
for_each = local.trusted_entities_principals
49+
content {
50+
type = principals.value.type
51+
identifiers = principals.value.identifiers
52+
}
3153
}
3254
}
3355
}

variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ variable "attach_policy_statements" {
436436
}
437437

438438
variable "trusted_entities" {
439-
description = "Lambda Function additional trusted entities for assuming roles (trust relationship)"
440-
type = list(string)
439+
description = "List of additional trusted entities for assuming Lambda Function role (trust relationship)"
440+
type = any
441441
default = []
442442
}
443443

0 commit comments

Comments
 (0)