-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.tf
65 lines (56 loc) · 1.99 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
data "aws_iam_policy_document" "ecs_tasks_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "execution_role" {
name = "${var.service_name}-execution-role"
assume_role_policy = data.aws_iam_policy_document.ecs_tasks_assume_role_policy.json
}
resource "aws_iam_role_policy_attachment" "execution_role_policy" {
role = aws_iam_role.execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
resource "aws_iam_role_policy_attachment" "execution_role_additional_policies" {
count = length(var.execution_role_policy_arns)
role = aws_iam_role.execution_role.name
policy_arn = var.execution_role_policy_arns[count.index]
}
resource "aws_iam_role" "task_role" {
name = "${var.service_name}-task-role"
assume_role_policy = data.aws_iam_policy_document.ecs_tasks_assume_role_policy.json
}
resource "aws_iam_role_policy_attachment" "task_role_policy" {
role = aws_iam_role.task_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonECS_FullAccess"
}
resource "aws_iam_role_policy_attachment" "task_role_additional_policies" {
count = length(local.task_role_policy_arns)
role = aws_iam_role.task_role.name
policy_arn = local.task_role_policy_arns[count.index]
}
data "aws_iam_policy_document" "exec" {
count = var.enable_ecs_exec ? 1 : 0
statement {
effect = "Allow"
actions = [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel",
"logs:CreateLogStream",
"logs:DescribeLogStreams",
"logs:PutLogEvents"
]
resources = ["*"]
}
}
resource "aws_iam_policy" "exec" {
count = var.enable_ecs_exec ? 1 : 0
name = "${var.service_name}-ecs-exec-policy"
policy = data.aws_iam_policy_document.exec[0].json
}