-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
main.tf
463 lines (380 loc) · 12.2 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
locals {
codestar_enabled = module.this.enabled && var.codestar_connection_arn != "" && var.codestar_connection_arn != null
}
module "codepipeline_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["codepipeline"]
context = module.this.context
}
resource "aws_s3_bucket" "default" {
count = module.this.enabled ? 1 : 0
bucket = module.codepipeline_label.id
acl = "private"
force_destroy = var.s3_bucket_force_destroy
tags = module.codepipeline_label.tags
}
module "codepipeline_assume_role_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["codepipeline", "assume"]
context = module.this.context
}
resource "aws_iam_role" "default" {
count = module.this.enabled ? 1 : 0
name = module.codepipeline_assume_role_label.id
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}
data "aws_iam_policy_document" "assume_role" {
statement {
sid = ""
actions = [
"sts:AssumeRole"
]
principals {
type = "Service"
identifiers = ["codepipeline.amazonaws.com"]
}
effect = "Allow"
}
}
resource "aws_iam_role_policy_attachment" "default" {
count = module.this.enabled ? 1 : 0
role = join("", aws_iam_role.default.*.id)
policy_arn = join("", aws_iam_policy.default.*.arn)
}
resource "aws_iam_policy" "default" {
count = module.this.enabled ? 1 : 0
name = module.codepipeline_label.id
policy = data.aws_iam_policy_document.default.json
}
data "aws_iam_policy_document" "default" {
statement {
sid = ""
actions = [
"ec2:*",
"elasticloadbalancing:*",
"autoscaling:*",
"cloudwatch:*",
"s3:*",
"sns:*",
"cloudformation:*",
"rds:*",
"sqs:*",
"ecs:*",
"iam:PassRole"
]
resources = ["*"]
effect = "Allow"
}
}
resource "aws_iam_role_policy_attachment" "s3" {
count = module.this.enabled ? 1 : 0
role = join("", aws_iam_role.default.*.id)
policy_arn = join("", aws_iam_policy.s3.*.arn)
}
module "codepipeline_s3_policy_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["codepipeline", "s3"]
context = module.this.context
}
resource "aws_iam_policy" "s3" {
count = module.this.enabled ? 1 : 0
name = module.codepipeline_s3_policy_label.id
policy = join("", data.aws_iam_policy_document.s3.*.json)
}
data "aws_iam_policy_document" "s3" {
count = module.this.enabled ? 1 : 0
statement {
sid = ""
actions = [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:GetBucketVersioning",
"s3:PutObject"
]
resources = [
join("", aws_s3_bucket.default.*.arn),
"${join("", aws_s3_bucket.default.*.arn)}/*"
]
effect = "Allow"
}
}
resource "aws_iam_role_policy_attachment" "codebuild" {
count = module.this.enabled ? 1 : 0
role = join("", aws_iam_role.default.*.id)
policy_arn = join("", aws_iam_policy.codebuild.*.arn)
}
module "codebuild_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["codebuild"]
context = module.this.context
}
resource "aws_iam_policy" "codebuild" {
count = module.this.enabled ? 1 : 0
name = module.codebuild_label.id
policy = data.aws_iam_policy_document.codebuild.json
}
data "aws_iam_policy_document" "codebuild" {
statement {
sid = ""
actions = [
"codebuild:*"
]
resources = [module.codebuild.project_id]
effect = "Allow"
}
}
# https://docs.aws.amazon.com/codepipeline/latest/userguide/connections-permissions.html
resource "aws_iam_role_policy_attachment" "codestar" {
count = local.codestar_enabled ? 1 : 0
role = join("", aws_iam_role.default.*.id)
policy_arn = join("", aws_iam_policy.codestar.*.arn)
}
module "codestar_label" {
source = "cloudposse/label/null"
version = "0.25.0"
enabled = local.codestar_enabled
attributes = ["codestar"]
context = module.this.context
}
resource "aws_iam_policy" "codestar" {
count = local.codestar_enabled ? 1 : 0
name = module.codestar_label.id
policy = join("", data.aws_iam_policy_document.codestar.*.json)
}
data "aws_iam_policy_document" "codestar" {
count = local.codestar_enabled ? 1 : 0
statement {
sid = ""
actions = [
"codestar-connections:UseConnection"
]
condition {
test = "ForAllValues:StringEquals"
variable = "codestar-connections:FullRepositoryId"
values = [
format("%s/%s", var.repo_owner, var.repo_name)
]
}
resources = [var.codestar_connection_arn]
effect = "Allow"
}
}
data "aws_caller_identity" "default" {
}
data "aws_region" "default" {
}
module "codebuild" {
enabled = module.this.enabled
source = "cloudposse/codebuild/aws"
version = "2.0.2"
build_type = var.build_type
build_image = var.build_image
build_compute_type = var.build_compute_type
build_timeout = var.build_timeout
buildspec = var.buildspec
delimiter = module.this.delimiter
attributes = ["build"]
privileged_mode = var.privileged_mode
aws_region = var.region != "" ? var.region : data.aws_region.default.name
aws_account_id = var.aws_account_id != "" ? var.aws_account_id : data.aws_caller_identity.default.account_id
image_repo_name = var.image_repo_name
image_tag = var.image_tag
github_token = var.github_oauth_token
environment_variables = var.environment_variables
badge_enabled = var.badge_enabled
cache_type = var.cache_type
local_cache_modes = var.local_cache_modes
secondary_artifact_location = var.secondary_artifact_bucket_id
secondary_artifact_identifier = var.secondary_artifact_identifier
secondary_artifact_encryption_enabled = var.secondary_artifact_encryption_enabled
vpc_config = var.codebuild_vpc_config
cache_bucket_suffix_enabled = var.cache_bucket_suffix_enabled
context = module.this.context
}
resource "aws_iam_role_policy_attachment" "codebuild_s3" {
count = module.this.enabled ? 1 : 0
role = module.codebuild.role_id
policy_arn = join("", aws_iam_policy.s3.*.arn)
}
resource "aws_iam_role_policy_attachment" "codebuild_codestar" {
count = local.codestar_enabled && var.codestar_output_artifact_format == "CODEBUILD_CLONE_REF" ? 1 : 0
role = module.codebuild.role_id
policy_arn = join("", aws_iam_policy.codestar.*.arn)
}
resource "aws_iam_role_policy_attachment" "codebuild_extras" {
for_each = module.this.enabled ? toset(var.codebuild_extra_policy_arns) : []
role = module.codebuild.role_id
policy_arn = each.value
}
resource "aws_codepipeline" "default" {
count = module.this.enabled && var.github_oauth_token != "" ? 1 : 0
name = module.codepipeline_label.id
role_arn = join("", aws_iam_role.default.*.arn)
artifact_store {
location = join("", aws_s3_bucket.default.*.bucket)
type = "S3"
}
depends_on = [
aws_iam_role_policy_attachment.default,
aws_iam_role_policy_attachment.s3,
aws_iam_role_policy_attachment.codebuild,
aws_iam_role_policy_attachment.codebuild_s3,
aws_iam_role_policy_attachment.codebuild_extras
]
stage {
name = "Source"
action {
name = "Source"
category = "Source"
owner = "ThirdParty"
provider = "GitHub"
version = "1"
output_artifacts = ["code"]
configuration = {
OAuthToken = var.github_oauth_token
Owner = var.repo_owner
Repo = var.repo_name
Branch = var.branch
PollForSourceChanges = var.poll_source_changes
}
}
}
stage {
name = "Build"
action {
name = "Build"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
version = "1"
input_artifacts = ["code"]
output_artifacts = ["task"]
configuration = {
ProjectName = module.codebuild.project_name
}
}
}
stage {
name = "Deploy"
action {
name = "Deploy"
category = "Deploy"
owner = "AWS"
provider = "ECS"
input_artifacts = ["task"]
version = "1"
configuration = {
ClusterName = var.ecs_cluster_name
ServiceName = var.service_name
}
}
}
lifecycle {
# prevent github OAuthToken from causing updates, since it's removed from state file
ignore_changes = [stage[0].action[0].configuration]
}
}
# https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodestarConnectionSource.html#action-reference-CodestarConnectionSource-example
resource "aws_codepipeline" "bitbucket" {
count = local.codestar_enabled ? 1 : 0
name = module.codepipeline_label.id
role_arn = join("", aws_iam_role.default.*.arn)
artifact_store {
location = join("", aws_s3_bucket.default.*.bucket)
type = "S3"
}
depends_on = [
aws_iam_role_policy_attachment.default,
aws_iam_role_policy_attachment.s3,
aws_iam_role_policy_attachment.codebuild,
aws_iam_role_policy_attachment.codebuild_s3,
aws_iam_role_policy_attachment.codestar,
aws_iam_role_policy_attachment.codebuild_extras
]
stage {
name = "Source"
action {
name = "Source"
category = "Source"
owner = "AWS"
provider = "CodeStarSourceConnection"
version = "1"
output_artifacts = ["code"]
configuration = {
ConnectionArn = var.codestar_connection_arn
FullRepositoryId = format("%s/%s", var.repo_owner, var.repo_name)
BranchName = var.branch
OutputArtifactFormat = var.codestar_output_artifact_format
}
}
}
stage {
name = "Build"
action {
name = "Build"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
version = "1"
input_artifacts = ["code"]
output_artifacts = ["task"]
configuration = {
ProjectName = module.codebuild.project_name
}
}
}
stage {
name = "Deploy"
action {
name = "Deploy"
category = "Deploy"
owner = "AWS"
provider = "ECS"
input_artifacts = ["task"]
version = "1"
configuration = {
ClusterName = var.ecs_cluster_name
ServiceName = var.service_name
}
}
}
}
resource "random_string" "webhook_secret" {
count = module.this.enabled && var.webhook_enabled ? 1 : 0
length = 32
# Special characters are not allowed in webhook secret (AWS silently ignores webhook callbacks)
special = false
}
locals {
webhook_secret = join("", random_string.webhook_secret.*.result)
webhook_url = join("", aws_codepipeline_webhook.webhook.*.url)
}
resource "aws_codepipeline_webhook" "webhook" {
count = module.this.enabled && var.webhook_enabled ? 1 : 0
name = module.codepipeline_label.id
authentication = var.webhook_authentication
target_action = var.webhook_target_action
target_pipeline = join("", aws_codepipeline.default.*.name)
authentication_configuration {
secret_token = local.webhook_secret
}
filter {
json_path = var.webhook_filter_json_path
match_equals = var.webhook_filter_match_equals
}
}
module "github_webhooks" {
source = "cloudposse/repository-webhooks/github"
version = "0.14.0"
enabled = module.this.enabled && var.webhook_enabled ? true : false
github_repositories = [var.repo_name]
webhook_url = local.webhook_url
webhook_secret = local.webhook_secret
webhook_content_type = "json"
events = var.github_webhook_events
context = module.this.context
}