-
Notifications
You must be signed in to change notification settings - Fork 19
/
iam.tf
89 lines (74 loc) · 2.66 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
// Allow EC2 instance to register as ECS cluster member, fetch ECR images, write logs to CloudWatch
data "aws_iam_policy_document" "ec2_instance_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ec2_instance_role" {
assume_role_policy = data.aws_iam_policy_document.ec2_instance_assume_role_policy.json
name = "EcsCluster${local.name}Ec2InstanceRole"
tags = local.tags
}
resource "aws_iam_role_policy_attachment" "ecs_instance_role" {
role = aws_iam_role.ec2_instance_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
resource "aws_iam_role_policy_attachment" "ssm_core_role" {
role = aws_iam_role.ec2_instance_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_instance_profile" "ecs_node" {
name = "EcsCluster${local.name}Ec2InstanceProfile"
role = aws_iam_role.ec2_instance_role.name
}
// Allow ECS service to interact with LoadBalancers
data "aws_iam_policy_document" "ecs_service_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_service_role" {
assume_role_policy = data.aws_iam_policy_document.ecs_service_role_policy.json
name = "EcsCluster${local.name}ServiceRole"
tags = local.tags
}
resource "aws_iam_role_policy_attachment" "service_role" {
role = aws_iam_role.ecs_service_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole"
}
data "aws_iam_policy_document" "ecs_task_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_task_role" {
assume_role_policy = data.aws_iam_policy_document.ecs_task_role_policy.json
name = "EcsCluster${local.name}DefaultTaskRole"
tags = local.tags
}
resource "aws_iam_role_policy_attachment" "default_task_role" {
role = aws_iam_role.ecs_task_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
data "aws_iam_policy_document" "allow_create_log_groups" {
statement {
actions = ["logs:CreateLogGroup"]
resources = ["*"]
}
}
resource "aws_iam_role_policy" "allow_create_log_groups" {
policy = data.aws_iam_policy_document.allow_create_log_groups.json
role = aws_iam_role.ecs_task_role.id
}