-
Notifications
You must be signed in to change notification settings - Fork 25
/
cicd.tf
82 lines (67 loc) · 1.72 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
# create ci/cd user with access keys (for build system)
resource "aws_iam_user" "cicd" {
name = "srv_${var.app}_${var.environment}_cicd"
}
resource "aws_iam_access_key" "cicd_keys" {
user = aws_iam_user.cicd.name
}
# 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 = [
data.aws_ecr_repository.ecr.arn,
]
}
# allows user to deploy to ecs
statement {
sid = "ecs"
actions = [
"ecr:GetAuthorizationToken",
"ecs:DescribeServices",
"ecs:DescribeTaskDefinition",
"ecs:UpdateService",
"ecs:RegisterTaskDefinition",
]
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,
]
}
}
resource "aws_iam_user_policy" "cicd_user_policy" {
name = "${var.app}_${var.environment}_cicd"
user = aws_iam_user.cicd.name
policy = data.aws_iam_policy_document.cicd_policy.json
}
data "aws_ecr_repository" "ecr" {
name = var.app
}
# The AWS keys for the CICD user to use in a build system
output "cicd_keys" {
value = "terraform state show aws_iam_access_key.cicd_keys"
}
# The URL for the docker image repo in ECR
output "docker_registry" {
value = data.aws_ecr_repository.ecr.repository_url
}