-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathroles.tf
129 lines (112 loc) · 3.99 KB
/
roles.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
/*
Module: ECS-Fargate-Appmesh
Version: 2.0.0
This file will create:
- IAM policy: to allow ecs tasks to assume role
- IAM role: to assume role policy we defined
- IAM role: to assume secret manager policy
- IAM role: to assume appmesh envoy access
*/
// ecs task execution role json data
data "aws_iam_policy_document" "ecs_task_execution_role" {
// version for policy
version = "2012-10-17"
// state for policy to allow service to assume role
statement {
sid = ""
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
// ecs task execution role
resource "aws_iam_role" "ecs_task_execution_role" {
// set name for role
name = "${var.prefix}-${var.env}-${var.app_name}-ecs-task-execution-role"
// attach policy to role
assume_role_policy = data.aws_iam_policy_document.ecs_task_execution_role.json
// add tags
tags = var.tags
}
// ecs task execution role policy attachment
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role" {
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
// ecs task allow appmesh permissions policy attachment
resource "aws_iam_role_policy_attachment" "ecs_appmesh_envoy_access_role" {
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/AWSAppMeshEnvoyAccess"
}
resource "aws_iam_policy" "secrets_policy" {
count = length(var.secrets) == 0 ? 0 : 1
name = "${var.prefix}-${var.env}-${var.app_name}-secrets-policy"
path = "/"
description = "Defined policy to access secrets from secret manager"
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"secretsmanager:GetResourcePolicy",
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret",
"secretsmanager:ListSecretVersionIds",
]
Effect = "Allow"
Resource = var.secrets.*.arn
},
]
})
}
// to attach the secret manager policy
resource "aws_iam_role_policy_attachment" "sm-policy-attach" {
count = length(var.secrets) == 0 ? 0 : 1
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = aws_iam_policy.secrets_policy[0].arn
}
resource "aws_iam_policy" "parameters_policy" {
count = length(var.parameters) == 0 ? 0 : 1
name = "${var.prefix}-${var.env}-${var.app_name}-parameters-policy"
path = "/"
description = "Defined policy to access parameters from parameter store"
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"ssm:GetParametersByPath",
"ssm:GetParameters",
"ssm:GetParameter"
]
Effect = "Allow"
Resource = var.parameters.*.arn
},
]
})
}
// to attach the secret manager policy
resource "aws_iam_role_policy_attachment" "ps-policy-attach" {
count = length(var.parameters) == 0 ? 0 : 1
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = aws_iam_policy.parameters_policy[0].arn
}
// add module provided policies
resource "aws_iam_role_policy_attachment" "module-provided-policies" {
count = length(var.policy_arn_attachments) == 0 ? 0 : length(var.policy_arn_attachments)
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = var.policy_arn_attachments[count.index]
}
// attach ecr policy if ecr is created
resource "aws_iam_role_policy_attachment" "appmesh-ecr-policy" {
count = (var.app_image != "none" || var.virtual_gateway_arn != "none") ? 0 : 1
role = aws_iam_role.ecs_task_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSAppRunnerServicePolicyForECRAccess"
}