generated from opszero/terraform-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
236 lines (211 loc) · 4.95 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
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
locals {
tags = merge(var.tags, {
Name = var.name
})
}
resource "aws_mwaa_environment" "this" {
name = var.name
environment_class = var.environment_class
execution_role_arn = aws_iam_role.this.arn
network_configuration {
security_group_ids = var.security_group_ids
subnet_ids = var.subnet_ids
}
source_bucket_arn = aws_s3_bucket.this.arn
dag_s3_path = var.dags_path
requirements_s3_path = var.requirements_s3_path
plugins_s3_path = var.plugins_s3_path
# logging_configuration {
# dag_processing_logs {
# enabled = true
# log_level = "DEBUG"
# }
# scheduler_logs {
# enabled = true
# log_level = "INFO"
# }
# task_logs {
# enabled = true
# log_level = "WARNING"
# }
# webserver_logs {
# enabled = true
# log_level = "ERROR"
# }
# worker_logs {
# enabled = true
# log_level = "CRITICAL"
# }
# }
tags = local.tags
depends_on = [
aws_iam_role.this,
aws_iam_role_policy.this,
aws_iam_role_policy_attachment.this,
aws_s3_bucket_public_access_block.this,
]
}
resource "aws_s3_bucket" "this" {
bucket = var.bucket_name != "" ? var.bucket_name : "${var.name}-airflow"
tags = local.tags
}
resource "aws_s3_bucket_public_access_block" "this" {
bucket = aws_s3_bucket.this.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_iam_role" "this" {
name = "${var.name}-airflow"
assume_role_policy = data.aws_iam_policy_document.assume.json
tags = local.tags
}
data "aws_iam_policy_document" "assume" {
version = "2012-10-17"
statement {
effect = "Allow"
principals {
identifiers = [
"airflow-env.amazonaws.com",
"airflow.amazonaws.com"
]
type = "Service"
}
actions = [
"sts:AssumeRole"
]
}
}
resource "aws_iam_role_policy_attachment" "this" {
count = length(var.iam_policy_arns)
role = aws_iam_role.this.name
policy_arn = var.iam_policy_arns[count.index]
}
resource "aws_iam_role_policy" "this" {
name = "${var.name}-airflow-execution-base"
policy = data.aws_iam_policy_document.this.json
role = aws_iam_role.this.id
}
data "aws_iam_policy_document" "this" {
source_policy_documents = [
data.aws_iam_policy_document.base.json,
]
}
data "aws_iam_policy_document" "base" {
version = "2012-10-17"
statement {
effect = "Allow"
actions = [
"airflow:PublishMetrics"
]
resources = [
"arn:aws:airflow:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:environment/${var.name}"
]
}
statement {
effect = "Deny"
actions = ["s3:ListAllMyBuckets"]
resources = [
aws_s3_bucket.this.arn,
"${aws_s3_bucket.this.arn}/*",
]
}
statement {
effect = "Allow"
actions = [
"s3:GetObject*",
"s3:GetBucket*",
"s3:List*"
]
resources = [
aws_s3_bucket.this.arn,
"${aws_s3_bucket.this.arn}/*",
]
}
statement {
effect = "Allow"
actions = [
"s3:GetAccountPublicAccessBlock"
]
resources = ["*"]
}
statement {
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:PutLogEvents",
"logs:GetLogEvents",
"logs:GetLogRecord",
"logs:GetLogGroupFields",
"logs:GetQueryResults"
]
resources = [
"arn:aws:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:airflow-${var.name}-*"
]
}
statement {
effect = "Allow"
actions = [
"logs:DescribeLogGroups"
]
resources = [
"*"
]
}
statement {
effect = "Allow"
actions = [
"s3:GetAccountPublicAccessBlock"
]
resources = [
"*"
]
}
statement {
effect = "Allow"
actions = [
"cloudwatch:PutMetricData"
]
resources = [
"*"
]
}
statement {
effect = "Allow"
actions = [
"sqs:ChangeMessageVisibility",
"sqs:DeleteMessage",
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl",
"sqs:ReceiveMessage",
"sqs:SendMessage"
]
resources = [
"arn:aws:sqs:${data.aws_region.current.name}:*:airflow-celery-*"
]
}
statement {
effect = "Allow"
actions = [
"kms:Decrypt",
"kms:DescribeKey",
"kms:GenerateDataKey*",
"kms:Encrypt"
]
resources = [
"arn:aws:kms:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:key/*"
]
condition {
test = "StringLike"
variable = "kms:ViaService"
values = [
"sqs.${data.aws_region.current.name}.amazonaws.com",
"s3.${data.aws_region.current.name}.amazonaws.com"
]
}
}
}