-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathiam.tf
173 lines (141 loc) · 3.98 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
# --------------------------------------------------------------------------
# EC2 Instance Profile
# --------------------------------------------------------------------------
resource "aws_iam_role" "jitsi" {
name = var.name
assume_role_policy = data.aws_iam_policy_document.tr.json
force_detach_policies = true
tags = var.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_iam_instance_profile" "jitsi" {
name_prefix = "${var.name}-"
role = aws_iam_role.jitsi.name
}
resource "aws_iam_role_policy_attachment" "attach_AmazonSSMManagedInstanceCore" {
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
role = aws_iam_role.jitsi.name
}
resource "aws_iam_role_policy_attachment" "attach_CloudWatchAgentServerPolicy" {
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
role = aws_iam_role.jitsi.name
}
# IAM Intance Profile Trust Relationship: EC2, SSM
data "aws_iam_policy_document" "tr" {
statement {
sid = "Ec2Ssm"
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${local.account_id}:root"]
}
principals {
type = "Service"
identifiers = [
"ec2.amazonaws.com",
"ssm.amazonaws.com"
]
}
}
}
# Jitsi Policy document
data "aws_iam_policy_document" "jitsi" {
statement {
sid = "Route53"
actions = [
"route53:ListHostedZones",
]
resources = ["*"]
}
statement {
sid = "EIPsAndTags"
actions = [
"ec2:AllocateAddress",
"ec2:AssociateAddress",
"ec2:CreateTags",
"ec2:DescribeAddresses",
"ec2:DisassociateAddress",
"ec2:ReleaseAddress"
]
resources = ["*"]
}
statement {
sid = "UpdateRoute53"
actions = ["route53:ChangeResourceRecordSets"]
resources = [
"arn:aws:route53:::hostedzone/${var.private_zone_id}",
"arn:aws:route53:::hostedzone/${var.public_zone_id}"
]
}
}
resource "aws_iam_policy" "jitsi" {
policy = data.aws_iam_policy_document.jitsi.json
name = "${var.name}-jitsi"
}
resource "aws_iam_role_policy_attachment" "attach-jitsi" {
policy_arn = aws_iam_policy.jitsi.arn
role = aws_iam_role.jitsi.name
}
# Cross-Account
data "aws_iam_policy_document" "assume" {
statement {
sid = "CrossAccountAssume"
actions = ["sts:AssumeRole"]
resources = [var.arn_role]
}
}
resource "aws_iam_policy" "assume" {
count = var.enable_cross_account == "1" ? 1 : 0
name = "${var.name}-cross-account-assume"
policy = data.aws_iam_policy_document.assume.json
}
resource "aws_iam_role_policy_attachment" "attach_assume" {
count = var.enable_cross_account == "1" ? 1 : 0
policy_arn = aws_iam_policy.assume[0].arn
role = aws_iam_role.jitsi.name
}
# --------------------------------------------------------------------------
# OPTIONAL: DLM Lifecycle Manager Policy for automatic EBS Snapshots
# --------------------------------------------------------------------------
# Trust Relationship
data "aws_iam_policy_document" "dlm_tr" {
count = var.enable_dlm == true ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
identifiers = ["dlm.amazonaws.com"]
type = "Service"
}
}
}
# IAM Role
resource "aws_iam_role" "dlm_lifecycle_role" {
count = var.enable_dlm == true ? 1 : 0
name = "${var.name}-dlm"
assume_role_policy = data.aws_iam_policy_document.dlm_tr[0].json
tags = local.tags
}
# IAM Policy
data "aws_iam_policy_document" "dlm" {
count = var.enable_dlm == true ? 1 : 0
statement {
actions = [
"ec2:CreateSnapshot",
"ec2:DeleteSnapshot",
"ec2:DescribeVolumes",
"ec2:DescribeSnapshots"
]
resources = ["*"]
}
statement {
actions = ["ec2:CreateTags"]
resources = ["arn:aws:ec2:*::snapshot/*"]
}
}
resource "aws_iam_role_policy" "dlm_lifecycle" {
name = "${var.name}-dlm"
role = aws_iam_role.dlm_lifecycle_role[0].id
policy = data.aws_iam_policy_document.dlm[0].json
}