-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
228 lines (202 loc) · 6.43 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# checkov:skip=CKV_*
locals {
s3_state_file_url = "https://${var.s3_bucket_name}.s3.${var.s3_bucket_region}.amazonaws.com/${var.s3_state_file_path}"
statefile_name = split("/", local.s3_state_file_url)[length(split("/", local.s3_state_file_url)) - 1]
declared_app_components = [
for app_component in var.app_components :
{
name = app_component["app_component_name"]
type = app_component["app_component_type"]
resourceNames = [
for resource in app_component["resources"] :
resource["resource_name"]
]
}
]
app_common_app_component = [
{
name = "appcommon"
type = "AWS::ResilienceHub::AppCommonAppComponent",
resourceNames : []
}
]
app_components = tolist(concat(local.declared_app_components, local.app_common_app_component))
resources_list = flatten([
for component in var.app_components : [
for resource in component["resources"] : {
resource_name = resource["resource_name"]
resource_type = resource["resource_type"]
resource_identifier_type = resource["resource_identifier_type"]
resource_identifier = resource["resource_identifier"]
resource_region = resource["resource_region"]
}
]
])
state_file_mapping = [
{
mapping_type = "Terraform"
physical_resource_id = {
identifier = local.s3_state_file_url
type = "Native"
}
terraform_source_name = local.statefile_name
}
]
eks_mappings_only = tolist(toset([
for resource in local.resources_list :
{
mapping_type = "EKS"
physical_resource_id = {
# clusterArn/namespace
identifier = join("/", slice(split("/", resource["resource_identifier"]), 0, 3))
type = "Arn"
aws_region = resource["resource_region"]
}
# clusterName/namespace
eks_source_name = join("/", slice(split("/", resource["resource_identifier"]), 1, 3))
}
if startswith(resource["resource_type"], "AWS::EKS")
]))
resource_mappings = concat(local.eks_mappings_only, local.state_file_mapping)
statefile_resources_json = [
for resource in local.resources_list :
{
logicalResourceId = {
identifier = resource["resource_name"]
terraformSourceName = local.statefile_name
}
physicalResourceId = {
identifier = resource["resource_identifier"]
awsRegion = resource["resource_region"]
type = resource["resource_identifier_type"]
}
type = resource["resource_type"]
name = resource["resource_name"]
}
if !startswith(resource["resource_type"], "AWS::EKS")
]
eks_resources_json = [
for resource in local.resources_list :
{
logicalResourceId = {
# clusterName/namespace
eksSourceName = join("/", slice(split("/", resource["resource_identifier"]), 1, 3))
# resource_name must be the deployment/replica/pod name
identifier = resource["resource_name"]
}
type = resource["resource_type"]
name = resource["resource_name"]
}
if startswith(resource["resource_type"], "AWS::EKS")
]
resources_json = concat(local.statefile_resources_json, local.eks_resources_json)
}
resource "random_id" "session" {
byte_length = 16
}
resource "aws_iam_role" "resilience_hub_assessment_role" {
name = var.arh_role_name
assume_role_policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Service" : "resiliencehub.amazonaws.com"
},
"Action" : "sts:AssumeRole"
}
]
})
}
resource "aws_iam_role_policy_attachment" "resilience_hub_assessment_attachment" {
role = aws_iam_role.resilience_hub_assessment_role.name
policy_arn = "arn:aws:iam::aws:policy/AWSResilienceHubAsssessmentExecutionPolicy"
}
# tfsec:ignore:aws-iam-no-policy-wildcards
resource "aws_iam_policy" "resilience_hub_full_access_policy" {
name = "ResilienceHubFullAccessPolicy-${random_id.session.id}"
description = "Policy granting full access to Resilience Hub"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
# Need in order to manage Resilience Hub resources
Action = "resiliencehub:*"
Resource = "*"
}
]
})
}
resource "aws_iam_role_policy_attachment" "resilience_hub_full_access_attachment" {
role = aws_iam_role.resilience_hub_assessment_role.name
policy_arn = aws_iam_policy.resilience_hub_full_access_policy.arn
}
# tfsec:ignore:aws-iam-no-policy-wildcards
resource "aws_iam_policy" "additional_invoker_role_permissions" {
name = "AdditionalInvokerRolePermissions-${random_id.session.id}"
description = "Policy granting additional permissions to InvokerRole"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
# Need in order to be able to read statefiles for assessments
Action = "s3:GetObject"
Resource = "*"
}
]
})
}
resource "aws_iam_role_policy_attachment" "resilience_hub_additional_access_attachment" {
role = aws_iam_role.resilience_hub_assessment_role.name
policy_arn = aws_iam_policy.additional_invoker_role_permissions.arn
}
resource "awscc_resiliencehub_app" "app" {
name = var.app_name
app_template_body = jsonencode({
resources = local.resources_json
appComponents = local.app_components
excludedResources = {}
version = 2
})
resource_mappings = local.resource_mappings
resiliency_policy_arn = awscc_resiliencehub_resiliency_policy.policy.policy_arn
tags = {
"terraform" = "managed"
}
permission_model = {
type = "RoleBased"
invoker_role_name = var.arh_role_name
}
depends_on = [
aws_iam_role.resilience_hub_assessment_role,
awscc_resiliencehub_resiliency_policy.policy
]
}
resource "awscc_resiliencehub_resiliency_policy" "policy" {
policy_name = "Policy-${random_id.session.id}"
tier = "MissionCritical"
policy = {
az = {
rto_in_secs = var.rto
rpo_in_secs = var.rpo
}
hardware = {
rto_in_secs = var.rto
rpo_in_secs = var.rpo
}
software = {
rto_in_secs = var.rto
rpo_in_secs = var.rpo
}
region = {
rto_in_secs = var.rto
rpo_in_secs = var.rpo
}
}
tags = {
"terraform" = "managed"
}
}