-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
175 lines (140 loc) · 5.96 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
locals {
volumes = concat(var.docker_volumes, var.efs_volumes)
}
resource "aws_ecs_task_definition" "this" {
family = "taskdef-${var.name}"
container_definitions = var.container_definitions
requires_compatibilities = [var.launch_type]
cpu = var.launch_type == "FARGATE" ? var.task_cpu : null
memory = var.launch_type == "FARGATE" ? var.task_memory : null
#checkov:skip=CKV_AWS_249:"Ensure that the Execution Role ARN and the Task Role ARN are different in ECS Task definitions"
execution_role_arn = var.execution_role_arn
task_role_arn = var.task_role_arn
network_mode = var.network_mode
dynamic "volume" {
for_each = local.volumes
content {
host_path = lookup(volume.value, "host_path", null)
name = volume.value.name
dynamic "docker_volume_configuration" {
for_each = lookup(volume.value, "docker_volume_configuration", [])
content {
autoprovision = lookup(docker_volume_configuration.value, "autoprovision", null)
driver = lookup(docker_volume_configuration.value, "driver", null)
driver_opts = lookup(docker_volume_configuration.value, "driver_opts", null)
labels = lookup(docker_volume_configuration.value, "labels", null)
scope = lookup(docker_volume_configuration.value, "scope", null)
}
}
dynamic "efs_volume_configuration" {
for_each = lookup(volume.value, "efs_volume_configuration", [])
content {
file_system_id = lookup(efs_volume_configuration.value, "file_system_id", null)
root_directory = lookup(efs_volume_configuration.value, "root_directory", null)
#checkov:skip=CKV_AWS_97:"Ensure Encryption in transit is enabled for EFS volumes in ECS Task definitions"
transit_encryption = lookup(efs_volume_configuration.value, "transit_encryption", "ENABLED")
transit_encryption_port = lookup(efs_volume_configuration.value, "transit_encryption_port", 2999)
dynamic "authorization_config" {
for_each = lookup(efs_volume_configuration.value, "authorization_config", [])
content {
access_point_id = lookup(authorization_config.value, "access_point_id", null)
iam = lookup(authorization_config.value, "iam", null)
}
}
}
}
}
}
dynamic "placement_constraints" {
for_each = var.task_placement_constraints
content {
type = placement_constraints.value.type
expression = lookup(placement_constraints.value, "expression", null)
}
}
tags = var.tags
}
# Create a data source to pull the latest active revision from
data "aws_ecs_task_definition" "this" {
task_definition = aws_ecs_task_definition.this.family
depends_on = [aws_ecs_task_definition.this] # ensures at least one task def exists
}
resource "aws_ecs_service" "this" {
#checkov:skip=CKV_AWS_332: Already defaulting to latest FARGATE platform version
name = var.name
cluster = var.cluster_id
# Use latest active revision
task_definition = "${aws_ecs_task_definition.this.family}:${max(
aws_ecs_task_definition.this.revision,
data.aws_ecs_task_definition.this.revision,
)}"
launch_type = var.launch_type
platform_version = var.launch_type == "FARGATE" ? var.platform_version : null
desired_count = var.desired_count
enable_ecs_managed_tags = var.enable_ecs_managed_tags
enable_execute_command = var.enable_execute_command
propagate_tags = var.propagate_tags
health_check_grace_period_seconds = var.health_check_grace_period_seconds
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
deployment_controller {
type = var.deployment_controller_type
}
dynamic "deployment_circuit_breaker" {
for_each = var.deployment_circuit_breaker.enable ? [var.deployment_circuit_breaker] : []
content {
enable = deployment_circuit_breaker.value.enable
rollback = deployment_circuit_breaker.value.rollback
}
}
dynamic "service_registries" {
for_each = var.service_registries
content {
registry_arn = service_registries.value.registry_arn
port = lookup(service_registries.value, "port", null)
container_name = lookup(service_registries.value, "container_name", null)
container_port = lookup(service_registries.value, "container_port", null)
}
}
dynamic "ordered_placement_strategy" {
for_each = var.ordered_placement_strategy
content {
type = ordered_placement_strategy.value.type
field = lookup(ordered_placement_strategy.value, "field", null)
}
}
dynamic "placement_constraints" {
for_each = var.service_placement_constraints
content {
type = placement_constraints.value.type
expression = lookup(placement_constraints.value, "expression", null)
}
}
dynamic "load_balancer" {
for_each = var.ecs_load_balancers
content {
container_name = load_balancer.value.container_name
container_port = load_balancer.value.container_port
elb_name = lookup(load_balancer.value, "elb_name", null)
target_group_arn = lookup(load_balancer.value, "target_group_arn", null)
}
}
dynamic "network_configuration" {
for_each = var.network_mode == "awsvpc" ? [1] : []
content {
subnets = var.subnets
security_groups = var.security_groups
assign_public_ip = var.assign_public_ip
}
}
dynamic "capacity_provider_strategy" {
for_each = var.capacity_provider_strategy != null ? var.capacity_provider_strategy : []
iterator = strategy
content {
capacity_provider = strategy.value["capacity_provider"]
weight = lookup(strategy.value, "weight", null)
base = lookup(strategy.value, "base", null)
}
}
tags = var.tags
}