-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam.tf
203 lines (171 loc) · 5.8 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
# -----------------------------------
# IAM Policy for ECS Task Assume Role
# -----------------------------------
# ECS Task Assume Role Policy Document
data "aws_iam_policy_document" "ecs_task_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
# ECS Task Execution Role
resource "aws_iam_role" "ecs_task_execution_role" {
name = "ecs_task_execution_role"
assume_role_policy = data.aws_iam_policy_document.ecs_task_assume_role_policy.json
}
# Attach ECS Task Execution Policy to Execution Role
resource "aws_iam_role_policy_attachment" "ecs_task_execution_policy" {
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
# Conditional Policy for Cross-Account Access to ECR (for non-prod environments)
resource "aws_iam_role_policy" "ecr_cross_account_access" {
count = local.is_prod ? 0 : 1
role = aws_iam_role.ecs_task_execution_role.name
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability"
],
"Resource": "arn:aws:ecr:${data.aws_region.current.name}:${var.account_ids["prod"]}:repository/*"
}
]
}
EOF
}
# Attach ECR Read-Only Policy for Production
resource "aws_iam_role_policy_attachment" "ecr_read_access" {
count = local.is_prod ? 1 : 0
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
}
# -----------------------------------
# IAM Role for ECS Tasks
# -----------------------------------
# ECS Task Role
resource "aws_iam_role" "ecs_task_role" {
name = "ecs_task_role"
assume_role_policy = data.aws_iam_policy_document.ecs_task_assume_role_policy.json
}
# -----------------------------------
# OpenSearch Policies for ECS Tasks
# -----------------------------------
# OpenSearch Policy Document for ECS Tasks
data "aws_iam_policy_document" "ecs_task_opensearch_policy" {
statement {
actions = ["es:ESHttp*"]
resources = ["${aws_opensearch_domain.opensearch_domain.arn}/*"]
}
}
# OpenSearch Policy for ECS Tasks
resource "aws_iam_policy" "ecs_task_opensearch_policy" {
name = "ecs_task_opensearch_policy"
policy = data.aws_iam_policy_document.ecs_task_opensearch_policy.json
}
# Attach OpenSearch Policy to ECS Task Role
resource "aws_iam_role_policy_attachment" "ecs_task_opensearch_policy_attachment" {
role = aws_iam_role.ecs_task_role.name
policy_arn = aws_iam_policy.ecs_task_opensearch_policy.arn
}
# -----------------------------------
# IAM Role and Policies for Grafana
# -----------------------------------
# Grafana Assume Role Policy Document
data "aws_iam_policy_document" "grafana_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["grafana.amazonaws.com"]
}
}
}
# IAM Role for Grafana Workspace
resource "aws_iam_role" "grafana_workspace_role" {
name = "grafana-assume"
assume_role_policy = data.aws_iam_policy_document.grafana_assume_role_policy.json
}
# OpenSearch Policy Document for Grafana
data "aws_iam_policy_document" "grafana_opensearch_policy" {
statement {
actions = ["es:ESHttp*"]
resources = ["${aws_opensearch_domain.opensearch_domain.arn}/*"]
}
}
# OpenSearch Policy for Grafana
resource "aws_iam_policy" "grafana_opensearch_policy" {
name = "grafana-opensearch-policy"
policy = data.aws_iam_policy_document.grafana_opensearch_policy.json
}
# Attach OpenSearch Policy to Grafana Role
resource "aws_iam_role_policy_attachment" "grafana_opensearch_policy_attach" {
role = aws_iam_role.grafana_workspace_role.name
policy_arn = aws_iam_policy.grafana_opensearch_policy.arn
}
# SNS Publish Policy Document for Grafana
data "aws_iam_policy_document" "grafana_sns_publish_policy" {
statement {
actions = ["sns:Publish", "sns:GetTopicAttributes"]
resources = [aws_sns_topic.grafana_alerts.arn]
}
}
# SNS Publish Policy for Grafana
resource "aws_iam_policy" "grafana_sns_publish_policy" {
name = "grafana-sns-publish-policy"
policy = data.aws_iam_policy_document.grafana_sns_publish_policy.json
}
# Attach SNS Publish Policy to Grafana Role
resource "aws_iam_role_policy_attachment" "grafana_sns_publish_policy_attach" {
role = aws_iam_role.grafana_workspace_role.name
policy_arn = aws_iam_policy.grafana_sns_publish_policy.arn
}
# -----------------------------------
# OpenSearch Access Policies
# -----------------------------------
# OpenSearch Domain Access Policy
data "aws_iam_policy_document" "opensearch_policy" {
statement {
effect = "Allow"
principals {
type = "*"
identifiers = ["*"]
}
actions = ["es:*"]
resources = [
"arn:aws:es:${data.aws_region.current.name}:${local.account_id}:domain/${local.opensearch.domain_name}/*"
]
}
}
# -----------------------------------
# CloudWatch Log Policy for OpenSearch
# -----------------------------------
# Policy Document for OpenSearch Logging in CloudWatch
data "aws_iam_policy_document" "opensearch_log_policy" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["es.amazonaws.com"]
}
actions = [
"logs:PutLogEvents",
"logs:PutLogEventsBatch",
"logs:CreateLogStream",
]
resources = ["arn:aws:logs:*"]
}
}
# Attach CloudWatch Log Policy for OpenSearch
resource "aws_cloudwatch_log_resource_policy" "opensearch_log_policy" {
policy_name = "opensearch-logging-policy"
policy_document = data.aws_iam_policy_document.opensearch_log_policy.json
}