Skip to content

Commit 51e6e88

Browse files
authored
override codebuild defaults (#31)
Optional override for plan and apply CodeBuild buildspecs and images
1 parent 97efbe6 commit 51e6e88

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ module "pipeline" {
7474
checkov_version = "3.2.0"
7575
tflint_version = "0.55.0"
7676
77+
build_override = {
78+
plan_buildspec = file("./my_plan.yml")
79+
plan_image = "aws/codebuild/amazonlinux2-x86_64-standard:5.0"
80+
apply_buildspec = file("./my_apply.yml")
81+
apply_image = "hashicorp/terraform:latest"
82+
}
83+
7784
vpc = {
7885
vpc_id = "vpc-011a22334455bb66c",
7986
subnets = ["subnet-011aabbcc2233d4ef"],

codebuild.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module "validation" {
88
codebuild_role = aws_iam_role.codebuild_validate.arn
99
environment_variables = var.tags == "" ? local.env_var : local.conditional_env_var
1010
build_timeout = var.build_timeout
11-
build_spec = "${each.key}.yml"
11+
build_spec = file("${path.module}/modules/codebuild/buildspecs/${each.key}.yml")
1212
log_group = aws_cloudwatch_log_group.this.name
1313
image = each.value
1414
vpc = var.vpc
@@ -20,9 +20,9 @@ module "plan" {
2020
codebuild_role = aws_iam_role.codebuild_execution.arn
2121
environment_variables = local.env_var
2222
build_timeout = var.build_timeout
23-
build_spec = "plan.yml"
23+
build_spec = var.build_override["plan_buildspec"] != null ? var.build_override["plan_buildspec"] : file("${path.module}/modules/codebuild/buildspecs/plan.yml")
2424
log_group = aws_cloudwatch_log_group.this.name
25-
image = "hashicorp/terraform:${var.terraform_version}"
25+
image = var.build_override["plan_image"] != null ? var.build_override["plan_image"] : "hashicorp/terraform:${var.terraform_version}"
2626
vpc = var.vpc
2727
}
2828

@@ -32,9 +32,9 @@ module "apply" {
3232
codebuild_role = aws_iam_role.codebuild_execution.arn
3333
environment_variables = local.env_var
3434
build_timeout = var.build_timeout
35-
build_spec = "apply.yml"
35+
build_spec = var.build_override["apply_buildspec"] != null ? var.build_override["apply_buildspec"] : file("${path.module}/modules/codebuild/buildspecs/apply.yml")
3636
log_group = aws_cloudwatch_log_group.this.name
37-
image = "hashicorp/terraform:${var.terraform_version}"
37+
image = var.build_override["apply_image"] != null ? var.build_override["apply_image"] : "hashicorp/terraform:${var.terraform_version}"
3838
vpc = var.vpc
3939
}
4040

docs/optional_inputs.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
`tflint_version` controls the [tflint](https://github.com/terraform-linters/tflint) version. It defaults to 0.48.0.
2626

27+
`build_override` can replace the existing CodeBuild buildspecs and images with your own.
28+
2729
`vpc` configures the CodeBuild projects to [run in a VPC](https://docs.aws.amazon.com/codebuild/latest/userguide/vpc-support.html).
2830

2931
`notifications` creates a [CodeStar notification](https://docs.aws.amazon.com/dtconsole/latest/userguide/welcome.html) for the pipeline. `sns_topic` is the SNS topic arn. `events` are the [notification events](https://docs.aws.amazon.com/dtconsole/latest/userguide/concepts.html#events-ref-pipeline). `detail_type` is either BASIC or FULL. The SNS topic must allow [codestar-notifications.amazonaws.com to publush to the topic](https://docs.aws.amazon.com/dtconsole/latest/userguide/notification-target-create.html).

modules/codebuild/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ resource "aws_codebuild_project" "this" {
3333

3434
source {
3535
type = "CODEPIPELINE"
36-
buildspec = file("${path.module}/buildspecs/${var.build_spec}")
36+
buildspec = var.build_spec
3737
git_clone_depth = 0
3838
insecure_ssl = false
3939
report_build_status = false

variables.tf

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ variable "build_timeout" {
3636
default = 10
3737
}
3838

39+
variable "build_override" {
40+
description = "Override CodeBuild images and buildspecs"
41+
type = object({
42+
plan_buildspec = optional(string)
43+
plan_image = optional(string)
44+
apply_buildspec = optional(string)
45+
apply_image = optional(string)
46+
})
47+
default = {}
48+
}
49+
3950
variable "checkov_skip" {
4051
description = "list of checkov checks to skip"
4152
type = list(string)
@@ -65,22 +76,18 @@ variable "detect_changes" {
6576
default = false
6677
}
6778

79+
variable "kms_key" {
80+
description = "AWS KMS key ARN"
81+
type = string
82+
default = null
83+
}
84+
6885
variable "log_retention" {
6986
description = "CloudWatch log group retention, in days"
7087
type = number
7188
default = 90
7289
}
7390

74-
variable "notifications" {
75-
description = "SNS notification configuration"
76-
type = object({
77-
sns_topic = string
78-
events = list(string)
79-
detail_type = string
80-
})
81-
default = null
82-
}
83-
8491
variable "mode" {
8592
description = "pipeline execution mode"
8693
type = string
@@ -95,10 +102,14 @@ variable "mode" {
95102
}
96103
}
97104

98-
variable "kms_key" {
99-
description = "AWS KMS key ARN"
100-
type = string
101-
default = null
105+
variable "notifications" {
106+
description = "SNS notification configuration"
107+
type = object({
108+
sns_topic = string
109+
events = list(string)
110+
detail_type = string
111+
})
112+
default = null
102113
}
103114

104115
variable "tags" {

0 commit comments

Comments
 (0)