-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.tf
206 lines (175 loc) · 5.8 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
locals {
secrets_json = var.use_ssm_parameter_store ? ({
"secrets" : [
{ "name" : "LaceworkAccessToken", "valueFrom" : local.ssm_parameter_arn }
]
}) : ({})
environment_json = {
"environment" : flatten([
(!var.use_ssm_parameter_store) ? ([{
"name" : "LaceworkAccessToken", "value" : var.lacework_access_token
}]) : ([]),
length(var.lacework_server_url) > 0 ? ([{
"name" : "LaceworkServerUrl", "value" : var.lacework_server_url
}]) : ([]),
])
}
container_definition_json = jsonencode([merge(
local.secrets_json,
local.environment_json,
{
"essential" : true,
"image" : var.lacework_datacollector_image,
"mountPoints" : [
{
"readOnly" : true,
"containerPath" : "/laceworkfim",
"sourceVolume" : "root"
},
{
"readOnly" : null,
"containerPath" : "/var/lib/lacework",
"sourceVolume" : "var_lib_lacework"
},
{
"readOnly" : null,
"containerPath" : "/var/log",
"sourceVolume" : "var_log"
},
{
"readOnly" : null,
"containerPath" : "/var/run",
"sourceVolume" : "var_run"
},
{
"readOnly" : true,
"containerPath" : "/etc/passwd",
"sourceVolume" : "etc_passwd"
},
{
"readOnly" : true,
"containerPath" : "/etc/group",
"sourceVolume" : "etc_group"
}
],
"name" : local.ecs_task_family_name,
"privileged" : true
}
)])
ecs_service_name = length(var.ecs_service_name) > 0 ? var.ecs_service_name : "${var.resource_prefix}-service-${random_id.uniq.hex}"
ecs_task_family_name = length(var.ecs_task_family_name) > 0 ? var.ecs_task_family_name : "${var.resource_prefix}-task-${random_id.uniq.hex}"
iam_role_arn = var.use_existing_iam_role ? var.iam_role_arn : aws_iam_role.ecs_execution[0].arn
iam_role_name = (!var.use_existing_iam_role && length(var.iam_role_name) > 0) ? var.iam_role_name : "${var.resource_prefix}-role-${random_id.uniq.hex}"
ssm_parameter_arn = var.use_ssm_parameter_store ? (length(var.ssm_parameter_arn) > 0 ? var.ssm_parameter_arn : aws_ssm_parameter.lacework_access_token[0].arn) : ""
version_file = "${abspath(path.module)}/VERSION"
module_name = "terraform-aws-ecs-agent"
module_version = fileexists(local.version_file) ? file(local.version_file) : ""
}
resource "random_id" "uniq" {
byte_length = 4
}
resource "aws_iam_role" "ecs_execution" {
count = var.use_existing_iam_role ? 0 : 1
name = local.iam_role_name
tags = var.iam_role_tags
assume_role_policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "ecs_task_execution_policy_attachment" {
count = var.use_existing_iam_role ? 0 : 1
role = aws_iam_role.ecs_execution[count.index].name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
data "aws_iam_policy_document" "ssm_parameter_store_policy" {
count = (!var.use_existing_iam_role && var.use_ssm_parameter_store) ? 1 : 0
version = "2012-10-17"
statement {
sid = "GetSsmParameter"
actions = ["ssm:GetParameters"]
resources = [local.ssm_parameter_arn]
}
dynamic "statement" {
for_each = var.ssm_parameter_encrypted ? [1] : []
content {
sid = "DecryptSsmParameter"
actions = ["kms:Decrypt"]
resources = [var.ssm_parameter_kms_arn]
}
}
}
resource "aws_iam_policy" "ssm_parameter_store_policy" {
count = (!var.use_existing_iam_role && var.use_ssm_parameter_store) ? 1 : 0
name = local.iam_role_name
description = "An IAM policy to allow a Lacework Agent ECS task to pull the agent access token from SSM"
policy = data.aws_iam_policy_document.ssm_parameter_store_policy[count.index].json
}
resource "aws_iam_role_policy_attachment" "ssm_parameter_store_iam_role_policy" {
count = (!var.use_existing_iam_role && var.use_ssm_parameter_store) ? 1 : 0
role = local.iam_role_name
policy_arn = aws_iam_policy.ssm_parameter_store_policy[count.index].arn
}
resource "aws_ecs_task_definition" "lacework_datacollector" {
family = local.ecs_task_family_name
container_definitions = local.container_definition_json
cpu = var.lacework_task_cpu
memory = var.lacework_task_mem
task_role_arn = local.iam_role_arn
execution_role_arn = local.iam_role_arn
network_mode = "host"
pid_mode = "host"
volume {
name = "root"
host_path = "/"
}
volume {
name = "var_lib_lacework"
host_path = "/var/lib/lacework"
}
volume {
name = "var_log"
host_path = "/var/log"
}
volume {
name = "var_run"
host_path = "/var/run"
}
volume {
name = "etc_passwd"
host_path = "/etc/passwd"
}
volume {
name = "etc_group"
host_path = "/etc/group"
}
}
resource "aws_ecs_service" "lacework_datacollector" {
name = local.ecs_service_name
cluster = var.ecs_cluster_arn
scheduling_strategy = "DAEMON"
launch_type = var.ecs_launch_type
task_definition = aws_ecs_task_definition.lacework_datacollector.arn
}
resource "aws_ssm_parameter" "lacework_access_token" {
count = (var.use_ssm_parameter_store && length(var.ssm_parameter_arn) == 0) ? 1 : 0
name = var.ssm_parameter_name
key_id = var.ssm_parameter_kms_arn
type = var.ssm_parameter_encrypted ? "SecureString" : "String"
value = var.lacework_access_token
}
data "lacework_metric_module" "lwmetrics" {
name = local.module_name
version = local.module_version
}