-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
133 lines (111 loc) · 4.69 KB
/
Copy pathmain.tf
File metadata and controls
133 lines (111 loc) · 4.69 KB
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
###############################################################################
# Locals
#
# render_configuration: the cluster `configuration` block is only emitted when
# the caller supplies execute-command settings or a CMK for managed storage /
# Fargate ephemeral storage. An empty configuration block would otherwise create
# noise in plans.
###############################################################################
locals {
render_configuration = (var.execute_command != null ||
var.kms_key_arn != null ||
var.fargate_ephemeral_storage_kms_key_arn != null)
render_managed_storage = (var.kms_key_arn != null ||
var.fargate_ephemeral_storage_kms_key_arn != null)
# Create the capacity-provider association only when there is something to
# associate (Fargate providers, EC2 providers, or a default strategy).
manage_capacity_providers = (length(var.fargate_capacity_providers) +
length(var.capacity_providers) +
length(var.default_capacity_provider_strategy)) > 0
}
###############################################################################
# ECS cluster (keystone)
###############################################################################
resource "aws_ecs_cluster" "this" {
name = var.name
setting {
name = "containerInsights"
value = var.container_insights
}
dynamic "configuration" {
for_each = local.render_configuration ? [1]: []
content {
dynamic "execute_command_configuration" {
for_each = var.execute_command != null ? [var.execute_command]: []
content {
kms_key_id = try(coalesce(execute_command_configuration.value.kms_key_arn, var.kms_key_arn), null)
logging = execute_command_configuration.value.logging
dynamic "log_configuration" {
for_each = execute_command_configuration.value.logging == "OVERRIDE" ? [1]: []
content {
cloud_watch_encryption_enabled = execute_command_configuration.value.cloudwatch_encryption_enabled
cloud_watch_log_group_name = execute_command_configuration.value.cloudwatch_log_group_name
s3_bucket_name = execute_command_configuration.value.s3_bucket_name
s3_bucket_encryption_enabled = execute_command_configuration.value.s3_bucket_encryption_enabled
s3_key_prefix = execute_command_configuration.value.s3_key_prefix
}
}
}
}
dynamic "managed_storage_configuration" {
for_each = local.render_managed_storage ? [1]: []
content {
kms_key_id = var.kms_key_arn
fargate_ephemeral_storage_kms_key_id = var.fargate_ephemeral_storage_kms_key_arn
}
}
}
}
dynamic "service_connect_defaults" {
for_each = var.service_connect_namespace != null ? [1]: []
content {
namespace = var.service_connect_namespace
}
}
tags = var.tags
}
###############################################################################
# EC2-backed capacity providers
#
# One aws_ecs_capacity_provider per entry, each bound to an externally-owned
# Auto Scaling group (referenced by ARN). FORCE-NEW on the provider name and on
# the backing ASG association.
###############################################################################
resource "aws_ecs_capacity_provider" "this" {
for_each = var.capacity_providers
name = each.key
auto_scaling_group_provider {
auto_scaling_group_arn = each.value.auto_scaling_group_arn
managed_draining = each.value.managed_draining
managed_termination_protection = each.value.managed_termination_protection
managed_scaling {
status = each.value.managed_scaling.status
target_capacity = each.value.managed_scaling.target_capacity
minimum_scaling_step_size = each.value.managed_scaling.minimum_scaling_step_size
maximum_scaling_step_size = each.value.managed_scaling.maximum_scaling_step_size
instance_warmup_period = each.value.managed_scaling.instance_warmup_period
}
}
tags = merge(var.tags, each.value.tags)
}
###############################################################################
# Capacity-provider association + default strategy
#
# Referencing the EC2 capacity provider resources (rather than the input map
# keys) makes this association depend on them, so it is applied only after the
# providers exist. Fargate providers are AWS-managed and need no resource.
###############################################################################
resource "aws_ecs_cluster_capacity_providers" "this" {
for_each = local.manage_capacity_providers ? { this = true }: {}
cluster_name = aws_ecs_cluster.this.name
capacity_providers = toset(concat(tolist(var.fargate_capacity_providers),
[for cp in aws_ecs_capacity_provider.this: cp.name],))
dynamic "default_capacity_provider_strategy" {
for_each = var.default_capacity_provider_strategy
content {
capacity_provider = default_capacity_provider_strategy.value.capacity_provider
base = default_capacity_provider_strategy.value.base
weight = default_capacity_provider_strategy.value.weight
}
}
}