-
Notifications
You must be signed in to change notification settings - Fork 5
/
iam.tf
243 lines (215 loc) · 6.87 KB
/
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
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
data "aws_iam_policy_document" "lambda_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "receiver_lambda_role" {
name = "ReceiverLambdaRole"
description = "Used by the controlshift-webhook-handler Lambda for receiving db replication data from ControlShift"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
}
data "aws_iam_policy_document" "receiver_execution_policy" {
# allow the lambda to write cloudwatch logs
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["arn:aws:logs:*:*:*"]
}
# allow the receiver lambda to enqueue work
statement {
effect = "Allow"
actions = ["sqs:SendMessage"]
resources = ["arn:aws:sqs:${var.aws_region}:*:${aws_sqs_queue.receiver_queue.name}",
"arn:aws:sqs:${var.aws_region}:*:${aws_sqs_queue.receiver_queue_glue.name}"]
}
}
resource "aws_iam_role_policy" "lambda_receiver" {
name = "AllowsReceiverExecution"
role = aws_iam_role.receiver_lambda_role.id
policy = data.aws_iam_policy_document.receiver_execution_policy.json
}
resource "aws_iam_role" "api_gateway_role" {
name = "APIGatewayRole"
description = "Used by the Controlshift API Gateway webhook endpoint for CloudWatch logging"
assume_role_policy = data.aws_iam_policy_document.gateway_assume_role.json
}
data "aws_iam_policy_document" "gateway_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["apigateway.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "gateway_cloudwatch_logging" {
role = aws_iam_role.api_gateway_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
}
resource "aws_iam_role" "loader_lambda_role" {
name_prefix = "LoaderLambdaRole"
description = "Used by the controlshift-redshift-loader Lambda for processing db replication data from ControlShift into Redshift"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
}
resource "aws_iam_role_policy" "lambda_loads_tables" {
name_prefix = "AllowsLoaderExecution"
role = aws_iam_role.loader_lambda_role.id
policy = data.aws_iam_policy_document.loader_execution_policy.json
}
data "aws_iam_policy_document" "loader_execution_policy" {
statement {
effect = "Allow"
actions = [
"dynamodb:DeleteItem",
"dynamodb:DescribeTable",
"dynamodb:GetItem",
"dynamodb:ListTables",
"dynamodb:PutItem",
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:UpdateItem"
]
resources = [
aws_dynamodb_table.loader_config.arn,
aws_dynamodb_table.batches.arn,
aws_dynamodb_table.processed_files.arn
]
}
# Apparently, the KMS resource needs to be *
statement {
effect = "Allow"
actions = [
"kms:DescribeKey",
"kms:GetKeyPolicy",
"kms:Decrypt"
]
resources = [
aws_kms_key.lambda_config.arn
]
}
statement {
effect = "Allow"
actions = [
"s3:Get*",
"s3:Put*",
"s3:List*",
]
resources = [
aws_s3_bucket.manifest.arn,
"${aws_s3_bucket.manifest.arn}/*"
]
}
statement {
effect = "Allow"
actions = ["s3:GetObject", "s3:ListBucket"]
resources = [
"arn:aws:s3:::agra-data-exports-${var.controlshift_environment}",
"arn:aws:s3:::agra-data-exports-${var.controlshift_environment}/*"]
}
statement {
effect = "Allow"
actions = [
"sns:GetEndpointAttributes",
"sns:GetSubscriptionAttributes",
"sns:GetTopicAttributes",
"sns:ListTopics",
"sns:Publish",
"sns:Subscribe",
"sns:Unsubscribe"
]
resources = [
aws_sns_topic.success_sns_topic.arn,
aws_sns_topic.failure_sns_topic.arn
]
}
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["arn:aws:logs:*:*:*"]
}
# allow lambda to be wired up to the queue. These are the minimum permissions for the SQS Lambda Executor.
statement {
effect = "Allow"
actions = ["sqs:ReceiveMessage", "sqs:DeleteMessage", "sqs:GetQueueAttributes"]
resources = ["arn:aws:sqs:${var.aws_region}:*:${aws_sqs_queue.receiver_queue.name}"]
}
statement {
effect = "Allow"
actions = ["ec2:CreateNetworkInterface", "ec2:DescribeNetworkInterfaces", "ec2:DeleteNetworkInterface"]
resources = ["*"]
}
}
data "aws_iam_policy_document" "run_glue_job_execution_policy" {
# allow the lambda to write cloudwatch logs
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["arn:aws:logs:*:*:*"]
}
# allow the lambda to enqueue work
statement {
effect = "Allow"
actions = [ "glue:StartJobRun" ]
resources = [ aws_glue_job.signatures_full.arn ]
}
}
resource "aws_iam_role_policy" "lambda_run_glue_job" {
name = "AllowsRunGlueJobExecution"
role = aws_iam_role.run_glue_job_lambda_role.id
policy = data.aws_iam_policy_document.run_glue_job_execution_policy.json
}
resource "aws_iam_role" "run_glue_job_lambda_role" {
name = "RunGlueJobLambdaRole"
description = "Used by the controlshift-run-glue-job Lambda for triggering AWS Glue job once the crawler finishes"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
}
data "aws_iam_policy_document" "run_glue_crawler_execution_policy" {
# allow the lambda to write cloudwatch logs
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["arn:aws:logs:*:*:*"]
}
# allow the lambda to enqueue work
statement {
effect = "Allow"
actions = [ "glue:StartCrawler" ]
resources = [ aws_glue_crawler.signatures_crawler.arn ]
}
# allow glue lambda to be wired up to the queue. These are the minimum permissions for the SQS Lambda Executor.
statement {
effect = "Allow"
actions = ["sqs:ReceiveMessage", "sqs:DeleteMessage", "sqs:GetQueueAttributes"]
resources = ["arn:aws:sqs:${var.aws_region}:*:${aws_sqs_queue.receiver_queue_glue.name}"]
}
}
resource "aws_iam_role_policy" "lambda_run_glue_crawler" {
name = "AllowsRunGlueCrawlerExecution"
role = aws_iam_role.run_glue_crawler_lambda_role.id
policy = data.aws_iam_policy_document.run_glue_crawler_execution_policy.json
}
resource "aws_iam_role" "run_glue_crawler_lambda_role" {
name = "RunGlueCrawlerLambdaRole"
description = "Used by the controlshift-run-glue-crawler Lambda for triggering AWS Glue crawler when a new signatures table full data export is available."
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
}