Skip to content

Commit 3f0528f

Browse files
authored
Add enabled flag (#17)
* add enabled flag
1 parent b0e5d5a commit 3f0528f

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

main.tf

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module "label" {
1414
}
1515

1616
resource "aws_s3_bucket" "cache_bucket" {
17-
count = "${var.cache_enabled == "true" ? 1 : 0}"
17+
count = "${var.enabled == "true" && var.cache_enabled == "true" ? 1 : 0}"
1818
bucket = "${local.cache_bucket_name_normalised}"
1919
acl = "private"
2020
force_destroy = true
@@ -54,7 +54,7 @@ locals {
5454
cache_def = {
5555
"true" = [{
5656
type = "S3"
57-
location = "${var.cache_enabled == "true" ? aws_s3_bucket.cache_bucket.0.bucket : "none" }"
57+
location = "${var.enabled == "true" && var.cache_enabled == "true" ? join("", aws_s3_bucket.cache_bucket.*.bucket) : "none" }"
5858
}]
5959

6060
"false" = []
@@ -65,6 +65,7 @@ locals {
6565
}
6666

6767
resource "aws_iam_role" "default" {
68+
count = "${var.enabled == "true" ? 1 : 0}"
6869
name = "${module.label.id}"
6970
assume_role_policy = "${data.aws_iam_policy_document.role.json}"
7071
}
@@ -87,13 +88,14 @@ data "aws_iam_policy_document" "role" {
8788
}
8889

8990
resource "aws_iam_policy" "default" {
91+
count = "${var.enabled == "true" ? 1 : 0}"
9092
name = "${module.label.id}"
9193
path = "/service-role/"
9294
policy = "${data.aws_iam_policy_document.permissions.json}"
9395
}
9496

9597
resource "aws_iam_policy" "default_cache_bucket" {
96-
count = "${var.cache_enabled == "true" ? 1 : 0}"
98+
count = "${var.enabled == "true" && var.cache_enabled == "true" ? 1 : 0}"
9799
name = "${module.label.id}-cache-bucket"
98100
path = "/service-role/"
99101
policy = "${data.aws_iam_policy_document.permissions_cache_bucket.json}"
@@ -124,6 +126,8 @@ data "aws_iam_policy_document" "permissions" {
124126
}
125127

126128
data "aws_iam_policy_document" "permissions_cache_bucket" {
129+
count = "${var.enabled == "true" ? 1 : 0}"
130+
127131
statement {
128132
sid = ""
129133

@@ -141,17 +145,19 @@ data "aws_iam_policy_document" "permissions_cache_bucket" {
141145
}
142146

143147
resource "aws_iam_role_policy_attachment" "default" {
148+
count = "${var.enabled == "true" ? 1 : 0}"
144149
policy_arn = "${aws_iam_policy.default.arn}"
145150
role = "${aws_iam_role.default.id}"
146151
}
147152

148153
resource "aws_iam_role_policy_attachment" "default_cache_bucket" {
149-
count = "${var.cache_enabled == "true" ? 1 : 0}"
154+
count = "${var.enabled == "true" && var.cache_enabled == "true" ? 1 : 0}"
150155
policy_arn = "${element(aws_iam_policy.default_cache_bucket.*.arn, count.index)}"
151156
role = "${aws_iam_role.default.id}"
152157
}
153158

154159
resource "aws_codebuild_project" "default" {
160+
count = "${var.enabled == "true" ? 1 : 0}"
155161
name = "${module.label.id}"
156162
service_role = "${aws_iam_role.default.arn}"
157163

outputs.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
output "project_name" {
2-
value = "${aws_codebuild_project.default.name}"
2+
value = "${join("", aws_codebuild_project.default.*.name)}"
33
}
44

55
output "project_id" {
6-
value = "${aws_codebuild_project.default.id}"
6+
value = "${join("", aws_codebuild_project.default.*.id)}"
77
}
88

99
output "role_arn" {
10-
value = "${aws_iam_role.default.id}"
10+
value = "${join("", aws_iam_role.default.*.id)}"
1111
}
1212

1313
output "cache_bucket_name" {
14-
value = "${var.cache_enabled == "true" ? aws_s3_bucket.cache_bucket.0.bucket : "UNSET" }"
14+
value = "${var.enabled == "true" && var.cache_enabled == "true" ? join("", aws_s3_bucket.cache_bucket.*.bucket) : "UNSET" }"
1515
}

variables.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
variable "namespace" {
2+
type = "string"
23
default = "global"
34
}
45

56
variable "stage" {
7+
type = "string"
68
default = "default"
79
}
810

911
variable "name" {
12+
type = "string"
1013
default = "codebuild"
1114
}
1215

@@ -21,31 +24,43 @@ variable "environment_variables" {
2124
description = "A list of maps, that contain both the key 'name' and the key 'value' to be used as additional environment variables for the build."
2225
}
2326

27+
variable "enabled" {
28+
type = "string"
29+
default = "true"
30+
description = "A boolean to enable/disable resource creation."
31+
}
32+
2433
variable "cache_enabled" {
34+
type = "string"
2535
default = "true"
2636
description = "If cache_enabled is true, create an S3 bucket for storing codebuild cache inside"
2737
}
2838

2939
variable "cache_expiration_days" {
40+
type = "string"
3041
default = "7"
3142
description = "How many days should the build cache be kept."
3243
}
3344

3445
variable "cache_bucket_suffix_enabled" {
46+
type = "string"
3547
default = "true"
3648
description = "The cache bucket generates a random 13 character string to generate a unique bucket name. If set to false it uses terraform-null-label's id value"
3749
}
3850

3951
variable "build_image" {
52+
type = "string"
4053
default = "aws/codebuild/docker:1.12.1"
4154
description = "Docker image for build environment, e.g. 'aws/codebuild/docker:1.12.1' or 'aws/codebuild/eb-nodejs-6.10.0-amazonlinux-64:4.0.0'. For more info: http://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref.html"
4255
}
4356

4457
variable "build_compute_type" {
58+
type = "string"
4559
default = "BUILD_GENERAL1_SMALL"
4660
}
4761

4862
variable "buildspec" {
63+
type = "string"
4964
default = ""
5065
description = "Optional buildspec declaration to use for building the project"
5166
}
@@ -66,11 +81,13 @@ variable "tags" {
6681
}
6782

6883
variable "privileged_mode" {
84+
type = "string"
6985
default = "false"
7086
description = "(Optional) If set to true, enables running the Docker daemon inside a Docker container on the CodeBuild instance. Used when building Docker images"
7187
}
7288

7389
variable "github_token" {
90+
type = "string"
7491
default = ""
7592
description = "(Optional) GitHub auth token environment variable (`GITHUB_TOKEN`)"
7693
}

0 commit comments

Comments
 (0)