Skip to content

Commit 095cb29

Browse files
authored
feat: Allow setting custom trust policy in iam-assumable-role (#176)
1 parent b82cc52 commit 095cb29

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

examples/iam-assumable-role/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,25 @@ Run `terraform destroy` when you don't need these resources.
2626

2727
## Providers
2828

29-
No providers.
29+
| Name | Version |
30+
|------|---------|
31+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.23 |
3032

3133
## Modules
3234

3335
| Name | Source | Version |
3436
|------|--------|---------|
3537
| <a name="module_iam_assumable_role_admin"></a> [iam\_assumable\_role\_admin](#module\_iam\_assumable\_role\_admin) | ../../modules/iam-assumable-role | n/a |
3638
| <a name="module_iam_assumable_role_custom"></a> [iam\_assumable\_role\_custom](#module\_iam\_assumable\_role\_custom) | ../../modules/iam-assumable-role | n/a |
39+
| <a name="module_iam_assumable_role_custom_trust_policy"></a> [iam\_assumable\_role\_custom\_trust\_policy](#module\_iam\_assumable\_role\_custom\_trust\_policy) | ../../modules/iam-assumable-role | n/a |
3740
| <a name="module_iam_assumable_role_sts"></a> [iam\_assumable\_role\_sts](#module\_iam\_assumable\_role\_sts) | ../../modules/iam-assumable-role | n/a |
3841
| <a name="module_iam_policy"></a> [iam\_policy](#module\_iam\_policy) | ../../modules/iam-policy | n/a |
3942

4043
## Resources
4144

42-
No resources.
45+
| Name | Type |
46+
|------|------|
47+
| [aws_iam_policy_document.custom_trust_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
4348

4449
## Inputs
4550

examples/iam-assumable-role/main.tf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,54 @@ module "iam_assumable_role_sts" {
9191
# number_of_custom_role_policy_arns = 3
9292
}
9393

94+
#########################################
95+
# IAM assumable role with custom trust policy
96+
#########################################
97+
module "iam_assumable_role_custom_trust_policy" {
98+
source = "../../modules/iam-assumable-role"
99+
100+
create_role = true
101+
102+
role_name = "iam_assumable_role_custom_trust_policy"
103+
104+
custom_role_trust_policy = data.aws_iam_policy_document.custom_trust_policy.json
105+
custom_role_policy_arns = ["arn:aws:iam::aws:policy/AmazonCognitoReadOnly"]
106+
}
107+
108+
data "aws_iam_policy_document" "custom_trust_policy" {
109+
statement {
110+
effect = "Allow"
111+
actions = ["sts:AssumeRole"]
112+
113+
condition {
114+
test = "StringEquals"
115+
variable = "sts:ExternalId"
116+
values = ["some-ext-id"]
117+
}
118+
119+
condition {
120+
test = "StringEquals"
121+
variable = "aws:PrincipalOrgID"
122+
values = ["o-someorgid"]
123+
}
124+
125+
principals {
126+
type = "AWS"
127+
identifiers = ["*"]
128+
}
129+
}
130+
131+
statement {
132+
effect = "Deny"
133+
actions = ["sts:AssumeRole"]
134+
135+
principals {
136+
type = "AWS"
137+
identifiers = ["arn:aws:iam::111111111111:root"]
138+
}
139+
}
140+
}
141+
94142
#########################################
95143
# IAM policy
96144
#########################################

modules/iam-assumable-role/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ No modules.
4646
| <a name="input_create_instance_profile"></a> [create\_instance\_profile](#input\_create\_instance\_profile) | Whether to create an instance profile | `bool` | `false` | no |
4747
| <a name="input_create_role"></a> [create\_role](#input\_create\_role) | Whether to create a role | `bool` | `false` | no |
4848
| <a name="input_custom_role_policy_arns"></a> [custom\_role\_policy\_arns](#input\_custom\_role\_policy\_arns) | List of ARNs of IAM policies to attach to IAM role | `list(string)` | `[]` | no |
49+
| <a name="input_custom_role_trust_policy"></a> [custom\_role\_trust\_policy](#input\_custom\_role\_trust\_policy) | A custorm role trust policy | `string` | `""` | no |
4950
| <a name="input_force_detach_policies"></a> [force\_detach\_policies](#input\_force\_detach\_policies) | Whether policies should be detached from this role when destroying | `bool` | `false` | no |
5051
| <a name="input_max_session_duration"></a> [max\_session\_duration](#input\_max\_session\_duration) | Maximum CLI/API session duration in seconds between 3600 and 43200 | `number` | `3600` | no |
5152
| <a name="input_mfa_age"></a> [mfa\_age](#input\_mfa\_age) | Max age of valid MFA (in seconds) for roles which require MFA | `number` | `86400` | no |

modules/iam-assumable-role/main.tf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ locals {
33
}
44

55
data "aws_iam_policy_document" "assume_role" {
6+
count = var.custom_role_trust_policy == "" && var.role_requires_mfa ? 0 : 1
7+
68
statement {
79
effect = "Allow"
810

@@ -30,6 +32,8 @@ data "aws_iam_policy_document" "assume_role" {
3032
}
3133

3234
data "aws_iam_policy_document" "assume_role_with_mfa" {
35+
count = var.custom_role_trust_policy == "" && var.role_requires_mfa ? 1 : 0
36+
3337
statement {
3438
effect = "Allow"
3539

@@ -79,7 +83,12 @@ resource "aws_iam_role" "this" {
7983
force_detach_policies = var.force_detach_policies
8084
permissions_boundary = var.role_permissions_boundary_arn
8185

82-
assume_role_policy = var.role_requires_mfa ? data.aws_iam_policy_document.assume_role_with_mfa.json : data.aws_iam_policy_document.assume_role.json
86+
assume_role_policy = coalesce(
87+
var.custom_role_trust_policy,
88+
try(data.aws_iam_policy_document.assume_role_with_mfa[0].json,
89+
data.aws_iam_policy_document.assume_role[0].json
90+
)
91+
)
8392

8493
tags = var.tags
8594
}

modules/iam-assumable-role/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ variable "custom_role_policy_arns" {
7676
default = []
7777
}
7878

79+
variable "custom_role_trust_policy" {
80+
description = "A custorm role trust policy"
81+
type = string
82+
default = ""
83+
}
84+
7985
variable "number_of_custom_role_policy_arns" {
8086
description = "Number of IAM policies to attach to IAM role"
8187
type = number

0 commit comments

Comments
 (0)