-
Notifications
You must be signed in to change notification settings - Fork 3
/
glue_iam.tf
148 lines (132 loc) · 3.46 KB
/
glue_iam.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
data "aws_iam_policy_document" "glue_assume_role_policy_document" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["glue.amazonaws.com"]
}
effect = "Allow"
}
statement {
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = [data.aws_caller_identity.current.account_id]
}
condition {
test = "Bool"
variable = "aws:MultiFactorAuthPresent"
values = ["true"]
}
}
}
resource "aws_iam_role" "glue_role" {
name = format("%s-%s-glue-role", var.project, var.environment)
description = format("%s-%s: Assume role policy for AWS Glue", var.project, var.environment)
assume_role_policy = data.aws_iam_policy_document.glue_assume_role_policy_document.json
tags = local.project_tags
}
resource "aws_iam_role_policy_attachment" "glue_role_att" {
role = aws_iam_role.glue_role.name
policy_arn = format("arn:%s:iam::aws:policy/service-role/AWSGlueServiceRole", data.aws_partition.current.partition)
}
data "aws_iam_policy_document" "glue_policy_document" {
statement {
sid = "ReadOnlyFromBuckets"
actions = [
"s3:GetBucketLocation",
"s3:GetBucketRequestPayment",
"s3:GetEncryptionConfiguration",
"s3:GetObject",
"s3:ListBucket",
]
effect = "Allow"
resources = flatten([for bucket in var.subscriber_buckets : [
format("arn:%s:s3:::%s", data.aws_partition.current.partition, bucket),
format("arn:%s:s3:::%s/*", data.aws_partition.current.partition, bucket),
]
])
}
statement {
sid = "AthenaGlueAccess"
actions = [
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:ListMultipartUploadParts",
"s3:ListAllMyBuckets",
"s3:AbortMultipartUpload",
"s3:CreateBucket",
"s3:PutObject",
"s3:DeleteObject",
]
effect = "Allow"
resources = [
module.athena_results.arn,
"${module.athena_results.arn}/*",
]
}
statement {
sid = "GlueCSVResults"
actions = [
"s3:*",
]
effect = "Allow"
resources = [
module.glue_tmp_bucket.arn,
"${module.glue_tmp_bucket.arn}/*",
aws_s3_bucket.csv_results.arn,
"${aws_s3_bucket.csv_results.arn}/*",
]
}
statement {
sid = "DecryptS3Files"
actions = [
"kms:Decrypt",
]
effect = "Allow"
resources = ["*"]
}
statement {
actions = [
"athena:*",
]
effect = "Allow"
resources = ["*"]
}
statement {
actions = [
"glue:*",
]
effect = "Allow"
resources = ["*"]
}
statement {
actions = [
"sns:ListTopics",
"sns:GetTopicAttributes",
]
effect = "Allow"
resources = ["*"]
}
statement {
actions = [
"cloudwatch:CreateLogGroup",
"cloudwatch:PutMetricAlarm",
"cloudwatch:DescribeAlarms",
"cloudwatch:DeleteAlarms",
]
effect = "Allow"
resources = ["*"]
}
}
resource "aws_iam_policy" "glue_policy" {
name = format("%s-%s-glue-policy", var.project, var.environment)
description = format("%s-%s: Allow AWS Glue to read from resources", var.project, var.environment)
policy = data.aws_iam_policy_document.glue_policy_document.json
}
resource "aws_iam_role_policy_attachment" "glue_policy_att" {
role = aws_iam_role.glue_role.name
policy_arn = aws_iam_policy.glue_policy.arn
}