-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiam_roles.tf
148 lines (131 loc) · 3.84 KB
/
iam_roles.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
# IAM roles to allow Lambda functions to access different AWS resources.
# Fetch our own account id and region. Used in our IAM policy templates.
data "aws_caller_identity" "current" {}
# Template for our 'notify' lambda IAM policy
data "template_file" "iam_lambda_notify" {
template = "${file("./files/iam_lambda_notify.tpl")}"
vars {
account_id = "${data.aws_caller_identity.current.account_id}"
region = "${var.region}"
}
}
# Template for our 'read_instances' lambda IAM policy
data "template_file" "iam_lambda_read_instances" {
template = "${file("./files/iam_lambda_read_instances.tpl")}"
vars {
account_id = "${data.aws_caller_identity.current.account_id}"
region = "${var.region}"
}
}
# Template for our 'stop_and_terminate_instances' lambda IAM policy
data "template_file" "iam_lambda_stop_and_terminate_instances" {
template = "${file("./files/iam_lambda_stop_and_terminate_instances.tpl")}"
vars {
account_id = "${data.aws_caller_identity.current.account_id}"
region = "${var.region}"
}
}
# Template for our 'terminate_asgs' lambda IAM policy
data "template_file" "iam_lambda_terminate_asgs" {
template = "${file("./files/iam_lambda_terminate_asgs.tpl")}"
vars {
account_id = "${data.aws_caller_identity.current.account_id}"
region = "${var.region}"
}
}
# Role for our 'notify' lambda to assume
# This role is allowed to use the data collector lambda functions.
resource "aws_iam_role" "lambda_notify" {
name = "lambda_notify"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
]
}
EOF
}
# Role for our 'read_instances' lambda to assume
# Used by data collectors to gather ec2 instance data.
resource "aws_iam_role" "lambda_read_instances" {
name = "lambda_read_instances"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
]
}
EOF
}
# Role for our 'stop_and_terminate_instances' lambda to assume.
# This is used by lambdas that manage instance lifecycles.
resource "aws_iam_role" "lambda_stop_and_terminate_instances" {
name = "lambda_stop_and_terminate_instances"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
]
}
EOF
}
# Role for our 'terminate_asgs' lambda to assume.
resource "aws_iam_role" "lambda_terminate_asgs" {
name = "lambda_terminate_asgs"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
]
}
EOF
}
# Here we ingest the template and create the role policies
resource "aws_iam_role_policy" "lambda_notify_policy" {
name = "lambda_notify_policy"
policy = "${data.template_file.iam_lambda_notify.rendered}"
role = "${aws_iam_role.lambda_notify.id}"
}
resource "aws_iam_role_policy" "lambda_read_instances_policy" {
name = "lambda_read_instances_policy"
policy = "${data.template_file.iam_lambda_read_instances.rendered}"
role = "${aws_iam_role.lambda_read_instances.id}"
}
resource "aws_iam_role_policy" "lambda_stop_and_terminate_instances" {
name = "lambda_stop_and_terminate_instances"
policy = "${data.template_file.iam_lambda_stop_and_terminate_instances.rendered}"
role = "${aws_iam_role.lambda_stop_and_terminate_instances.id}"
}
resource "aws_iam_role_policy" "lambda_terminate_asgs" {
name = "lambda_terminate_asgs"
policy = "${data.template_file.iam_lambda_terminate_asgs.rendered}"
role = "${aws_iam_role.lambda_terminate_asgs.id}"
}