Skip to content

Commit

Permalink
Enabled var should enable/disable resource creation (#3)
Browse files Browse the repository at this point in the history
* conditionally create resources

* conditionally include data
  • Loading branch information
sarkis authored Jun 19, 2018
1 parent d5613ac commit 1a27713
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module "codepipeline_label" {
}

resource "aws_s3_bucket" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
bucket = "${module.codepipeline_label.id}"
acl = "private"
tags = "${module.codepipeline_label.tags}"
Expand All @@ -25,6 +26,7 @@ module "codepipeline_assume_label" {
}

resource "aws_iam_role" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${module.codepipeline_assume_label.id}"
assume_role_policy = "${data.aws_iam_policy_document.assume.json}"
}
Expand All @@ -47,11 +49,13 @@ data "aws_iam_policy_document" "assume" {
}

resource "aws_iam_role_policy_attachment" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
role = "${aws_iam_role.default.id}"
policy_arn = "${aws_iam_policy.default.arn}"
}

resource "aws_iam_policy" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${module.codepipeline_label.id}"
policy = "${data.aws_iam_policy_document.default.json}"
}
Expand Down Expand Up @@ -80,6 +84,7 @@ data "aws_iam_policy_document" "default" {
}

resource "aws_iam_role_policy_attachment" "s3" {
count = "${var.enabled == "true" ? 1 : 0}"
role = "${aws_iam_role.default.id}"
policy_arn = "${aws_iam_policy.s3.arn}"
}
Expand All @@ -95,11 +100,13 @@ module "codepipeline_s3_policy_label" {
}

resource "aws_iam_policy" "s3" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${module.codepipeline_s3_policy_label.id}"
policy = "${data.aws_iam_policy_document.s3.json}"
}

data "aws_iam_policy_document" "s3" {
count = "${var.enabled == "true" ? 1 : 0}"
statement {
sid = ""

Expand All @@ -120,6 +127,7 @@ data "aws_iam_policy_document" "s3" {
}

resource "aws_iam_role_policy_attachment" "codebuild" {
count = "${var.enabled == "true" ? 1 : 0}"
role = "${aws_iam_role.default.id}"
policy_arn = "${aws_iam_policy.codebuild.arn}"
}
Expand All @@ -135,6 +143,7 @@ module "codebuild_label" {
}

resource "aws_iam_policy" "codebuild" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${module.codebuild_label.id}"
policy = "${data.aws_iam_policy_document.codebuild.json}"
}
Expand All @@ -153,7 +162,7 @@ data "aws_iam_policy_document" "codebuild" {
}

module "build" {
source = "git::https://github.com/cloudposse/terraform-aws-codebuild.git?ref=tags/0.7.1"
source = "git::https://github.com/cloudposse/terraform-aws-codebuild.git?ref=tags/0.7.2"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
Expand All @@ -169,14 +178,17 @@ module "build" {
image_repo_name = "${var.image_repo_name}"
image_tag = "${var.image_tag}"
github_token = "${var.github_oauth_token}"
enabled = "${var.enabled}"
}

resource "aws_iam_role_policy_attachment" "codebuild_s3" {
count = "${var.enabled == "true" ? 1 : 0}"
role = "${module.build.role_arn}"
policy_arn = "${aws_iam_policy.s3.arn}"
}

resource "aws_codepipeline" "source_build_deploy" {
count = "${var.enabled == "true" ? 1 : 0}"
name = "${module.codepipeline_label.id}"
role_arn = "${aws_iam_role.default.arn}"

Expand Down
4 changes: 2 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ variable "name" {

variable "enabled" {
default = "true"
description = "Enable ``CodePipeline`` creation"
description = "Enable `CodePipeline` creation"
}

variable "ecs_cluster_name" {
Expand All @@ -41,7 +41,7 @@ variable "repo_name" {
}

variable "branch" {
description = "Branch of the GitHub repository, _e.g._ ``master``"
description = "Branch of the GitHub repository, _e.g._ `master`"
}

variable "build_image" {
Expand Down

0 comments on commit 1a27713

Please sign in to comment.