Skip to content

Commit

Permalink
Add Support for CodeBuild (#3)
Browse files Browse the repository at this point in the history
* Added code buil

* Added code build

* Added code build

* Remove provider vars

* Added outputs

* Added outputs

* Added default image

* Grant perms to trigger build

* Grant perms

* Fix bump version

* Update main.tf

* Bind right versions
  • Loading branch information
goruha authored Aug 9, 2017
1 parent f79da29 commit 9bcf9ab
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 31 deletions.
133 changes: 102 additions & 31 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
# Define composite variables for resources
module "label" {
source = "git::https://github.com/cloudposse/tf_label.git"
source = "git::https://github.com/cloudposse/tf_label.git?ref=tags/0.1.0"
namespace = "${var.namespace}"
name = "${var.name}"
stage = "${var.stage}"
}


resource "aws_s3_bucket" "default" {
bucket = "${module.label.id}"
acl = "private"

tags {
Name = "${module.label.id}"
Namespace = "${var.namespace}"
Stage = "${var.stage}"
}
tags = "${module.label.tags}"
}

data "aws_iam_policy_document" "codepipeline" {
resource "aws_iam_role" "default" {
name = "${module.label.id}"

assume_role_policy = "${data.aws_iam_policy_document.assume.json}"
}

data "aws_iam_policy_document" "assume" {
statement {
sid = ""

actions = [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:GetBucketVersioning",
"s3:PutObject",
"sts:AssumeRole",
]

resources = [
"${aws_s3_bucket.default.arn}",
"${aws_s3_bucket.default.arn}/*",
"arn:aws:s3:::elasticbeanstalk*",
]
principals {
type = "Service"
identifiers = ["codepipeline.amazonaws.com"]
}

effect = "Allow"
}
}

resource "aws_iam_role_policy_attachment" "default" {
role = "${aws_iam_role.default.id}"
policy_arn = "${aws_iam_policy.default.arn}"
}

resource "aws_iam_policy" "default" {
name = "${module.label.id}"
policy = "${data.aws_iam_policy_document.default.json}"
}

data "aws_iam_policy_document" "default" {
statement {
sid = ""

Expand All @@ -61,33 +70,74 @@ data "aws_iam_policy_document" "codepipeline" {
}
}

data "aws_iam_policy_document" "assume" {
resource "aws_iam_role_policy_attachment" "s3" {
role = "${aws_iam_role.default.id}"
policy_arn = "${aws_iam_policy.s3.arn}"
}

resource "aws_iam_policy" "s3" {
name = "${module.label.id}-s3"
policy = "${data.aws_iam_policy_document.s3.json}"
}

data "aws_iam_policy_document" "s3" {
statement {
sid = ""

actions = [
"sts:AssumeRole",
"s3:GetObject",
"s3:GetObjectVersion",
"s3:GetBucketVersioning",
"s3:PutObject",
]

principals {
type = "Service"
identifiers = ["codepipeline.amazonaws.com"]
}
resources = [
"${aws_s3_bucket.default.arn}",
"${aws_s3_bucket.default.arn}/*",
"arn:aws:s3:::elasticbeanstalk*",
]

effect = "Allow"
}
}

resource "aws_iam_role" "default" {
name = "${module.label.id}"
resource "aws_iam_role_policy_attachment" "codebuild" {
role = "${aws_iam_role.default.id}"
policy_arn = "${aws_iam_policy.codebuild.arn}"
}

assume_role_policy = "${data.aws_iam_policy_document.assume.json}"

resource "aws_iam_policy" "codebuild" {
name = "${module.label.id}-codebuild"
policy = "${data.aws_iam_policy_document.codebuild.json}"
}

resource "aws_iam_role_policy" "codepipeline" {
name = "${module.label.id}"
role = "${aws_iam_role.default.id}"
policy = "${data.aws_iam_policy_document.codepipeline.json}"
data "aws_iam_policy_document" "codebuild" {
statement {
sid = ""

actions = [
"codebuild:*",
]

resources = ["${module.build.project_id}"]
effect = "Allow"
}
}

module "build" {
source = "git::https://github.com/cloudposse/tf_codebuild.git?ref=tags/0.1.0"
namespace = "${var.namespace}"
name = "${var.name}-build"
stage = "${var.stage}"

image = "${var.build_image}"
instance_size = "${var.build_instance_size}"
}

resource "aws_iam_role_policy_attachment" "codebuild_s3" {
role = "${module.build.role_arn}"
policy_arn = "${aws_iam_policy.s3.arn}"
}

resource "aws_codepipeline" "default" {
Expand Down Expand Up @@ -120,6 +170,25 @@ resource "aws_codepipeline" "default" {
}
}

stage {
name = "Build"

action {
name = "Compose"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
version = "1"

input_artifacts = ["code"]
output_artifacts = ["package"]

configuration {
ProjectName = "${module.build.project_name}"
}
}
}

stage {
name = "Deploy"

Expand All @@ -128,7 +197,7 @@ resource "aws_codepipeline" "default" {
category = "Deploy"
owner = "AWS"
provider = "ElasticBeanstalk"
input_artifacts = ["code"]
input_artifacts = ["package"]
version = "1"

configuration {
Expand All @@ -137,4 +206,6 @@ resource "aws_codepipeline" "default" {
}
}
}

tags = "${module.label.tags}"
}
8 changes: 8 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ variable "repo_owner" {}
variable "repo_name" {}

variable "branch" {}

variable "build_image" {
default = "alpine"
}

variable "build_instance_size" {
default = "BUILD_GENERAL1_SMALL"
}

0 comments on commit 9bcf9ab

Please sign in to comment.