-
Notifications
You must be signed in to change notification settings - Fork 12
/
cicd.tf
98 lines (79 loc) · 1.98 KB
/
cicd.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
# create ci/cd user with access keys (for build system)
variable "ecr_repository_arn" {}
variable "additional_user_groups" {
default = []
}
resource "aws_iam_user" "cicd" {
name = "srv_${var.app}_${var.environment}_cicd"
}
resource "aws_iam_group" "cicd" {
name = "srv_${var.app}_${var.environment}_cicd"
}
resource "aws_iam_user_group_membership" "cicd" {
user = aws_iam_user.cicd.name
groups = concat([aws_iam_group.cicd.name], var.additional_user_groups)
}
# grant required permissions to deploy
data "aws_iam_policy_document" "cicd_policy" {
# allows user to push/pull to the registry
statement {
sid = "ecr"
actions = [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability",
"ecr:PutImage",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
]
resources = [
var.ecr_repository_arn
]
}
# allows user to deploy to ecs
statement {
sid = "ecs"
actions = [
"ecr:GetAuthorizationToken",
"ecs:DescribeServices",
"ecs:DescribeTaskDefinition",
"ecs:UpdateService",
"ecs:RegisterTaskDefinition",
"ecs:TagResource"
]
resources = [
"*",
]
}
# allows user to run ecs task using task execution and app roles
statement {
sid = "approle"
actions = [
"iam:PassRole",
]
resources = [
aws_iam_role.app_role.arn,
aws_iam_role.ecsTaskExecutionRole.arn,
]
}
# allows user to manipulate its own access keys
statement {
sid = "accessKeys"
actions = [
"iam:DeleteAccessKey",
"iam:GetAccessKeyLastUsed",
"iam:UpdateAccessKey",
"iam:CreateAccessKey",
"iam:ListAccessKeys",
]
resources = [
aws_iam_user.cicd.arn,
]
}
}
resource "aws_iam_group_policy" "cicd_group_policy" {
name = "${var.app}_${var.environment}_cicd"
group = aws_iam_group.cicd.name
policy = data.aws_iam_policy_document.cicd_policy.json
}