-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.tf
152 lines (133 loc) · 3.37 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
resource "aws_iam_instance_profile" "ecs_profile" {
name_prefix = replace(
format("%.102s", replace("tf-ECSProfile-${var.name}-", "_", "-")),
"/\\s/",
"-",
)
role = aws_iam_role.ecs_role.name
path = var.iam_path
}
resource "aws_iam_role" "ecs_role" {
name_prefix = replace(
format("%.32s", replace("tf-ECSInRole-${var.name}-", "_", "-")),
"/\\s/",
"-",
)
path = var.iam_path
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": ["ecs.amazonaws.com", "ec2.amazonaws.com"]
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# It may be useful to add the following for troubleshooting the InstanceStatus
# Health check if using the fitnesskeeper/consul docker image
# "ec2:Describe*",
# "autoscaling:Describe*",
resource "aws_iam_policy" "ecs_policy" {
name_prefix = replace(
format("%.102s", replace("tf-ECSInPol-${var.name}-", "_", "-")),
"/\\s/",
"-",
)
description = "A terraform created policy for ECS"
path = var.iam_path
count = length(var.custom_iam_policy) > 0 ? 0 : 1
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecs:CreateCluster",
"ecs:DeregisterContainerInstance",
"ecs:DiscoverPollEndpoint",
"ecs:Poll",
"ecs:RegisterContainerInstance",
"ecs:StartTelemetrySession",
"ecs:Submit*",
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_policy" "custom_ecs_policy" {
name_prefix = replace(
format("%.102s", replace("tf-ECSInPol-${var.name}-", "_", "-")),
"/\\s/",
"-",
)
description = "A terraform created policy for ECS"
path = var.iam_path
count = length(var.custom_iam_policy) > 0 ? 1 : 0
policy = var.custom_iam_policy
}
resource "aws_iam_policy_attachment" "attach_ecs" {
name = "ecs-attachment"
roles = [aws_iam_role.ecs_role.name]
policy_arn = element(
concat(
aws_iam_policy.ecs_policy.*.arn,
aws_iam_policy.custom_ecs_policy.*.arn,
),
0,
)
}
# IAM Resources for Consul and Registrator Agents
data "aws_iam_policy_document" "consul_task_policy" {
statement {
actions = [
"ec2:Describe*",
"autoscaling:Describe*",
]
resources = ["*"]
}
}
data "aws_iam_policy_document" "assume_role_consul_task" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "consul_task" {
count = var.enable_agents ? 1 : 0
name_prefix = replace(
format("%.32s", replace("tf-agentTaskRole-${var.name}-", "_", "-")),
"/\\s/",
"-",
)
path = var.iam_path
assume_role_policy = data.aws_iam_policy_document.assume_role_consul_task.json
}
resource "aws_iam_role_policy" "consul_ecs_task" {
count = var.enable_agents ? 1 : 0
name_prefix = replace(
format("%.102s", replace("tf-agentTaskPol-${var.name}-", "_", "-")),
"/\\s/",
"-",
)
role = aws_iam_role.consul_task[0].id
policy = data.aws_iam_policy_document.consul_task_policy.json
}