forked from cloudposse/terraform-aws-codebuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
213 lines (176 loc) · 5.63 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
data "aws_caller_identity" "default" {}
data "aws_region" "default" {}
# Define composite variables for resources
module "label" {
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.1.0"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
delimiter = "${var.delimiter}"
attributes = "${var.attributes}"
tags = "${var.tags}"
}
resource "aws_s3_bucket" "cache_bucket" {
count = "${var.enabled == "true" && var.cache_enabled == "true" ? 1 : 0}"
bucket = "${local.cache_bucket_name_normalised}"
acl = "private"
force_destroy = true
tags = "${module.label.tags}"
lifecycle_rule {
id = "codebuildcache"
enabled = true
prefix = "/"
tags = "${module.label.tags}"
expiration {
days = "${var.cache_expiration_days}"
}
}
}
resource "random_string" "bucket_prefix" {
length = 12
number = false
upper = false
special = false
lower = true
}
locals {
cache_bucket_name = "${module.label.id}${var.cache_bucket_suffix_enabled == "true" ? "-${random_string.bucket_prefix.result}" : "" }"
## Clean up the bucket name to use only hyphens, and trim its length to 63 characters.
## As per https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
cache_bucket_name_normalised = "${substr(join("-", split("_", lower(local.cache_bucket_name))), 0, min(length(local.cache_bucket_name),63))}"
## This is the magic where a map of a list of maps is generated
## and used to conditionally add the cache bucket option to the
## aws_codebuild_project
cache_def = {
"true" = [{
type = "S3"
location = "${var.enabled == "true" && var.cache_enabled == "true" ? join("", aws_s3_bucket.cache_bucket.*.bucket) : "none" }"
}]
"false" = []
}
# Final Map Selected from above
cache = "${local.cache_def[var.cache_enabled]}"
}
resource "aws_iam_role" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${module.label.id}"
assume_role_policy = "${data.aws_iam_policy_document.role.json}"
}
data "aws_iam_policy_document" "role" {
statement {
sid = ""
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = ["codebuild.amazonaws.com"]
}
effect = "Allow"
}
}
resource "aws_iam_policy" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${module.label.id}"
path = "/service-role/"
policy = "${data.aws_iam_policy_document.permissions.json}"
}
resource "aws_iam_policy" "default_cache_bucket" {
count = "${var.enabled == "true" && var.cache_enabled == "true" ? 1 : 0}"
name = "${module.label.id}-cache-bucket"
path = "/service-role/"
policy = "${data.aws_iam_policy_document.permissions_cache_bucket.json}"
}
data "aws_iam_policy_document" "permissions" {
statement {
sid = ""
actions = [
"ecr:BatchCheckLayerAvailability",
"ecr:CompleteLayerUpload",
"ecr:GetAuthorizationToken",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"ssm:GetParameters",
]
effect = "Allow"
resources = [
"*",
]
}
}
data "aws_iam_policy_document" "permissions_cache_bucket" {
count = "${var.enabled == "true" ? 1 : 0}"
statement {
sid = ""
actions = [
"s3:*",
]
effect = "Allow"
resources = [
"${aws_s3_bucket.cache_bucket.arn}",
"${aws_s3_bucket.cache_bucket.arn}/*",
]
}
}
resource "aws_iam_role_policy_attachment" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
policy_arn = "${aws_iam_policy.default.arn}"
role = "${aws_iam_role.default.id}"
}
resource "aws_iam_role_policy_attachment" "default_cache_bucket" {
count = "${var.enabled == "true" && var.cache_enabled == "true" ? 1 : 0}"
policy_arn = "${element(aws_iam_policy.default_cache_bucket.*.arn, count.index)}"
role = "${aws_iam_role.default.id}"
}
resource "aws_codebuild_project" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${module.label.id}"
service_role = "${aws_iam_role.default.arn}"
artifacts {
type = "CODEPIPELINE"
}
# The cache as a list with a map object inside.
cache = ["${local.cache}"]
environment {
compute_type = "${var.build_compute_type}"
image = "${var.build_image}"
type = "LINUX_CONTAINER"
privileged_mode = "${var.privileged_mode}"
environment_variable = [{
"name" = "AWS_REGION"
"value" = "${signum(length(var.aws_region)) == 1 ? var.aws_region : data.aws_region.default.name}"
},
{
"name" = "AWS_ACCOUNT_ID"
"value" = "${signum(length(var.aws_account_id)) == 1 ? var.aws_account_id : data.aws_caller_identity.default.account_id}"
},
{
"name" = "IMAGE_REPO_NAME"
"value" = "${signum(length(var.image_repo_name)) == 1 ? var.image_repo_name : "UNSET"}"
},
{
"name" = "IMAGE_TAG"
"value" = "${signum(length(var.image_tag)) == 1 ? var.image_tag : "latest"}"
},
{
"name" = "STAGE"
"value" = "${signum(length(var.stage)) == 1 ? var.stage : "UNSET"}"
},
{
"name" = "GITHUB_TOKEN"
"value" = "${signum(length(var.github_token)) == 1 ? var.github_token : "UNSET"}"
},
"${var.environment_variables}",
]
}
source {
buildspec = "${var.buildspec}"
type = "${var.source_type}"
location = "${var.source_location}"
}
tags = "${module.label.tags}"
}