Skip to content

Commit 2d9d213

Browse files
committed
chore: Update naming use and remove role self-assume
1 parent e3eefb9 commit 2d9d213

File tree

28 files changed

+254
-242
lines changed

28 files changed

+254
-242
lines changed

examples/iam-role/main.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ module "iam_role_instance_profile" {
2323

2424
name = "${local.name}-instance-profile"
2525

26-
# https://aws.amazon.com/blogs/security/announcing-an-update-to-iam-role-trust-policy-behavior/
27-
allow_self_assume_role = true
2826
create_instance_profile = true
2927

3028
assume_role_policy_statements = [

modules/iam-account/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ module "iam_account" {
2424
## Notes
2525

2626
If IAM account alias was previously set (either via AWS console or during the creation of an account from AWS Organizations) you will see this error:
27-
```
27+
28+
```sh
2829
aws_iam_account_alias.this: Error creating account alias with name my-account-alias
2930
```
3031

3132
If you want to manage IAM alias using Terraform (otherwise why are you reading this?) you need to import this resource like this:
32-
```
33+
34+
```sh
3335
$ terraform import module.iam_account.aws_iam_account_alias.this this
3436

3537
module.iam_account.aws_iam_account_alias.this: Importing from ID "this"...
@@ -64,18 +66,17 @@ No modules.
6466
|------|------|
6567
| [aws_iam_account_alias.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_account_alias) | resource |
6668
| [aws_iam_account_password_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_account_password_policy) | resource |
67-
| [aws_caller_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
6869

6970
## Inputs
7071

7172
| Name | Description | Type | Default | Required |
7273
|------|-------------|------|---------|:--------:|
7374
| <a name="input_account_alias"></a> [account\_alias](#input\_account\_alias) | AWS IAM account alias for this account | `string` | n/a | yes |
7475
| <a name="input_allow_users_to_change_password"></a> [allow\_users\_to\_change\_password](#input\_allow\_users\_to\_change\_password) | Whether to allow users to change their own password | `bool` | `true` | no |
76+
| <a name="input_create"></a> [create](#input\_create) | Determines whether resources will be created (affects all resources) | `bool` | `true` | no |
7577
| <a name="input_create_account_password_policy"></a> [create\_account\_password\_policy](#input\_create\_account\_password\_policy) | Whether to create AWS IAM account password policy | `bool` | `true` | no |
76-
| <a name="input_get_caller_identity"></a> [get\_caller\_identity](#input\_get\_caller\_identity) | Whether to get AWS account ID, User ID, and ARN in which Terraform is authorized | `bool` | `true` | no |
7778
| <a name="input_hard_expiry"></a> [hard\_expiry](#input\_hard\_expiry) | Whether users are prevented from setting a new password after their password has expired (i.e. require administrator reset) | `bool` | `false` | no |
78-
| <a name="input_max_password_age"></a> [max\_password\_age](#input\_max\_password\_age) | The number of days that an user password is valid. | `number` | `0` | no |
79+
| <a name="input_max_password_age"></a> [max\_password\_age](#input\_max\_password\_age) | The number of days that an user password is valid | `number` | `0` | no |
7980
| <a name="input_minimum_password_length"></a> [minimum\_password\_length](#input\_minimum\_password\_length) | Minimum length to require for user passwords | `number` | `8` | no |
8081
| <a name="input_password_reuse_prevention"></a> [password\_reuse\_prevention](#input\_password\_reuse\_prevention) | The number of previous passwords that users are prevented from reusing | `number` | `null` | no |
8182
| <a name="input_require_lowercase_characters"></a> [require\_lowercase\_characters](#input\_require\_lowercase\_characters) | Whether to require lowercase characters for user passwords | `bool` | `true` | no |
@@ -90,7 +91,7 @@ No modules.
9091
| <a name="output_caller_identity_account_id"></a> [caller\_identity\_account\_id](#output\_caller\_identity\_account\_id) | The AWS Account ID number of the account that owns or contains the calling entity |
9192
| <a name="output_caller_identity_arn"></a> [caller\_identity\_arn](#output\_caller\_identity\_arn) | The AWS ARN associated with the calling entity |
9293
| <a name="output_caller_identity_user_id"></a> [caller\_identity\_user\_id](#output\_caller\_identity\_user\_id) | The unique identifier of the calling entity |
93-
| <a name="output_iam_account_password_policy_expire_passwords"></a> [iam\_account\_password\_policy\_expire\_passwords](#output\_iam\_account\_password\_policy\_expire\_passwords) | Indicates whether passwords in the account expire. Returns true if max\_password\_age contains a value greater than 0. Returns false if it is 0 or not present. |
94+
| <a name="output_iam_account_password_policy_expire_passwords"></a> [iam\_account\_password\_policy\_expire\_passwords](#output\_iam\_account\_password\_policy\_expire\_passwords) | Indicates whether passwords in the account expire. Returns true if max\_password\_age contains a value greater than 0. Returns false if it is 0 or not present |
9495
<!-- END_TF_DOCS -->
9596

9697
## License

modules/iam-account/main.tf

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
data "aws_caller_identity" "this" {
2-
count = var.get_caller_identity ? 1 : 0
3-
}
1+
################################################################################
2+
# Alias
3+
################################################################################
44

55
resource "aws_iam_account_alias" "this" {
6+
count = var.create ? 1 : 0
7+
68
account_alias = var.account_alias
79
}
810

11+
################################################################################
12+
# Password Policy
13+
################################################################################
14+
915
resource "aws_iam_account_password_policy" "this" {
10-
count = var.create_account_password_policy ? 1 : 0
16+
count = var.create && var.create_account_password_policy ? 1 : 0
1117

1218
max_password_age = var.max_password_age
1319
minimum_password_length = var.minimum_password_length

modules/iam-account/outputs.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
output "caller_identity_account_id" {
22
description = "The AWS Account ID number of the account that owns or contains the calling entity"
3-
value = try(data.aws_caller_identity.this[0].account_id, "")
3+
value = try(data.aws_caller_identity.this[0].account_id, null)
44
}
55

66
output "caller_identity_arn" {
77
description = "The AWS ARN associated with the calling entity"
8-
value = try(data.aws_caller_identity.this[0].arn, "")
8+
value = try(data.aws_caller_identity.this[0].arn, null)
99
}
1010

1111
output "caller_identity_user_id" {
1212
description = "The unique identifier of the calling entity"
13-
value = try(data.aws_caller_identity.this[0].user_id, "")
13+
value = try(data.aws_caller_identity.this[0].user_id, null)
1414
}
1515

1616
output "iam_account_password_policy_expire_passwords" {
17-
description = "Indicates whether passwords in the account expire. Returns true if max_password_age contains a value greater than 0. Returns false if it is 0 or not present."
18-
value = try(aws_iam_account_password_policy.this[0].expire_passwords, "")
17+
description = "Indicates whether passwords in the account expire. Returns true if max_password_age contains a value greater than 0. Returns false if it is 0 or not present"
18+
value = try(aws_iam_account_password_policy.this[0].expire_passwords, null)
1919
}

modules/iam-account/variables.tf

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
variable "get_caller_identity" {
2-
description = "Whether to get AWS account ID, User ID, and ARN in which Terraform is authorized"
1+
variable "create" {
2+
description = "Determines whether resources will be created (affects all resources)"
33
type = bool
44
default = true
55
}
66

7+
################################################################################
8+
# Alias
9+
################################################################################
10+
711
variable "account_alias" {
812
description = "AWS IAM account alias for this account"
913
type = string
1014
}
1115

16+
################################################################################
17+
# Password Policy
18+
################################################################################
19+
1220
variable "create_account_password_policy" {
1321
description = "Whether to create AWS IAM account password policy"
1422
type = bool
1523
default = true
1624
}
1725

1826
variable "max_password_age" {
19-
description = "The number of days that an user password is valid."
27+
description = "The number of days that an user password is valid"
2028
type = number
2129
default = 0
2230
}

modules/iam-group/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ No modules.
7777
| <a name="input_enable_self_management_permissions"></a> [enable\_self\_management\_permissions](#input\_enable\_self\_management\_permissions) | Determines whether permissions are added to the policy which allow the groups IAM users to manage their credentials and MFA | `bool` | `true` | no |
7878
| <a name="input_name"></a> [name](#input\_name) | The group's name. The name must consist of upper and lowercase alphanumeric characters with no spaces. You can also include any of the following characters: `=,.@-_.` | `string` | `""` | no |
7979
| <a name="input_path"></a> [path](#input\_path) | Path in which to create the group | `string` | `null` | no |
80-
| <a name="input_permission_statements"></a> [permission\_statements](#input\_permission\_statements) | List of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for the policy | `any` | `[]` | no |
80+
| <a name="input_permission_statements"></a> [permission\_statements](#input\_permission\_statements) | List of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for the policy | <pre>list(object({<br/> sid = optional(string)<br/> actions = optional(list(string))<br/> not_actions = optional(list(string))<br/> effect = optional(string)<br/> resources = optional(list(string))<br/> not_resources = optional(list(string))<br/> principals = optional(list(object({<br/> type = string<br/> identifiers = list(string)<br/> })))<br/> not_principals = optional(list(object({<br/> type = string<br/> identifiers = list(string)<br/> })))<br/> condition = optional(list(object({<br/> test = string<br/> values = list(string)<br/> variable = string<br/> })))<br/> }))</pre> | `null` | no |
8181
| <a name="input_policies"></a> [policies](#input\_policies) | Policies to attach to the IAM role in `{'static_name' = 'policy_arn'}` format | `map(string)` | `{}` | no |
8282
| <a name="input_policy_description"></a> [policy\_description](#input\_policy\_description) | Description of the IAM policy | `string` | `null` | no |
83-
| <a name="input_policy_name_prefix"></a> [policy\_name\_prefix](#input\_policy\_name\_prefix) | Name prefix for IAM policy | `string` | `null` | no |
83+
| <a name="input_policy_name"></a> [policy\_name](#input\_policy\_name) | Name to use on IAM policy created | `string` | `null` | no |
8484
| <a name="input_policy_path"></a> [policy\_path](#input\_policy\_path) | The IAM policy path | `string` | `null` | no |
85+
| <a name="input_policy_use_name_prefix"></a> [policy\_use\_name\_prefix](#input\_policy\_use\_name\_prefix) | Determines whether the IAM policy name (`policy_name`) is used as a prefix | `bool` | `true` | no |
8586
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no |
8687
| <a name="input_users"></a> [users](#input\_users) | A list of IAM User names to associate with the Group | `list(string)` | `[]` | no |
8788
| <a name="input_users_account_id"></a> [users\_account\_id](#input\_users\_account\_id) | An overriding AWS account ID where the group's users reside; leave empty to use the current account ID for the AWS provider | `string` | `null` | no |

modules/iam-group/main.tf

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
data "aws_partition" "current" {}
2-
data "aws_caller_identity" "current" {}
1+
data "aws_partition" "current" {
2+
count = var.create ? 1 : 0
3+
}
4+
data "aws_caller_identity" "current" {
5+
count = var.create ? 1 : 0
6+
}
37

48
locals {
5-
users_account_id = coalesce(var.users_account_id, data.aws_caller_identity.current.account_id)
9+
partition = try(data.aws_partition.current[0].partition, "")
10+
users_account_id = try(coalesce(var.users_account_id, data.aws_caller_identity.current[0].account_id), "")
611

712
user_resources = [for pattern in ["user/$${aws:username}", "user/*/$${aws:username}"] :
8-
"arn:${data.aws_partition.current.partition}:iam::${local.users_account_id}:${pattern}"
13+
"arn:${local.partition}:iam::${local.users_account_id}:${pattern}"
914
]
1015
}
1116

@@ -34,6 +39,8 @@ resource "aws_iam_group_membership" "this" {
3439

3540
locals {
3641
create_policy = var.create && var.create_policy && (var.enable_self_management_permissions || length(var.permission_statements) > 0)
42+
43+
policy_name = try(coalesce(var.policy_name, var.name), "")
3744
}
3845

3946
# Allows MFA-authenticated IAM users to manage their own credentials on the My security credentials page
@@ -135,7 +142,7 @@ data "aws_iam_policy_document" "this" {
135142
content {
136143
sid = "ManageOwnVirtualMFADevice"
137144
actions = ["iam:CreateVirtualMFADevice"]
138-
resources = ["arn:${data.aws_partition.current.partition}:iam::${local.users_account_id}:mfa/*"]
145+
resources = ["arn:${local.partition}:iam::${local.users_account_id}:mfa/*"]
139146
}
140147
}
141148

@@ -180,18 +187,18 @@ data "aws_iam_policy_document" "this" {
180187
}
181188

182189
dynamic "statement" {
183-
for_each = var.permission_statements
190+
for_each = var.permission_statements != null ? var.permission_statements : []
184191

185192
content {
186-
sid = try(statement.value.sid, null)
187-
actions = try(statement.value.actions, null)
188-
not_actions = try(statement.value.not_actions, null)
189-
effect = try(statement.value.effect, null)
190-
resources = try(statement.value.resources, null)
191-
not_resources = try(statement.value.not_resources, null)
193+
sid = statement.value.sid
194+
actions = statement.value.actions
195+
not_actions = statement.value.not_actions
196+
effect = statement.value.effect
197+
resources = statement.value.resources
198+
not_resources = statement.value.not_resources
192199

193200
dynamic "principals" {
194-
for_each = try(statement.value.principals, [])
201+
for_each = statement.value.principals != null ? statement.value.principals : []
195202

196203
content {
197204
type = principals.value.type
@@ -200,7 +207,7 @@ data "aws_iam_policy_document" "this" {
200207
}
201208

202209
dynamic "not_principals" {
203-
for_each = try(statement.value.not_principals, [])
210+
for_each = statement.value.not_principals != null ? statement.value.not_principals : []
204211

205212
content {
206213
type = not_principals.value.type
@@ -209,7 +216,7 @@ data "aws_iam_policy_document" "this" {
209216
}
210217

211218
dynamic "condition" {
212-
for_each = try(statement.value.conditions, [])
219+
for_each = statement.value.condition != null ? statement.value.condition : []
213220

214221
content {
215222
test = condition.value.test
@@ -224,8 +231,9 @@ data "aws_iam_policy_document" "this" {
224231
resource "aws_iam_policy" "this" {
225232
count = local.create_policy ? 1 : 0
226233

227-
name_prefix = try(coalesce(var.policy_name_prefix, "${var.name}-"), null)
228234
description = var.policy_description
235+
name = var.policy_use_name_prefix ? null : local.policy_name
236+
name_prefix = var.policy_use_name_prefix ? "${local.policy_name}-" : null
229237
path = coalesce(var.policy_path, var.path, "/")
230238
policy = data.aws_iam_policy_document.this[0].json
231239

modules/iam-group/variables.tf

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,42 @@ variable "enable_mfa_enforcment" {
5656

5757
variable "permission_statements" {
5858
description = "List of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for the policy"
59-
type = any
60-
default = []
59+
type = list(object({
60+
sid = optional(string)
61+
actions = optional(list(string))
62+
not_actions = optional(list(string))
63+
effect = optional(string)
64+
resources = optional(list(string))
65+
not_resources = optional(list(string))
66+
principals = optional(list(object({
67+
type = string
68+
identifiers = list(string)
69+
})))
70+
not_principals = optional(list(object({
71+
type = string
72+
identifiers = list(string)
73+
})))
74+
condition = optional(list(object({
75+
test = string
76+
values = list(string)
77+
variable = string
78+
})))
79+
}))
80+
default = null
6181
}
6282

63-
variable "policy_name_prefix" {
64-
description = "Name prefix for IAM policy"
83+
variable "policy_name" {
84+
description = "Name to use on IAM policy created"
6585
type = string
6686
default = null
6787
}
6888

89+
variable "policy_use_name_prefix" {
90+
description = "Determines whether the IAM policy name (`policy_name`) is used as a prefix"
91+
type = bool
92+
default = true
93+
}
94+
6995
variable "policy_description" {
7096
description = "Description of the IAM policy"
7197
type = string

modules/iam-oidc-provider/main.tf

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
data "aws_partition" "current" {}
1+
data "aws_partition" "current" {
2+
count = var.create ? 1 : 0
3+
}
4+
5+
locals {
6+
dns_suffix = try(data.aws_partition.current[0].dns_suffix, "")
7+
}
28

39
################################################################################
4-
# GitHub OIDC Provider
10+
# OIDC Provider
511
################################################################################
612

713
data "tls_certificate" "this" {
@@ -14,7 +20,7 @@ resource "aws_iam_openid_connect_provider" "this" {
1420
count = var.create ? 1 : 0
1521

1622
url = var.url
17-
client_id_list = coalescelist(var.client_id_list, ["sts.${data.aws_partition.current.dns_suffix}"])
23+
client_id_list = coalescelist(var.client_id_list, ["sts.${local.dns_suffix}"])
1824
thumbprint_list = data.tls_certificate.this[0].certificates[*].sha1_fingerprint
1925

2026
tags = var.tags

modules/iam-oidc-provider/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ variable "tags" {
1010
default = {}
1111
}
1212

13+
################################################################################
14+
# OIDC Provider
15+
################################################################################
16+
1317
variable "client_id_list" {
1418
description = "List of client IDs (also known as audiences) for the IAM OIDC provider. Defaults to STS service if not values are provided"
1519
type = list(string)

0 commit comments

Comments
 (0)