Skip to content

Commit e198524

Browse files
authored
Move profiles_enabled logic out of providers.tf and into iam-roles (#702)
1 parent 79aba10 commit e198524

File tree

22 files changed

+132
-49
lines changed

22 files changed

+132
-49
lines changed

modules/account-map/modules/iam-roles/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
This submodule is used by other modules to determine which IAM Roles
44
or AWS CLI Config Profiles to use for various tasks, most commonly
5-
for applying Terraform plans.
5+
for applying Terraform plans.
66

77
## Special Configuration Needed
88

99
In order to avoid having to pass customization information through every module
1010
that uses this submodule, if the default configuration does not suit your needs,
11-
you are expected to customize `variables.tf` with the defaults you want to
12-
use in your project. For example, if you are including the `tenant` label
13-
in the designation of your "root" account (your Organization Management Account),
14-
then you should modify `variables.tf` so that `global_tenant_name` defaults
15-
to the appropriate value.
11+
you are expected to add `variables_override.tf` to override the variables with
12+
the defaults you want to use in your project. For example, if you are not using
13+
"core" as the `tenant` portion of your "root" account (your Organization Management Account),
14+
then you should include the `variable "overridable_global_tenant_name"` declaration
15+
in your `variables_override.tf` so that `overridable_global_tenant_name` defaults
16+
to the value you are using (or the empty string if you are not using `tenant` at all).

modules/account-map/modules/iam-roles/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ module "account_map" {
2222
}
2323

2424
locals {
25-
account_name = lookup(module.always.descriptors, "account_name", module.always.stage)
25+
account_name = lookup(module.always.descriptors, "account_name", module.always.stage)
26+
profiles_enabled = module.account_map.outputs.profiles_enabled
2627
}

modules/account-map/modules/iam-roles/outputs.tf

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
output "terraform_role_arn" {
2-
value = module.account_map.outputs.terraform_roles[local.account_name]
2+
value = local.profiles_enabled ? null : module.account_map.outputs.terraform_roles[local.account_name]
33
description = "The AWS Role ARN for Terraform to use when provisioning resources in the account, when Role ARNs are in use"
44
}
55

@@ -9,7 +9,7 @@ output "terraform_role_arns" {
99
}
1010

1111
output "terraform_profile_name" {
12-
value = module.account_map.outputs.terraform_profiles[local.account_name]
12+
value = local.profiles_enabled ? module.account_map.outputs.terraform_profiles[local.account_name] : null
1313
description = "The AWS config profile name for Terraform to use when provisioning resources in the account, when profiles are in use"
1414
}
1515

@@ -27,17 +27,17 @@ output "org_role_arn" {
2727
}
2828

2929
output "global_tenant_name" {
30-
value = var.global_tenant_name
30+
value = var.overridable_global_tenant_name
3131
description = "The `null-label` `tenant` value used for organization-wide resources"
3232
}
3333

3434
output "global_environment_name" {
35-
value = var.global_environment_name
35+
value = var.overridable_global_environment_name
3636
description = "The `null-label` `environment` value used for regionless (global) resources"
3737
}
3838

3939
output "global_stage_name" {
40-
value = var.global_stage_name
40+
value = var.overridable_global_stage_name
4141
description = "The `null-label` `stage` value for the organization management account (where the `account-map` state is stored)"
4242
}
4343

@@ -50,22 +50,22 @@ output "current_account_account_name" {
5050
}
5151

5252
output "dns_terraform_role_arn" {
53-
value = module.account_map.outputs.terraform_roles[module.account_map.outputs.dns_account_account_name]
53+
value = local.profiles_enabled ? null : module.account_map.outputs.terraform_roles[module.account_map.outputs.dns_account_account_name]
5454
description = "The AWS Role ARN for Terraform to use to provision DNS Zone delegations, when Role ARNs are in use"
5555
}
5656

5757
output "dns_terraform_profile_name" {
58-
value = module.account_map.outputs.terraform_profiles[module.account_map.outputs.dns_account_account_name]
58+
value = local.profiles_enabled ? module.account_map.outputs.terraform_profiles[module.account_map.outputs.dns_account_account_name] : null
5959
description = "The AWS config profile name for Terraform to use to provision DNS Zone delegations, when profiles are in use"
6060
}
6161

6262
output "audit_terraform_role_arn" {
63-
value = module.account_map.outputs.terraform_roles[module.account_map.outputs.audit_account_account_name]
63+
value = local.profiles_enabled ? null : module.account_map.outputs.terraform_roles[module.account_map.outputs.audit_account_account_name]
6464
description = "The AWS Role ARN for Terraform to use to provision resources in the \"audit\" role account, when Role ARNs are in use"
6565
}
6666

6767
output "audit_terraform_profile_name" {
68-
value = module.account_map.outputs.terraform_profiles[module.account_map.outputs.audit_account_account_name]
68+
value = local.profiles_enabled ? module.account_map.outputs.terraform_profiles[module.account_map.outputs.audit_account_account_name] : null
6969
description = "The AWS config profile name for Terraform to use to provision resources in the \"audit\" role account, when profiles are in use"
7070
}
7171

@@ -75,26 +75,26 @@ output "identity_account_account_name" {
7575
}
7676

7777
output "identity_terraform_role_arn" {
78-
value = module.account_map.outputs.terraform_roles[module.account_map.outputs.identity_account_account_name]
78+
value = local.profiles_enabled ? null : module.account_map.outputs.terraform_roles[module.account_map.outputs.identity_account_account_name]
7979
description = "The AWS Role ARN for Terraform to use to provision resources in the \"identity\" role account, when Role ARNs are in use"
8080
}
8181

8282
output "identity_terraform_profile_name" {
83-
value = module.account_map.outputs.terraform_profiles[module.account_map.outputs.identity_account_account_name]
83+
value = local.profiles_enabled ? module.account_map.outputs.terraform_profiles[module.account_map.outputs.identity_account_account_name] : null
8484
description = "The AWS config profile name for Terraform to use to provision resources in the \"identity\" role account, when profiles are in use"
8585
}
8686

8787
output "identity_cicd_role_arn" {
88-
value = module.account_map.outputs.cicd_roles[module.account_map.outputs.identity_account_account_name]
88+
value = local.profiles_enabled ? null : module.account_map.outputs.cicd_roles[module.account_map.outputs.identity_account_account_name]
8989
description = "(Deprecated) The AWS Role ARN for CI/CD tools to assume to gain access to other accounts, when Role ARNs are in use"
9090
}
9191

9292
output "identity_cicd_profile_name" {
93-
value = module.account_map.outputs.cicd_profiles[module.account_map.outputs.identity_account_account_name]
93+
value = local.profiles_enabled ? module.account_map.outputs.cicd_profiles[module.account_map.outputs.identity_account_account_name] : null
9494
description = "(Deprecated) The AWS config profile name for CI/CD tools to assume to gain access to other accounts, when profiles are in use"
9595
}
9696

9797
output "profiles_enabled" {
98-
value = module.account_map.outputs.profiles_enabled
98+
value = local.profiles_enabled
9999
description = "When true, use AWS config profiles in Terraform AWS provider configurations. When false, use Role ARNs."
100100
}

modules/account-map/modules/iam-roles/variables.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@ variable "privileged" {
44
default = false
55
}
66

7-
variable "global_tenant_name" {
7+
## The overridable_* variables in this file provide Cloud Posse defaults.
8+
## Because this module is used in bootstrapping Terraform, we do not configure
9+
## these inputs in the normal way. Instead, to change the values, you should
10+
## add a `variables_override.tf` file and change the default to the value you want.
11+
variable "overridable_global_tenant_name" {
812
type = string
913
description = "The tenant name used for organization-wide resources"
1014
default = "core"
1115
}
1216

13-
variable "global_environment_name" {
17+
variable "overridable_global_environment_name" {
1418
type = string
1519
description = "Global environment name"
1620
default = "gbl"
1721
}
1822

19-
variable "global_stage_name" {
23+
variable "overridable_global_stage_name" {
2024
type = string
21-
description = "The stage name for the organization management account (where the `accout-map` state is stored)"
25+
description = "The stage name for the organization management account (where the `account-map` state is stored)"
2226
default = "root"
2327
}

modules/aws-waf-acl/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ components:
8484
| <a name="input_geo_match_statement_rules"></a> [geo\_match\_statement\_rules](#input\_geo\_match\_statement\_rules) | A rule statement used to identify web requests based on country of origin.<br><br>action:<br> The action that AWS WAF should take on a web request when it matches the rule's statement.<br>name:<br> A friendly name of the rule.<br>priority:<br> If you define more than one Rule in a WebACL,<br> AWS WAF evaluates each request against the rules in order based on the value of priority.<br> AWS WAF processes rules with lower priority first.<br><br>statement:<br> country\_codes:<br> A list of two-character country codes.<br> forwarded\_ip\_config:<br> fallback\_behavior:<br> The match status to assign to the web request if the request doesn't have a valid IP address in the specified position.<br> Possible values: `MATCH`, `NO_MATCH`<br> header\_name:<br> The name of the HTTP header to use for the IP address.<br><br>visibility\_config:<br> Defines and enables Amazon CloudWatch metrics and web request sample collection.<br><br> cloudwatch\_metrics\_enabled:<br> Whether the associated resource sends metrics to CloudWatch.<br> metric\_name:<br> A friendly name of the CloudWatch metric.<br> sampled\_requests\_enabled:<br> Whether AWS WAF should store a sampling of the web requests that match the rules. | `list(any)` | `null` | no |
8585
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br>Set to `0` for unlimited length.<br>Set to `null` for default, which is `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
8686
| <a name="input_import_profile_name"></a> [import\_profile\_name](#input\_import\_profile\_name) | AWS Profile name to use when importing a resource | `string` | `null` | no |
87+
| <a name="input_import_role_arn"></a> [import\_role\_arn](#input\_import\_role\_arn) | IAM Role ARN to use when importing a resource | `string` | `null` | no |
8788
| <a name="input_ip_set_reference_statement_rules"></a> [ip\_set\_reference\_statement\_rules](#input\_ip\_set\_reference\_statement\_rules) | A rule statement used to detect web requests coming from particular IP addresses or address ranges.<br><br>action:<br> The action that AWS WAF should take on a web request when it matches the rule's statement.<br>name:<br> A friendly name of the rule.<br>priority:<br> If you define more than one Rule in a WebACL,<br> AWS WAF evaluates each request against the rules in order based on the value of priority.<br> AWS WAF processes rules with lower priority first.<br><br>statement:<br> arn:<br> The ARN of the IP Set that this statement references.<br> ip\_set\_forwarded\_ip\_config:<br> fallback\_behavior:<br> The match status to assign to the web request if the request doesn't have a valid IP address in the specified position.<br> Possible values: `MATCH`, `NO_MATCH`<br> header\_name:<br> The name of the HTTP header to use for the IP address.<br> position:<br> The position in the header to search for the IP address.<br> Possible values include: `FIRST`, `LAST`, or `ANY`.<br><br>visibility\_config:<br> Defines and enables Amazon CloudWatch metrics and web request sample collection.<br><br> cloudwatch\_metrics\_enabled:<br> Whether the associated resource sends metrics to CloudWatch.<br> metric\_name:<br> A friendly name of the CloudWatch metric.<br> sampled\_requests\_enabled:<br> Whether AWS WAF should store a sampling of the web requests that match the rules. | `list(any)` | `null` | no |
8889
| <a name="input_label_key_case"></a> [label\_key\_case](#input\_label\_key\_case) | The letter case of label keys (`tag` names) (i.e. `name`, `namespace`, `environment`, `stage`, `attributes`) to use in `tags`.<br>Possible values: `lower`, `title`, `upper`.<br>Default value: `title`. | `string` | `null` | no |
8990
| <a name="input_label_order"></a> [label\_order](#input\_label\_order) | The naming order of the id output and Name tag.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no |

modules/aws-waf-acl/providers.tf

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
provider "aws" {
2-
region = var.region
3-
profile = coalesce(var.import_profile_name, module.iam_roles.terraform_profile_name)
2+
region = var.region
3+
4+
profile = module.iam_roles.profiles_enabled ? coalesce(var.import_profile_name, module.iam_roles.terraform_profile_name) : null
5+
6+
dynamic "assume_role" {
7+
for_each = module.iam_roles.profiles_enabled ? [] : ["role"]
8+
content {
9+
role_arn = coalesce(var.import_role_arn, module.iam_roles.terraform_role_arn)
10+
}
11+
}
412
}
513

614
module "iam_roles" {
@@ -13,3 +21,9 @@ variable "import_profile_name" {
1321
default = null
1422
description = "AWS Profile name to use when importing a resource"
1523
}
24+
25+
variable "import_role_arn" {
26+
type = string
27+
default = null
28+
description = "IAM Role ARN to use when importing a resource"
29+
}

modules/cognito/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ components:
119119
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
120120
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br>Set to `0` for unlimited length.<br>Set to `null` for keep the existing setting, which defaults to `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
121121
| <a name="input_identity_providers"></a> [identity\_providers](#input\_identity\_providers) | Cognito Identity Providers configuration | `list(any)` | `[]` | no |
122-
| <a name="input_import_profile_name"></a> [import\_profile\_name](#input\_import\_profile\_name) | AWS Profile to use when importing a resource | `string` | `null` | no |
122+
| <a name="input_import_profile_name"></a> [import\_profile\_name](#input\_import\_profile\_name) | AWS Profile name to use when importing a resource | `string` | `null` | no |
123+
| <a name="input_import_role_arn"></a> [import\_role\_arn](#input\_import\_role\_arn) | IAM Role ARN to use when importing a resource | `string` | `null` | no |
123124
| <a name="input_label_key_case"></a> [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.<br>Does not affect keys of tags passed in via the `tags` input.<br>Possible values: `lower`, `title`, `upper`.<br>Default value: `title`. | `string` | `null` | no |
124125
| <a name="input_label_order"></a> [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no |
125126
| <a name="input_label_value_case"></a> [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,<br>set as tag values, and output by this module individually.<br>Does not affect values of tags passed in via the `tags` input.<br>Possible values: `lower`, `title`, `upper` and `none` (no transformation).<br>Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.<br>Default value: `lower`. | `string` | `null` | no |

modules/cognito/providers.tf

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
provider "aws" {
22
region = var.region
33

4-
profile = coalesce(var.import_profile_name, module.iam_roles.terraform_profile_name)
4+
profile = module.iam_roles.profiles_enabled ? coalesce(var.import_profile_name, module.iam_roles.terraform_profile_name) : null
5+
6+
dynamic "assume_role" {
7+
for_each = module.iam_roles.profiles_enabled ? [] : ["role"]
8+
content {
9+
role_arn = coalesce(var.import_role_arn, module.iam_roles.terraform_role_arn)
10+
}
11+
}
512
}
613

714
module "iam_roles" {
@@ -12,5 +19,11 @@ module "iam_roles" {
1219
variable "import_profile_name" {
1320
type = string
1421
default = null
15-
description = "AWS Profile to use when importing a resource"
22+
description = "AWS Profile name to use when importing a resource"
23+
}
24+
25+
variable "import_role_arn" {
26+
type = string
27+
default = null
28+
description = "IAM Role ARN to use when importing a resource"
1629
}

modules/dns-delegated/providers.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ provider "aws" {
2020
profile = module.iam_roles.profiles_enabled ? coalesce(var.import_profile_name, module.iam_roles.terraform_profile_name) : null
2121

2222
dynamic "assume_role" {
23-
for_each = var.import_role_arn == null ? (module.iam_roles.terraform_role_arn != null ? [true] : []) : ["import"]
23+
for_each = module.iam_roles.profiles_enabled ? [] : ["role"]
2424
content {
2525
role_arn = coalesce(var.import_role_arn, module.iam_roles.terraform_role_arn)
2626
}

modules/dns-primary/providers.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ provider "aws" {
44
profile = module.iam_roles.profiles_enabled ? coalesce(var.import_profile_name, module.iam_roles.terraform_profile_name) : null
55

66
dynamic "assume_role" {
7-
for_each = var.import_role_arn == null ? (module.iam_roles.terraform_role_arn != null ? [true] : []) : ["import"]
7+
for_each = module.iam_roles.profiles_enabled ? [] : ["role"]
88
content {
99
role_arn = coalesce(var.import_role_arn, module.iam_roles.terraform_role_arn)
1010
}

0 commit comments

Comments
 (0)