forked from schubergphilis/terraform-aws-mcaf-avm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
173 lines (155 loc) · 9.76 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
locals {
tfe_workspace = {
clear_text_terraform_variables = merge(
// always add account = var.name
{ account = var.name },
// if environment, add environment = var.account.environment
var.account.environment != null ? { environment = var.account.environment } : {},
// if workload_boundary_arn, add workload_permissions_boundary_arn = aws_iam_policy.workload_boundary[0].arn
var.permissions_boundaries.workload_boundary != null && var.permissions_boundaries.workload_boundary != null ? { workload_permissions_boundary_arn = aws_iam_policy.workload_boundary[0].arn } : {}
)
working_directory = var.account.environment != null ? "terraform/${var.account.environment}" : "terraform"
}
}
provider "aws" {
alias = "account"
region = var.tfe_workspace.default_region
assume_role {
role_arn = "arn:aws:iam::${module.account.id}:role/AWSControlTowerExecution"
}
}
module "account" {
source = "github.com/schubergphilis/terraform-aws-mcaf-account?ref=v0.5.1"
account = var.name
email = var.account.email
organizational_unit = var.account.organizational_unit
provisioned_product_name = var.account.provisioned_product_name
sso_email = var.account.sso_email
sso_firstname = var.account.sso_firstname
sso_lastname = var.account.sso_lastname
}
resource "aws_iam_policy" "workspace_boundary" {
provider = aws.account
count = var.permissions_boundaries.workspace_boundary_name != null && var.permissions_boundaries.workspace_boundary != null ? 1 : 0
name = var.permissions_boundaries.workspace_boundary_name
path = var.path
policy = templatefile(var.permissions_boundaries.workspace_boundary, { account_id = module.account.id })
}
resource "aws_iam_policy" "workload_boundary" {
provider = aws.account
count = var.permissions_boundaries.workload_boundary_name != null && var.permissions_boundaries.workload_boundary != null ? 1 : 0
name = var.permissions_boundaries.workload_boundary_name
path = var.path
policy = templatefile(var.permissions_boundaries.workload_boundary, { account_id = module.account.id })
}
module "tfe_workspace" {
count = var.create_default_workspace ? 1 : 0
source = "github.com/schubergphilis/terraform-aws-mcaf-workspace?ref=v0.13.0"
providers = { aws = aws.account }
agent_pool_id = var.tfe_workspace.agent_pool_id
agent_role_arn = var.tfe_workspace.agent_role_arn
auth_method = var.tfe_workspace.auth_method
auto_apply = var.tfe_workspace.auto_apply
branch = var.tfe_workspace.branch
clear_text_env_variables = var.tfe_workspace.clear_text_env_variables
clear_text_hcl_variables = var.tfe_workspace.clear_text_hcl_variables
clear_text_terraform_variables = merge(local.tfe_workspace.clear_text_terraform_variables, var.tfe_workspace.clear_text_terraform_variables)
execution_mode = var.tfe_workspace.execution_mode
file_triggers_enabled = var.tfe_workspace.file_triggers_enabled
global_remote_state = var.tfe_workspace.global_remote_state
name = coalesce(var.tfe_workspace.name, var.name)
oauth_token_id = var.tfe_workspace.vcs_oauth_token_id
path = var.path
permissions_boundary_arn = try(aws_iam_policy.workspace_boundary[0].arn, null)
policy = var.tfe_workspace.policy
policy_arns = var.tfe_workspace.policy_arns
project_id = var.tfe_workspace.project_id
region = var.tfe_workspace.default_region
remote_state_consumer_ids = var.tfe_workspace.remote_state_consumer_ids
repository_identifier = var.tfe_workspace.repository_identifier
role_name = var.tfe_workspace.role_name
sensitive_env_variables = var.tfe_workspace.sensitive_env_variables
sensitive_hcl_variables = var.tfe_workspace.sensitive_hcl_variables
sensitive_terraform_variables = var.tfe_workspace.sensitive_terraform_variables
slack_notification_triggers = var.tfe_workspace.slack_notification_triggers
slack_notification_url = var.tfe_workspace.slack_notification_url
ssh_key_id = var.tfe_workspace.ssh_key_id
tags = var.tags
team_access = var.tfe_workspace.team_access
terraform_organization = var.tfe_workspace.organization
terraform_version = var.tfe_workspace.terraform_version
trigger_prefixes = var.tfe_workspace.trigger_prefixes
username = var.tfe_workspace.username
working_directory = var.tfe_workspace.working_directory != null ? var.tfe_workspace.working_directory : local.tfe_workspace.working_directory
}
module "additional_tfe_workspaces" {
for_each = var.additional_tfe_workspaces
source = "github.com/schubergphilis/terraform-aws-mcaf-workspace?ref=v0.13.0"
providers = { aws = aws.account }
agent_pool_id = each.value.agent_pool_id != null ? each.value.agent_pool_id : var.tfe_workspace.agent_pool_id
agent_role_arn = each.value.agent_role_arn != null ? each.value.agent_role_arn : var.tfe_workspace.agent_role_arn
auth_method = each.value.auth_method != null ? each.value.auth_method : var.tfe_workspace.auth_method
auto_apply = each.value.auto_apply
branch = coalesce(each.value.branch, var.tfe_workspace.branch)
clear_text_env_variables = each.value.clear_text_env_variables
clear_text_hcl_variables = each.value.clear_text_hcl_variables
clear_text_terraform_variables = merge(local.tfe_workspace.clear_text_terraform_variables, each.value.clear_text_terraform_variables)
execution_mode = coalesce(each.value.execution_mode, var.tfe_workspace.execution_mode)
file_triggers_enabled = each.value.file_triggers_enabled
global_remote_state = each.value.global_remote_state
name = coalesce(each.value.name, each.key)
oauth_token_id = coalesce(each.value.vcs_oauth_token_id, var.tfe_workspace.vcs_oauth_token_id)
path = var.path
permissions_boundary_arn = try(aws_iam_policy.workspace_boundary[0].arn, null)
policy = each.value.policy
policy_arns = each.value.policy_arns
project_id = each.value.project_id != null ? each.value.project_id : var.tfe_workspace.project_id
region = coalesce(each.value.default_region, var.tfe_workspace.default_region)
remote_state_consumer_ids = each.value.remote_state_consumer_ids
repository_identifier = coalesce(each.value.repository_identifier, var.tfe_workspace.repository_identifier)
role_name = coalesce(each.value.role_name, "TFEPipeline${replace(title(each.key), "/[_-]/", "")}")
sensitive_env_variables = each.value.sensitive_env_variables
sensitive_hcl_variables = each.value.sensitive_hcl_variables
sensitive_terraform_variables = each.value.sensitive_terraform_variables
slack_notification_triggers = coalesce(each.value.slack_notification_triggers, var.tfe_workspace.slack_notification_triggers)
slack_notification_url = each.value.slack_notification_url != null ? each.value.slack_notification_url : var.tfe_workspace.slack_notification_url
ssh_key_id = each.value.ssh_key_id != null ? each.value.ssh_key_id : var.tfe_workspace.ssh_key_id
tags = var.tags
team_access = each.value.team_access != {} ? each.value.team_access : var.tfe_workspace.team_access
terraform_organization = var.tfe_workspace.organization
terraform_version = each.value.terraform_version != null ? each.value.terraform_version : var.tfe_workspace.terraform_version
trigger_prefixes = coalesce(each.value.trigger_prefixes, var.tfe_workspace.trigger_prefixes)
username = coalesce(each.value.username, "TFEPipeline-${each.key}")
working_directory = coalesce(each.value.working_directory, "terraform/${coalesce(each.value.name, each.key)}")
}
resource "aws_iam_account_alias" "alias" {
provider = aws.account
account_alias = "${var.account.alias_prefix}${var.name}"
}
resource "aws_account_alternate_contact" "billing" {
count = var.account.contact_billing == null ? 0 : 1
provider = aws.account
alternate_contact_type = "BILLING"
email_address = var.account.contact_billing.email_address
name = var.account.contact_billing.name
phone_number = var.account.contact_billing.phone_number
title = var.account.contact_billing.title
}
resource "aws_account_alternate_contact" "operations" {
count = var.account.contact_operations == null ? 0 : 1
provider = aws.account
alternate_contact_type = "OPERATIONS"
email_address = var.account.contact_operations.email_address
name = var.account.contact_operations.name
phone_number = var.account.contact_operations.phone_number
title = var.account.contact_operations.title
}
resource "aws_account_alternate_contact" "security" {
count = var.account.contact_security == null ? 0 : 1
provider = aws.account
alternate_contact_type = "SECURITY"
email_address = var.account.contact_security.email_address
name = var.account.contact_security.name
phone_number = var.account.contact_security.phone_number
title = var.account.contact_security.title
}