-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiam.tf
226 lines (202 loc) · 6.64 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
# IAM Role for ECS Task
resource "aws_iam_role" "ecs_task_execution_role" {
name = "ecs_task_execution_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# This block creates an inline IAM role policy named "ecs_task_execution_role_policy" for the role specified
resource "aws_iam_role_policy" "ecs_task_execution_role_policy" {
name = "ecs_task_execution_role_policy"
role = aws_iam_role.ecs_task_execution_role.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = "*"
}
]
})
}
# This block fetches information about the AWS account, including the account ID
data "aws_caller_identity" "current" {}
# This block creates a managed IAM policy called "parameter_store_access"
resource "aws_iam_policy" "parameter_store_access" {
name = "parameter_store_access"
description = "Policy to allow ECS tasks to access specific parameters in Parameter Store"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:GetParametersByPath",
"ssm:GetParameters",
"ssm:GetParameter"
],
"Resource": [
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/DJANGO_SECRET_KEY",
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/TECHRONOMICON_ACCESS_KEY_ID",
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/TECHRONOMICON_RDS_DB_NAME",
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/TECHRONOMICON_RDS_HOST",
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/TECHRONOMICON_RDS_PASSWORD",
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/TECHRONOMICON_RDS_USERNAME",
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/TECHRONOMICON_SECRET_ACCESS_KEY",
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/TECHRONOMICON_STORAGE_BUCKET_NAME",
"arn:aws:ssm:eu-west-1:${data.aws_caller_identity.current.account_id}:parameter/TECHRONOMICON_IP"
]
}
]
}
EOF
}
# Attach the IAM policy to the role
resource "aws_iam_role_policy_attachment" "ecs_role_parameter_store_access" {
role = aws_iam_role.ecs_role.name
policy_arn = aws_iam_policy.parameter_store_access.arn
}
# Create copy to copy files from the cloudwatch-logs-config bucket
resource "aws_iam_policy" "cloudwatch_logs_config_copy_policy" {
name = "cloudwatch_logs_config_s3_copy"
description = "IAM policy to allow copying files from the cloudwatch-logs-config bucket"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"s3:GetObject",
"s3:ListBucket",
"s3:CopyObject"
]
Resource = [
"arn:aws:s3:::${var.CLOUDWATCH_LOGS_CONFIG_BUCKET_NAME}",
"arn:aws:s3:::${var.CLOUDWATCH_LOGS_CONFIG_BUCKET_NAME}/*"
]
}
]
})
}
# Attach S3CopyPolicy to the role
resource "aws_iam_role_policy_attachment" "ecs_role_cloudwatch_logs_config_copy_policy" {
role = aws_iam_role.ecs_role.name
policy_arn = aws_iam_policy.cloudwatch_logs_config_copy_policy.arn
}
# Create IAM role
resource "aws_iam_role" "ecs_role" {
name = "ecs_role"
assume_role_policy = jsonencode({
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
}]
Version = "2012-10-17"
})
}
# Attach AmazonEC2ContainerServiceforEC2Role managed policy
resource "aws_iam_role_policy_attachment" "ecs_policy_attachment" {
role = aws_iam_role.ecs_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
# Create policy for EC2 to interact with Cloudwatch logs
resource "aws_iam_policy" "cwlogs_policy" {
name = "CWLogsPolicy"
description = "A policy that allows sufficient permissions for CloudWatch logs and metric data."
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams",
"logs:PutRetentionPolicy"
],
Resource = "*",
Effect = "Allow"
},
{
Action = "cloudwatch:PutMetricData",
Resource = "*",
Effect = "Allow"
}
]
})
}
# Attach EC2 Cloudwatch policy to role
resource "aws_iam_role_policy_attachment" "cwlogs_policy_attachment" {
role = aws_iam_role.ecs_role.name
policy_arn = aws_iam_policy.cwlogs_policy.arn
}
# Attach SSM policy to role
resource "aws_iam_role_policy_attachment" "ssm_policy_attachment" {
role = aws_iam_role.ecs_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
# Create IAM instance profile
resource "aws_iam_instance_profile" "ecs_instance_profile" {
name = "ecs_instance_profile"
role = aws_iam_role.ecs_role.name
}
# Setup Github as an identity provider
resource "aws_iam_openid_connect_provider" "default" {
url = "https://token.actions.githubusercontent.com"
client_id_list = [
"sts.amazonaws.com",
]
thumbprint_list = ["d89e3bd43d5d909b47a18977aa9d5ce36cee184c"]
}
# Create role for Github workflow to assume
resource "aws_iam_role" "github-actions-role" {
name = "github-actions-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Federated = aws_iam_openid_connect_provider.default.arn
}
Action = "sts:AssumeRoleWithWebIdentity"
Condition = {
StringEquals = {
"token.actions.githubusercontent.com:aud" = "sts.amazonaws.com",
},
StringLike = {
"token.actions.githubusercontent.com:sub" = [
"repo:lukejcollins/techronomicon-infra:*",
"repo:lukejcollins/techronomicon-ansible:*"
]
}
}
}
]
})
}
# Attach AdministratorAccess policy to Github Actions role
resource "aws_iam_role_policy_attachment" "github-actions-role-attachement" {
role = aws_iam_role.github-actions-role.name
policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
}