This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.tf
74 lines (65 loc) · 1.84 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
resource "aws_iam_instance_profile" "ecs_profile" {
name = "${replace(lower(var.ecs_cluster_name), " ", "-")}-profile"
role = aws_iam_role.ecs_role.name
}
resource "aws_iam_role" "ecs_role" {
name = "RoleForECSCluster-${replace(var.ecs_cluster_name, " ", "")}"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
data "aws_iam_policy" "aws_ssm_default" {
name = "AmazonSSMManagedInstanceCore"
}
data "aws_iam_policy" "aws_ecs_default" {
name = "AmazonEC2ContainerServiceforEC2Role"
}
# We don't do a data lookup here because the data element requires an ARN,
# which would just be redundant and dumb.
resource "aws_iam_role_policy_attachment" "ecs_role_policy_attachment" {
role = aws_iam_role.ecs_role.name
policy_arn = data.aws_iam_policy.aws_ecs_default.arn
}
# Add SSM for patching
resource "aws_iam_role_policy_attachment" "add_ssm_for_patching" {
role = aws_iam_role.ecs_role.name
policy_arn = data.aws_iam_policy.aws_ssm_default.arn
}
# gives ecs container instances the ability to create cloudwatch assets as needed
resource "aws_iam_policy" "cloudwatch_access_policy" {
name = "${replace(lower(var.ecs_cluster_name), " ", "-")}-cloudwatch-access-policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
],
"Resource": [
"arn:aws:logs:*:*:*"
]
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "cloudwatch_access_policy_attachment" {
role = aws_iam_role.ecs_role.name
policy_arn = aws_iam_policy.cloudwatch_access_policy.arn
}