generated from cloudposse/terraform-example-module
-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.tf
296 lines (238 loc) · 10.9 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
locals {
enabled = module.this.enabled
count = local.enabled ? 1 : 0
id = local.enabled ? join("", aws_codedeploy_app.default[*].id) : null
name = local.enabled ? join("", aws_codedeploy_app.default[*].name) : null
group_id = local.enabled ? join("", aws_codedeploy_deployment_group.default[*].id) : null
deployment_config_name = local.enabled ? join("", aws_codedeploy_deployment_config.default[*].id) : null
deployment_config_id = local.enabled ? join("", aws_codedeploy_deployment_config.default[*].deployment_config_id) : null
auto_rollback_configuration_enabled = local.enabled && var.auto_rollback_configuration_events != null && length(var.auto_rollback_configuration_events) > 0
alarm_configuration_enabled = local.enabled && var.alarm_configuration != null
default_sns_topic_enabled = local.enabled && var.create_default_sns_topic
sns_topic_arn = local.default_sns_topic_enabled ? module.sns_topic.sns_topic.arn : var.sns_topic_arn
default_service_role_enabled = local.enabled && var.create_default_service_role
default_service_role_count = local.default_service_role_enabled ? 1 : 0
service_role_arn = local.default_service_role_enabled ? join("", aws_iam_role.default[*].arn) : var.service_role_arn
default_policy_arn = {
Server = "arn:${join("", data.aws_partition.current[*].partition)}:iam::aws:policy/service-role/AWSCodeDeployRole"
Lambda = "arn:${join("", data.aws_partition.current[*].partition)}:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda"
ECS = "arn:${join("", data.aws_partition.current[*].partition)}:iam::aws:policy/AWSCodeDeployRoleForECS"
}
}
data "aws_iam_policy_document" "assume_role" {
count = local.default_service_role_count
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["codedeploy.amazonaws.com"]
}
}
}
data "aws_partition" "current" {
count = local.default_service_role_count
}
resource "aws_iam_role" "default" {
count = local.default_service_role_count
name = module.this.id
assume_role_policy = join("", data.aws_iam_policy_document.assume_role[*].json)
tags = module.this.tags
}
resource "aws_iam_role_policy_attachment" "default" {
count = local.default_service_role_count
policy_arn = format("%s", lookup(local.default_policy_arn, var.compute_platform))
role = join("", aws_iam_role.default[*].name)
}
module "sns_topic" {
source = "cloudposse/sns-topic/aws"
version = "0.21.0"
enabled = local.default_sns_topic_enabled
context = module.this.context
}
resource "aws_codedeploy_app" "default" {
count = local.count
name = module.this.id
compute_platform = var.compute_platform
tags = module.this.tags
}
resource "aws_codedeploy_deployment_config" "default" {
count = local.count
deployment_config_name = module.this.id
compute_platform = var.compute_platform
dynamic "minimum_healthy_hosts" {
for_each = var.minimum_healthy_hosts == null ? [] : [var.minimum_healthy_hosts]
content {
type = minimum_healthy_hosts.value.type
value = minimum_healthy_hosts.value.value
}
}
dynamic "traffic_routing_config" {
for_each = var.traffic_routing_config == null ? [] : [var.traffic_routing_config]
content {
type = traffic_routing_config.value.type
dynamic "time_based_linear" {
for_each = var.traffic_routing_config != null && lookup(var.traffic_routing_config, "type", null) == "TimeBasedLinear" ? [var.traffic_routing_config] : []
content {
interval = traffic_routing_config.value.interval
percentage = traffic_routing_config.value.percentage
}
}
dynamic "time_based_canary" {
for_each = var.traffic_routing_config != null && lookup(var.traffic_routing_config, "type", null) == "TimeBasedCanary" ? [var.traffic_routing_config] : []
content {
interval = traffic_routing_config.value.interval
percentage = traffic_routing_config.value.percentage
}
}
}
}
}
resource "aws_codedeploy_deployment_group" "default" {
count = local.count
app_name = local.name
deployment_group_name = module.this.id
deployment_config_name = local.deployment_config_name
service_role_arn = local.service_role_arn
autoscaling_groups = var.autoscaling_groups
dynamic "alarm_configuration" {
for_each = local.alarm_configuration_enabled ? [var.alarm_configuration] : []
content {
alarms = lookup(alarm_configuration.value, "alarms", null)
ignore_poll_alarm_failure = lookup(alarm_configuration.value, "ignore_poll_alarm_failure", null)
enabled = local.alarm_configuration_enabled
}
}
dynamic "auto_rollback_configuration" {
for_each = local.auto_rollback_configuration_enabled ? [1] : [0]
content {
enabled = local.auto_rollback_configuration_enabled
events = [var.auto_rollback_configuration_events]
}
}
dynamic "blue_green_deployment_config" {
for_each = var.blue_green_deployment_config == null ? [] : [var.blue_green_deployment_config]
content {
dynamic "deployment_ready_option" {
for_each = lookup(blue_green_deployment_config.value, "deployment_ready_option", null) == null ? [] : [lookup(blue_green_deployment_config.value, "deployment_ready_option", {})]
content {
action_on_timeout = lookup(deployment_ready_option.value, "action_on_timeout", null)
wait_time_in_minutes = lookup(deployment_ready_option.value, "wait_time_in_minutes", null)
}
}
dynamic "green_fleet_provisioning_option" {
for_each = lookup(blue_green_deployment_config.value, "green_fleet_provisioning_option", null) == null ? [] : [lookup(blue_green_deployment_config.value, "green_fleet_provisioning_option", {})]
content {
action = lookup(green_fleet_provisioning_option.value, "action", null)
}
}
dynamic "terminate_blue_instances_on_deployment_success" {
for_each = lookup(blue_green_deployment_config.value, "terminate_blue_instances_on_deployment_success", null) == null ? [] : [lookup(blue_green_deployment_config.value, "terminate_blue_instances_on_deployment_success", {})]
content {
action = lookup(terminate_blue_instances_on_deployment_success.value, "action", null)
termination_wait_time_in_minutes = lookup(terminate_blue_instances_on_deployment_success.value, "termination_wait_time_in_minutes", null)
}
}
}
}
dynamic "deployment_style" {
for_each = var.deployment_style == null ? [] : [var.deployment_style]
content {
deployment_option = deployment_style.value.deployment_option
deployment_type = deployment_style.value.deployment_type
}
}
# Note that you cannot have both ec_tag_filter and ec2_tag_set vars set!
# See https://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment-group.html for details
dynamic "ec2_tag_filter" {
for_each = length(var.ec2_tag_filter) > 0 ? var.ec2_tag_filter : []
content {
key = lookup(ec2_tag_filter.value, "key", null)
type = lookup(ec2_tag_filter.value, "type", null)
value = lookup(ec2_tag_filter.value, "value", null)
}
}
# Note that you cannot have both ec_tag_filter and ec2_tag_set vars set!
# See https://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment-group.html for details
dynamic "ec2_tag_set" {
for_each = length(var.ec2_tag_set) > 0 ? var.ec2_tag_set : []
content {
dynamic "ec2_tag_filter" {
for_each = ec2_tag_set.value.ec2_tag_filter
content {
key = lookup(ec2_tag_filter.value, "key", null)
type = lookup(ec2_tag_filter.value, "type", null)
value = lookup(ec2_tag_filter.value, "value", null)
}
}
}
}
dynamic "ecs_service" {
for_each = var.ecs_service == null ? [] : var.ecs_service
content {
cluster_name = ecs_service.value.cluster_name
service_name = ecs_service.value.service_name
}
}
dynamic "load_balancer_info" {
for_each = var.load_balancer_info == null ? [] : [var.load_balancer_info]
content {
dynamic "elb_info" {
for_each = lookup(load_balancer_info.value, "elb_info", null) == null ? [] : [load_balancer_info.value.elb_info]
content {
name = elb_info.value.name
}
}
dynamic "target_group_info" {
for_each = lookup(load_balancer_info.value, "target_group_info", null) == null ? [] : [load_balancer_info.value.target_group_info]
content {
name = target_group_info.value.name
}
}
dynamic "target_group_pair_info" {
for_each = lookup(load_balancer_info.value, "target_group_pair_info", null) == null ? [] : [load_balancer_info.value.target_group_pair_info]
content {
dynamic "prod_traffic_route" {
for_each = lookup(target_group_pair_info.value, "prod_traffic_route", null) == null ? [] : [target_group_pair_info.value.prod_traffic_route]
content {
listener_arns = prod_traffic_route.value.listener_arns
}
}
dynamic "target_group" {
for_each = lookup(target_group_pair_info.value, "target_group", null) == null ? [] : [target_group_pair_info.value.target_group]
content {
name = target_group.value.name
}
}
dynamic "target_group" {
for_each = lookup(target_group_pair_info.value, "blue_target_group", null) == null ? [] : [target_group_pair_info.value.blue_target_group]
content {
name = target_group.value.name
}
}
dynamic "target_group" {
for_each = lookup(target_group_pair_info.value, "green_target_group", null) == null ? [] : [target_group_pair_info.value.green_target_group]
content {
name = target_group.value.name
}
}
dynamic "test_traffic_route" {
for_each = lookup(target_group_pair_info.value, "test_traffic_route", null) == null ? [] : [target_group_pair_info.value.test_traffic_route]
content {
listener_arns = test_traffic_route.value.listener_arns
}
}
}
}
}
}
dynamic "trigger_configuration" {
for_each = local.sns_topic_arn == null ? [0] : [1]
content {
trigger_events = var.trigger_events
trigger_name = module.this.id
trigger_target_arn = local.sns_topic_arn
}
}
tags = module.this.tags
}