Repository navigation
Expand file tree
/
Copy pathmain.tf
More file actions
291 lines (246 loc) · 8.8 KB
/
Copy pathmain.tf
File metadata and controls
291 lines (246 loc) · 8.8 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
################################################################################
# CodeBuild
################################################################################
locals {
use_ecr_repository = var.create_ecr_repository || var.ecr_repository_name != null
ecr_repository_name = coalesce(var.ecr_repository_name, var.name)
environment_image = (
var.environment_image != null
? var.environment_image
: (
local.use_ecr_repository
? "${local.aws_account_id}.dkr.ecr.${local.aws_region}.amazonaws.com/${local.ecr_repository_name}:latest"
: "aws/codebuild/amazonlinux2-x86_64-standard:5.0"
)
)
}
resource "aws_codebuild_project" "this" {
name = var.name
description = local.description
tags = local.tags
build_timeout = var.build_timeout
service_role = (
local.create_iam_role
? aws_iam_role.this[0].arn
: "arn:${local.aws_partition}:iam::${local.aws_account_id}:role/${var.iam_role_name}"
)
artifacts {
type = "NO_ARTIFACTS"
}
environment {
type = var.environment_type
compute_type = var.environment_compute_type
image = local.environment_image
image_pull_credentials_type = (
# terraform > v1.3.0: startswith(local.environment_image, "aws/codebuild/")
length(regexall("^aws/codebuild/.*", local.environment_image)) > 0
? "CODEBUILD"
: "SERVICE_ROLE"
)
privileged_mode = var.privileged_mode
dynamic "environment_variable" {
for_each = var.environment_variables
content {
name = environment_variable.value.name
value = environment_variable.value.value
type = environment_variable.value.type
}
}
}
logs_config {
cloudwatch_logs {
group_name = (
var.create_cloudwatch_log_group
? aws_cloudwatch_log_group.codebuild[0].name
: data.aws_cloudwatch_log_group.codebuild[0].name
)
stream_name = local.cloudwatch_logs_steam_name
}
dynamic "s3_logs" {
for_each = try(var.s3_logs_bucket_name, null) == null ? toset([]) : toset([1])
content {
status = "ENABLED"
location = "${var.s3_logs_bucket_name}/${var.s3_logs_bucket_prefix}"
}
}
}
source {
type = "GITHUB"
location = var.source_location
git_clone_depth = 1
git_submodules_config {
fetch_submodules = true
}
dynamic "auth" {
for_each = var.source_auth != null ? [1] : []
content {
type = var.source_auth.type
resource = var.source_auth.resource
}
}
}
dynamic "vpc_config" {
for_each = local.has_vpc_config ? toset([1]) : toset([])
content {
vpc_id = var.vpc_id
subnets = var.subnet_ids
security_group_ids = local.security_group_ids
}
}
depends_on = [aws_iam_role_policy.codeconnection_required]
}
resource "aws_codebuild_source_credential" "string" {
count = local.has_github_personal_access_token ? 1 : 0
auth_type = "PERSONAL_ACCESS_TOKEN"
server_type = "GITHUB"
token = var.github_personal_access_token
}
resource "aws_codebuild_source_credential" "secret" {
count = local.has_github_personal_access_token_secret_arn ? 1 : 0
auth_type = "SECRETS_MANAGER"
server_type = "GITHUB"
token = var.github_secretsmanager_secret_arn
}
resource "aws_codebuild_source_credential" "ssm" {
count = local.has_github_personal_access_token_ssm_parameter ? 1 : 0
auth_type = "PERSONAL_ACCESS_TOKEN"
server_type = "GITHUB"
token = data.aws_ssm_parameter.github_personal_access_token[0].value
}
resource "aws_codebuild_source_credential" "codeconnection" {
count = local.has_github_codeconnection_arn ? 1 : 0
auth_type = "CODECONNECTIONS"
server_type = "GITHUB"
token = var.github_codeconnection_arn
}
resource "aws_codebuild_webhook" "this" {
depends_on = [
aws_codebuild_source_credential.string,
aws_codebuild_source_credential.ssm,
aws_codebuild_source_credential.secret,
aws_codebuild_source_credential.codeconnection,
aws_iam_role_policy.codeconnection_required
]
project_name = aws_codebuild_project.this.name
build_type = "BUILD"
dynamic "filter_group" {
for_each = var.webhook_filter_groups
content {
dynamic "filter" {
for_each = filter_group.value
content {
type = filter.value.type
pattern = filter.value.pattern
exclude_matched_pattern = filter.value.exclude_matched_pattern
}
}
}
}
dynamic "scope_configuration" {
for_each = var.source_location == "CODEBUILD_DEFAULT_WEBHOOK_SOURCE_LOCATION" && var.source_organization != null ? toset([1]) : toset([])
content {
name = var.source_organization
scope = "GITHUB_ORGANIZATION"
}
}
}
################################################################################
# Security Group
################################################################################
locals {
create_security_group = local.has_vpc_config && length(var.security_group_ids) == 0
security_group_name = coalesce(var.security_group_name, var.name)
security_group_ids = concat(
local.create_security_group
? [aws_security_group.codebuild[0].id]
: [],
var.security_group_ids
)
}
resource "aws_security_group" "codebuild" {
#checkov:skip=CKV2_AWS_5:access logging not required
count = local.create_security_group ? 1 : 0
vpc_id = var.vpc_id
name = local.security_group_name
description = "Security group for CodeBuild project ${var.name}"
tags = {
Name = local.security_group_name
}
}
resource "aws_vpc_security_group_egress_rule" "codebuild" {
count = local.create_security_group ? 1 : 0
security_group_id = aws_security_group.codebuild[count.index].id
cidr_ipv4 = "0.0.0.0/0"
ip_protocol = "-1"
description = "Allow all traffic to ALL"
}
resource "aws_vpc_security_group_ingress_rule" "codebuild" {
count = local.create_security_group ? 1 : 0
security_group_id = aws_security_group.codebuild[count.index].id
cidr_ipv4 = "0.0.0.0/0"
from_port = 443
ip_protocol = "tcp"
to_port = 443
description = "Allow HTTPS traffic from ALL"
}
resource "aws_security_group_rule" "ingress_with_cidr_blocks" {
count = local.create_security_group ? length(var.ingress_with_cidr_blocks) : 0
security_group_id = aws_security_group.codebuild[0].id
type = "ingress"
from_port = var.ingress_with_cidr_blocks[count.index].from_port
to_port = var.ingress_with_cidr_blocks[count.index].to_port
protocol = var.ingress_with_cidr_blocks[count.index].protocol
cidr_blocks = var.ingress_with_cidr_blocks[count.index].cidr_blocks
description = var.ingress_with_cidr_blocks[count.index].description
}
resource "aws_security_group_rule" "ingress_with_source_security_group_id" {
count = local.create_security_group ? length(var.ingress_with_source_security_group_id) : 0
security_group_id = aws_security_group.codebuild[0].id
type = "ingress"
from_port = var.ingress_with_source_security_group_id[count.index].from_port
to_port = var.ingress_with_source_security_group_id[count.index].to_port
protocol = var.ingress_with_source_security_group_id[count.index].protocol
source_security_group_id = var.ingress_with_source_security_group_id[count.index].source_security_group_id
description = var.ingress_with_source_security_group_id[count.index].description
}
################################################################################
# ECR Repository
################################################################################
resource "aws_ecr_repository" "this" {
#checkov:skip=CKV_AWS_136:encryption not required
#checkov:skip=CKV_AWS_51:latest tag used by codebuild so tag needs to be overwritten
count = var.create_ecr_repository ? 1 : 0
name = local.ecr_repository_name
image_tag_mutability = "IMMUTABLE"
force_delete = true
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "KMS"
kms_key = var.kms_key_id
}
}
resource "aws_ecr_lifecycle_policy" "policy" {
count = var.create_ecr_repository ? 1 : 0
repository = aws_ecr_repository.this[0].name
policy = <<-EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Expire images older than 14 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": {
"type": "expire"
}
}
]
}
EOF
}