-
Notifications
You must be signed in to change notification settings - Fork 14
/
iam.tf
84 lines (66 loc) · 2.6 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
resource "aws_iam_role" "task_execution_role" {
count = var.task_execution_role_arn == "" ? 1 : 0
assume_role_policy = data.aws_iam_policy_document.task_execution_role[count.index].json
description = "Task execution role for ${var.service_name}"
name = "${var.service_name}-execution-role-${data.aws_region.current.name}"
managed_policy_arns = [data.aws_iam_policy.ecs_task_execution_policy[count.index].arn]
path = "/ecs/"
tags = var.tags
}
data "aws_iam_policy_document" "task_execution_role" {
count = var.task_execution_role_arn == "" ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_task_role" {
count = var.task_role_arn == "" ? 1 : 0
assume_role_policy = data.aws_iam_policy_document.ecs_task_assume_role_policy[count.index].json
description = "Task Role for service ${var.service_name}"
name = "${var.service_name}-${data.aws_region.current.name}"
path = "/ecs/task-role/"
tags = var.tags
}
resource "aws_iam_role_policy" "ecs_task_role_policy" {
count = var.task_role_arn == "" && var.policy_document != "" ? 1 : 0
name = "ecs-task-${var.service_name}-${data.aws_region.current.name}"
policy = var.policy_document
role = aws_iam_role.ecs_task_role[count.index].id
}
data "aws_iam_policy_document" "ecs_task_assume_role_policy" {
count = var.task_role_arn == "" ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "enable_execute_command" {
count = var.enable_execute_command && var.task_role_arn == "" ? 1 : 0
statement {
actions = [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel",
]
resources = ["*"]
}
}
resource "aws_iam_policy" "enable_execute_command" {
count = var.enable_execute_command ? 1 : 0
name = "enable-execute-command-${var.service_name}-${data.aws_region.current.name}"
path = "/ecs/task-role/"
policy = data.aws_iam_policy_document.enable_execute_command[count.index].json
}
resource "aws_iam_role_policy_attachment" "enable_execute_command" {
count = var.enable_execute_command && var.task_role_arn == "" ? 1 : 0
role = aws_iam_role.ecs_task_role[count.index].name
policy_arn = aws_iam_policy.enable_execute_command[count.index].arn
}