generated from aws-ia/terraform-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.tf
204 lines (165 loc) · 6.75 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
# ---------------------------------------------------------------------------------------------------------------------
# MWAA Environment
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_mwaa_environment" "mwaa" {
name = var.name
airflow_version = var.airflow_version
environment_class = var.environment_class
min_workers = var.min_workers
max_workers = var.max_workers
kms_key = var.kms_key
dag_s3_path = var.dag_s3_path
plugins_s3_object_version = var.plugins_s3_object_version
plugins_s3_path = var.plugins_s3_path
requirements_s3_path = var.requirements_s3_path
requirements_s3_object_version = var.requirements_s3_object_version
startup_script_s3_path = var.startup_script_s3_path
startup_script_s3_object_version = var.startup_script_s3_object_version
schedulers = var.schedulers
execution_role_arn = local.execution_role_arn
airflow_configuration_options = local.airflow_configuration_options
source_bucket_arn = local.source_bucket_arn
webserver_access_mode = var.webserver_access_mode
weekly_maintenance_window_start = var.weekly_maintenance_window_start
tags = var.tags
network_configuration {
security_group_ids = local.security_group_ids
subnet_ids = var.private_subnet_ids
}
logging_configuration {
dag_processing_logs {
enabled = try(var.logging_configuration.dag_processing_logs.enabled, true)
log_level = try(var.logging_configuration.dag_processing_logs.log_level, "DEBUG")
}
scheduler_logs {
enabled = try(var.logging_configuration.scheduler_logs.enabled, true)
log_level = try(var.logging_configuration.scheduler_logs.log_level, "INFO")
}
task_logs {
enabled = try(var.logging_configuration.task_logs.enabled, true)
log_level = try(var.logging_configuration.task_logs.log_level, "WARNING")
}
webserver_logs {
enabled = try(var.logging_configuration.webserver_logs.enabled, true)
log_level = try(var.logging_configuration.webserver_logs.log_level, "ERROR")
}
worker_logs {
enabled = try(var.logging_configuration.worker_logs.enabled, true)
log_level = try(var.logging_configuration.worker_logs.log_level, "CRITICAL")
}
}
lifecycle {
ignore_changes = [
plugins_s3_object_version,
requirements_s3_object_version
]
}
}
# ---------------------------------------------------------------------------------------------------------------------
# IAM Role
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_iam_role" "mwaa" {
count = var.create_iam_role ? 1 : 0
name = var.iam_role_name != null ? var.iam_role_name : null
name_prefix = var.iam_role_name != null ? null : "mwaa-executor"
description = "MWAA IAM Role"
assume_role_policy = data.aws_iam_policy_document.mwaa_assume.json
force_detach_policies = var.force_detach_policies
path = var.iam_role_path
permissions_boundary = var.iam_role_permissions_boundary
tags = var.tags
}
resource "aws_iam_role_policy" "mwaa" {
count = var.create_iam_role ? 1 : 0
name_prefix = "mwaa-executor"
role = aws_iam_role.mwaa[0].id
policy = data.aws_iam_policy_document.mwaa.json
}
resource "aws_iam_role_policy_attachment" "mwaa" {
for_each = local.iam_role_additional_policies
policy_arn = each.key
role = aws_iam_role.mwaa[0].id
}
# ---------------------------------------------------------------------------------------------------------------------
# MWAA S3 Bucket
# ---------------------------------------------------------------------------------------------------------------------
#tfsec:ignore:AWS017 tfsec:ignore:AWS002 tfsec:ignore:AWS077
resource "aws_s3_bucket" "mwaa" {
count = var.create_s3_bucket ? 1 : 0
bucket_prefix = var.source_bucket_name != null ? var.source_bucket_name : format("%s-%s-", "mwaa", data.aws_caller_identity.current.account_id)
tags = var.tags
}
resource "aws_s3_bucket_acl" "mwaa" {
count = var.create_s3_bucket ? 1 : 0
bucket = aws_s3_bucket.mwaa[0].id
acl = "private"
}
#tfsec:ignore:aws-s3-encryption-customer-key
resource "aws_s3_bucket_server_side_encryption_configuration" "mwaa" {
count = var.create_s3_bucket ? 1 : 0
bucket = aws_s3_bucket.mwaa[0].id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_versioning" "mwaa" {
count = var.create_s3_bucket ? 1 : 0
bucket = aws_s3_bucket.mwaa[0].id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_public_access_block" "mwaa" {
count = var.create_s3_bucket ? 1 : 0
bucket = aws_s3_bucket.mwaa[0].id
block_public_acls = true
block_public_policy = true
restrict_public_buckets = true
ignore_public_acls = true
}
# ---------------------------------------------------------------------------------------------------------------------
# MWAA Security Group
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_security_group" "mwaa" {
count = var.create_security_group ? 1 : 0
name_prefix = "mwaa-"
description = "Security group for MWAA environment"
vpc_id = var.vpc_id
lifecycle {
create_before_destroy = true
}
tags = var.tags
}
resource "aws_security_group_rule" "mwaa_sg_inbound" {
count = var.create_security_group ? 1 : 0
type = "ingress"
from_port = 0
to_port = 0
protocol = "all"
source_security_group_id = aws_security_group.mwaa[0].id
security_group_id = aws_security_group.mwaa[0].id
description = "Amazon MWAA inbound access"
}
resource "aws_security_group_rule" "mwaa_sg_inbound_vpn" {
count = var.create_security_group && length(var.source_cidr) > 0 ? 1 : 0
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.source_cidr
security_group_id = aws_security_group.mwaa[0].id
description = "VPN Access for Airflow UI"
}
#tfsec:ignore:aws-vpc-no-public-egress-sgr
resource "aws_security_group_rule" "mwaa_sg_outbound" {
count = var.create_security_group ? 1 : 0
type = "egress"
from_port = 0
to_port = 0
protocol = "all"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.mwaa[0].id
description = "Amazon MWAA outbound access"
}