Skip to content

Commit 90ec6a1

Browse files
feat: add support for additional assume_role_policy statements (#203)
1 parent 53b55fd commit 90ec6a1

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,14 @@ module "vpc" {
270270

271271
## Additional IAM policies for Lambda Functions
272272

273-
There are 5 supported ways to attach IAM policies to IAM role used by Lambda Function:
273+
There are 6 supported ways to attach IAM policies to IAM role used by Lambda Function:
274274

275275
1. `policy_json` - JSON string or heredoc, when `attach_policy_json = true`.
276276
1. `policy_jsons` - List of JSON strings or heredoc, when `attach_policy_jsons = true` and `number_of_policy_jsons > 0`.
277277
1. `policy` - ARN of existing IAM policy, when `attach_policy = true`.
278278
1. `policies` - List of ARNs of existing IAM policies, when `attach_policies = true` and `number_of_policies > 0`.
279279
1. `policy_statements` - Map of maps to define IAM statements which will be generated as IAM policy. Requires `attach_policy_statements = true`. See `examples/complete` for more information.
280+
1. `assume_role_policy_statements` - Map of maps to define IAM statements which will be generated as IAM policy for assuming Lambda Function role (trust relationship). See `examples/complete` for more information.
280281

281282
## Lambda Permissions for allowed triggers
282283

@@ -659,6 +660,7 @@ No modules.
659660
|------|-------------|------|---------|:--------:|
660661
| <a name="input_allowed_triggers"></a> [allowed\_triggers](#input\_allowed\_triggers) | Map of allowed triggers to create Lambda permissions | `map(any)` | `{}` | no |
661662
| <a name="input_artifacts_dir"></a> [artifacts\_dir](#input\_artifacts\_dir) | Directory name where artifacts should be stored | `string` | `"builds"` | no |
663+
| <a name="input_assume_role_policy_statements"></a> [assume\_role\_policy\_statements](#input\_assume\_role\_policy\_statements) | Map of dynamic policy statements for assuming Lambda Function role (trust relationship) | `any` | `{}` | no |
662664
| <a name="input_attach_async_event_policy"></a> [attach\_async\_event\_policy](#input\_attach\_async\_event\_policy) | Controls whether async event policy should be added to IAM role for Lambda Function | `bool` | `false` | no |
663665
| <a name="input_attach_cloudwatch_logs_policy"></a> [attach\_cloudwatch\_logs\_policy](#input\_attach\_cloudwatch\_logs\_policy) | Controls whether CloudWatch Logs policy should be added to IAM role for Lambda Function | `bool` | `true` | no |
664666
| <a name="input_attach_dead_letter_policy"></a> [attach\_dead\_letter\_policy](#input\_attach\_dead\_letter\_policy) | Controls whether SNS/SQS dead letter notification policy should be added to IAM role for Lambda Function | `bool` | `false` | no |

examples/complete/main.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ module "lambda_function" {
6363
# Additional policies
6464
######################
6565

66+
assume_role_policy_statements = {
67+
account_root = {
68+
effect = "Allow",
69+
actions = ["sts:AssumeRole"],
70+
principals = {
71+
account_principal = {
72+
type = "AWS",
73+
identifiers = ["arn:aws:iam::135367859851:root"]
74+
}
75+
}
76+
condition = {
77+
stringequals_condition = {
78+
test = "StringEquals"
79+
variable = "sts:ExternalId"
80+
values = ["12345"]
81+
}
82+
}
83+
}
84+
}
85+
6686
attach_policy_json = true
6787
policy_json = <<EOF
6888
{

iam.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,42 @@ data "aws_iam_policy_document" "assume_role" {
5252
}
5353
}
5454
}
55+
56+
dynamic "statement" {
57+
for_each = var.assume_role_policy_statements
58+
59+
content {
60+
sid = lookup(statement.value, "sid", replace(statement.key, "/[^0-9A-Za-z]*/", ""))
61+
effect = lookup(statement.value, "effect", null)
62+
actions = lookup(statement.value, "actions", null)
63+
not_actions = lookup(statement.value, "not_actions", null)
64+
65+
dynamic "principals" {
66+
for_each = lookup(statement.value, "principals", [])
67+
content {
68+
type = principals.value.type
69+
identifiers = principals.value.identifiers
70+
}
71+
}
72+
73+
dynamic "not_principals" {
74+
for_each = lookup(statement.value, "not_principals", [])
75+
content {
76+
type = not_principals.value.type
77+
identifiers = not_principals.value.identifiers
78+
}
79+
}
80+
81+
dynamic "condition" {
82+
for_each = lookup(statement.value, "condition", [])
83+
content {
84+
test = condition.value.test
85+
variable = condition.value.variable
86+
values = condition.value.values
87+
}
88+
}
89+
}
90+
}
5591
}
5692

5793
resource "aws_iam_role" "lambda" {

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ variable "trusted_entities" {
441441
default = []
442442
}
443443

444+
variable "assume_role_policy_statements" {
445+
description = "Map of dynamic policy statements for assuming Lambda Function role (trust relationship)"
446+
type = any
447+
default = {}
448+
}
449+
444450
variable "policy_json" {
445451
description = "An additional policy document as JSON to attach to the Lambda Function role"
446452
type = string

0 commit comments

Comments
 (0)