-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
319 lines (280 loc) · 9.39 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
resource "tfe_agent_pool" "ecs_agent_pool" {
count = var.create_tfe_agent_pool ? 1 : 0
name = "${var.name}-agent-pool"
organization = var.hcp_terraform_org_name
organization_scoped = false # always explicitly specify the workspace or override by customer
}
resource "tfe_agent_token" "ecs_agent_token" {
count = var.create_tfe_agent_pool ? 1 : 0
agent_pool_id = tfe_agent_pool.ecs_agent_pool[0].id
description = "${var.name}-agent-token"
}
resource "aws_ssm_parameter" "agent_token" {
name = "/hcp-tf-token/${var.hcp_terraform_org_name}/${var.name}"
description = "HCP Terraform agent token"
type = "SecureString"
value = var.create_tfe_agent_pool ? tfe_agent_token.ecs_agent_token[0].token : var.tfe_agent_token
key_id = local.key_id
tags = var.tags
}
resource "aws_kms_key" "log_ssm_key" {
count = var.kms_key_arn == "" ? 1 : 0
description = "KMS key to encrypt the Log groups and SSM parameters"
deletion_window_in_days = 7
enable_key_rotation = true
policy = data.aws_iam_policy_document.kms_key_policy[0].json
}
data "aws_iam_policy_document" "kms_key_policy" {
count = var.kms_key_arn == "" ? 1 : 0
statement {
sid = "Allow access to the key"
effect = "Allow"
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
}
actions = [
"kms:*",
]
resources = [
"arn:aws:kms:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:key/*"
]
}
statement {
sid = "Allow CloudWatch logs and SSM access to the key"
effect = "Allow"
principals {
type = "Service"
identifiers = [
"logs.${data.aws_region.current.name}.amazonaws.com",
"ssm.amazonaws.com"
]
}
actions = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
]
resources = [
"arn:aws:kms:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:key/*"
]
}
}
locals {
key_id = var.kms_key_arn == "" ? aws_kms_key.log_ssm_key[0].arn : var.kms_key_arn
}
resource "aws_cloudwatch_log_group" "cloudwatch" {
count = var.create_cloudwatch_log_group ? 1 : 0
name = "/hcp/hcp-terraform-agents/ecs/${var.name}"
retention_in_days = var.cloudwatch_log_group_retention
kms_key_id = local.key_id
tags = var.tags
}
resource "aws_ecs_task_definition" "hcp_terraform_agent" {
family = "hcp-tf-agent-${var.hcp_terraform_org_name}-${var.name}"
cpu = var.agent_cpu
memory = var.agent_memory
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
task_role_arn = aws_iam_role.ecs_task_role.arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
runtime_platform {
operating_system_family = "LINUX"
}
container_definitions = jsonencode(
[
{
name : "hcp-terraform"
image : var.agent_image
essential : true
logConfiguration : {
logDriver : "awslogs",
options : {
awslogs-create-group : "true",
awslogs-group : var.create_cloudwatch_log_group ? aws_cloudwatch_log_group.cloudwatch[0].name : var.cloudwatch_log_group_name
awslogs-region : data.aws_region.current.name
awslogs-stream-prefix : "hcp-tf-${var.hcp_terraform_org_name}-${var.name}"
}
}
environment = concat([
{
name = "TFC_AGENT_SINGLE",
value = tostring(var.agent_single_execution)
},
{
name = "TFC_AGENT_NAME",
value = "hcp-tf-agent-${var.name}"
},
{
name = "TFC_ADDRESS",
value = var.hcp_terraform_address
},
{
name = "TFC_AGENT_LOG_LEVEL",
value = var.agent_log_level
},
{
name = "TFC_AGENT_AUTO_UPDATE",
value = var.agent_auto_update
}
], var.extra_env_vars),
secrets = [
{
name = "TFC_AGENT_TOKEN",
valueFrom = aws_ssm_parameter.agent_token.arn
}
]
}
]
)
tags = var.tags
}
resource "aws_ecs_service" "hcp_terraform_agent" {
name = "hcp-tf-agent-${var.name}"
cluster = var.create_ecs_cluster ? module.ecs_cluster[0].cluster_arn : var.ecs_cluster_arn
task_definition = aws_ecs_task_definition.hcp_terraform_agent.arn
desired_count = var.num_agents
propagate_tags = "SERVICE"
deployment_maximum_percent = "200"
deployment_minimum_healthy_percent = "33"
enable_ecs_managed_tags = "true"
capacity_provider_strategy {
capacity_provider = var.use_spot_instances ? "FARGATE_SPOT" : "FARGATE"
weight = "1"
}
network_configuration {
assign_public_ip = "false"
security_groups = [aws_security_group.hcp_terraform_agent.id]
subnets = var.subnet_ids
}
lifecycle {
postcondition {
condition = self.desired_count == var.num_agents
error_message = "The ECS service desired count is not equal to the number of agents specified in the configuration."
}
}
tags = merge(var.tags,
{
Name = "hcp-terraform-agent-${var.hcp_terraform_org_name}-${var.name}"
}
)
}
resource "aws_security_group" "hcp_terraform_agent" {
name_prefix = "hcp-tf-${var.hcp_terraform_org_name}-${var.name}-sg"
description = "Security group for HCP Terraform: ${var.name} in org ${var.hcp_terraform_org_name}"
vpc_id = var.vpc_id
lifecycle {
create_before_destroy = true
}
tags = var.tags
}
#tfsec:ignore:no-public-egress-sgr
resource "aws_security_group_rule" "allow_egress" {
protocol = "tcp"
type = "egress"
for_each = var.agent_egress_ports
from_port = each.value
to_port = each.value
cidr_blocks = var.agent_cidr_blocks
security_group_id = aws_security_group.hcp_terraform_agent.id
description = "Egress rule for HCP Terraform agent"
}
#####################################################################################
# ECS Cluster - Optional creation of an ECS cluster to run the HCP Terraform agent
#####################################################################################
module "ecs_cluster" {
count = var.create_ecs_cluster ? 1 : 0
source = "git::https://github.com/terraform-aws-modules/terraform-aws-ecs?ref=6b52c965734d95767d8e20d965afcd0db29dae5e" # v5.11.2
cluster_name = var.name
fargate_capacity_providers = {
FARGATE = {
default_capacity_provider_strategy = {
weight = 50
}
}
FARGATE_SPOT = {
default_capacity_provider_strategy = {
weight = 50
}
}
}
tags = merge(var.tags,
{
Name = var.name
}
)
}
#####################################################################################
# IAM
# Two roles are defined: the task execution role used during initialization,
# and the task role which is assumed by the container(s).
#####################################################################################
data "aws_iam_policy_document" "agent_assume_role_policy" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
identifiers = ["ecs-tasks.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_iam_role" "ecs_task_execution_role" {
name = "${var.name}-ecsTaskExecutionRole"
assume_role_policy = data.aws_iam_policy_document.agent_assume_role_policy.json
tags = merge(var.tags,
{
Name = "hcp-terraform-${var.hcp_terraform_org_name}-${var.name}-ecsTaskExecutionRole"
}
)
}
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy_attachment" {
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
data "aws_iam_policy_document" "ssm_access_policy" {
statement {
effect = "Allow"
actions = [
"kms:Decrypt"
]
resources = [
local.key_id
]
}
}
resource "aws_iam_role_policy" "ssm_access_policy" {
name = "ssm-access-policy"
role = aws_iam_role.ecs_task_execution_role.name
policy = data.aws_iam_policy_document.ssm_access_policy.json
}
data "aws_iam_policy_document" "agent_init_policy" {
statement {
effect = "Allow"
actions = ["ssm:GetParameters"]
resources = [aws_ssm_parameter.agent_token.arn]
}
}
resource "aws_iam_role_policy" "agent_init_policy" {
role = aws_iam_role.ecs_task_execution_role.name
name = "AccessSSMforAgentToken"
policy = data.aws_iam_policy_document.agent_init_policy.json
}
resource "aws_iam_role" "ecs_task_role" {
name = "${var.name}-ecsTaskRole"
assume_role_policy = data.aws_iam_policy_document.agent_assume_role_policy.json
tags = merge(var.tags,
{
Name = "hcp-terraform-${var.hcp_terraform_org_name}-${var.name}-ecsTaskRole"
}
)
}
resource "aws_iam_role_policy_attachment" "ecs_task_role_policy_attachment" {
for_each = toset(var.task_policy_arns)
role = aws_iam_role.ecs_task_role.name
policy_arn = each.key
}