-
Notifications
You must be signed in to change notification settings - Fork 28
/
iam.tf
162 lines (134 loc) · 3.5 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
##########################################
## Rancher server instance profile/role ##
##########################################
# Rancher server instance profile
resource "aws_iam_instance_profile" "rancher_server_instance_profile" {
name = "${var.server_name}-instance-profile"
roles = [
"${aws_iam_role.rancher_server_role.name}"
]
lifecycle {
create_before_destroy = true
}
}
# Rancher server role
resource "aws_iam_role" "rancher_server_role" {
name = "${var.server_name}-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
lifecycle {
create_before_destroy = true
}
}
#####################
## Custom policies ##
#####################
# S3 credentials bucket access
# Allows the server to read/write api keys to the S3 bucket.
resource "aws_iam_policy" "s3_server_credentials" {
name = "${var.server_name}-S3-credentials-access"
path = "/"
description = "Allow the Rancher server to access the credentials file in the S3 bucket."
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::${aws_s3_bucket.server_credentials_bucket.id}/keys.txt"
]
}
]
}
EOF
}
# SQS queue create & delete messages
# Allows the autoscaling hook app to receive and process messages
# published to the SQS queue.
resource "aws_iam_policy" "sqs_queue_access" {
name = "${var.server_name}-SQS-queue-access"
path = "/"
description = "Allow the lifecycle hook app to receive and process SQS queue messages."
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sqs:DeleteMessage",
"sqs:ReceiveMessage"
],
"Resource": [
"${aws_sqs_queue.autoscaling_hooks_queue.arn}"
]
}
]
}
EOF
}
# Autoscaling lifecycle complete action
resource "aws_iam_policy" "autoscaling_complete_lifecycle_action" {
name = "${var.server_name}-Allow-autoscaling-complete-action"
path = "/"
description = "Allow the lifecycle hook app send a complete action request that releases the instance for termination."
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"autoscaling:CompleteLifecycleAction"
],
"Resource": "*"
}
]
}
EOF
}
#############################
## Attach policies to role ##
#############################
# S3 bucket access
resource "aws_iam_policy_attachment" "rancher_server_s3_policy" {
name = "${var.server_name}_s3_policy"
policy_arn = "${aws_iam_policy.s3_server_credentials.arn}"
roles = [
"${aws_iam_role.rancher_server_role.name}"
]
}
# SQS access
resource "aws_iam_policy_attachment" "rancher_server_sqs_policy" {
name = "${var.server_name}_sqs_policy"
policy_arn = "${aws_iam_policy.sqs_queue_access.arn}"
roles = [
"${aws_iam_role.rancher_server_role.name}"
]
}
# Complete autoscaling hook
resource "aws_iam_policy_attachment" "complete_autoscaling_hooks" {
name = "${var.server_name}_complete_autoscaling_hooks"
policy_arn = "${aws_iam_policy.autoscaling_complete_lifecycle_action.arn}"
roles = [
"${aws_iam_role.rancher_server_role.name}"
]
}