Skip to content

Commit 2a23e93

Browse files
authored
Implement AWS CodeBuild (#1)
* Initial * Remove provider vars * Added outputs * Fix hardcoded values * Added default image * Grant perms * Grant perms * Fix bump version * Added readme * Update README.md * fmt * fmt * Added module param * Update README.md * Update readme * Readme fix
1 parent 8013b4b commit 2a23e93

File tree

4 files changed

+156
-1
lines changed

4 files changed

+156
-1
lines changed

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
1-
# tf_codebuild
1+
# tf_codebuild
2+
3+
Terraform config to create codebuild project for codepipeline
4+
5+
## Usage
6+
7+
Include this repository as a module in your existing terraform code:
8+
9+
```
10+
module "build" {
11+
source = "git::https://github.com/cloudposse/tf_codebuild.git"
12+
namespace = "general"
13+
name = "ci"
14+
stage = "staging"
15+
16+
image = "apline"
17+
instance_size = "BUILD_GENERAL1_SMALL"
18+
}
19+
```
20+
21+
Grant appropriate permsissions to s3
22+
23+
```
24+
resource "aws_iam_role_policy_attachment" "codebuild_s3" {
25+
role = "${module.build.role_arn}"
26+
policy_arn = "${aws_iam_policy.s3.arn}"
27+
}
28+
```
29+
30+
## Input
31+
32+
| Name | Default | Decription |
33+
|:-------------:|:--------------------:|:------------------------------------------------------------------------------------------------------------------------------:|
34+
| namespace | global | Namespace |
35+
| stage | default | Stage |
36+
| name | codebuild | Name |
37+
| image | alpine | Docker image used as environment |
38+
| instance_size | BUILD_GENERAL1_SMALL | Instance size for job. Possible values are: ```BUILD_GENERAL1_SMALL``` ```BUILD_GENERAL1_MEDIUM``` ```BUILD_GENERAL1_LARGE``` |
39+
40+
## Output
41+
42+
| Name | Decription |
43+
|:------------:|:----------------------:|
44+
| project_name | CodeBuild project name |
45+
| project_id | CodeBuild project arn |
46+
| role_arn | IAM Role arn |

main.tf

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Define composite variables for resources
2+
module "label" {
3+
source = "git::https://github.com/cloudposse/tf_label.git?ref=tags/0.1.0"
4+
namespace = "${var.namespace}"
5+
name = "${var.name}"
6+
stage = "${var.stage}"
7+
}
8+
9+
resource "aws_iam_role" "default" {
10+
name = "${module.label.id}"
11+
assume_role_policy = "${data.aws_iam_policy_document.role.json}"
12+
}
13+
14+
data "aws_iam_policy_document" "role" {
15+
statement {
16+
sid = ""
17+
18+
actions = [
19+
"sts:AssumeRole",
20+
]
21+
22+
principals {
23+
type = "Service"
24+
identifiers = ["codebuild.amazonaws.com"]
25+
}
26+
27+
effect = "Allow"
28+
}
29+
}
30+
31+
resource "aws_iam_policy" "default" {
32+
name = "${module.label.id}"
33+
path = "/service-role/"
34+
policy = "${data.aws_iam_policy_document.logs.json}"
35+
}
36+
37+
data "aws_iam_policy_document" "logs" {
38+
statement {
39+
sid = ""
40+
41+
actions = [
42+
"logs:CreateLogGroup",
43+
"logs:CreateLogStream",
44+
"logs:PutLogEvents",
45+
]
46+
47+
effect = "Allow"
48+
49+
resources = [
50+
"*",
51+
]
52+
}
53+
}
54+
55+
resource "aws_iam_policy_attachment" "default" {
56+
name = "${module.label.id}"
57+
policy_arn = "${aws_iam_policy.default.arn}"
58+
roles = ["${aws_iam_role.default.id}"]
59+
}
60+
61+
resource "aws_codebuild_project" "default" {
62+
name = "${module.label.id}"
63+
service_role = "${aws_iam_role.default.arn}"
64+
65+
artifacts {
66+
type = "CODEPIPELINE"
67+
}
68+
69+
environment {
70+
compute_type = "${var.instance_size}"
71+
image = "${var.image}"
72+
type = "LINUX_CONTAINER"
73+
}
74+
75+
source {
76+
type = "CODEPIPELINE"
77+
}
78+
79+
tags = "${module.label.tags}"
80+
}

outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "project_name" {
2+
value = "${aws_codebuild_project.default.name}"
3+
}
4+
5+
output "project_id" {
6+
value = "${aws_codebuild_project.default.id}"
7+
}
8+
9+
output "role_arn" {
10+
value = "${aws_iam_role.default.id}"
11+
}

variables.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "namespace" {
2+
default = "global"
3+
}
4+
5+
variable "stage" {
6+
default = "default"
7+
}
8+
9+
variable "name" {
10+
default = "codebuild"
11+
}
12+
13+
variable "image" {
14+
default = "alpine"
15+
}
16+
17+
variable "instance_size" {
18+
default = "BUILD_GENERAL1_SMALL"
19+
}

0 commit comments

Comments
 (0)