Skip to content

Commit 2055d9b

Browse files
authored
Add environment_variables for building Docker images and pushing to ECR (#7)
* Add `environment_variables` for building `Docker` images and pushing to `AWS ECR` * Add description to `variables.tf` * Update `README.md` * Update `README.md` * Update `variables.tf` * Use `data.aws_caller_identity.current.account_id` if `var.aws_account_id` is not provided * Use `data.aws_region.current.name` if `var.aws_region` is not provided * Rename `data sources` to `default` * Remove `default` from `variable "image"` * Rename `image` to `build_image` * Update `README.md` * Rename `instance_size` to `build_compute_type` * Remove `S3` policy resource from `README.md` since it's not `CodeBuild` specific * Update description * Add `default` and `description` for `variable "build_image"`
1 parent 0cbf9bb commit 2055d9b

File tree

3 files changed

+98
-30
lines changed

3 files changed

+98
-30
lines changed

README.md

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,52 @@ Include this repository as a module in your existing terraform code:
88

99
```
1010
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"
11+
source = "git::https://github.com/cloudposse/tf_codebuild.git?ref=tags/0.5.0"
12+
namespace = "general"
13+
name = "ci"
14+
stage = "staging"
15+
16+
# http://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref.html
17+
build_image = "aws/codebuild/docker:1.12.1"
18+
build_compute_type = "BUILD_GENERAL1_SMALL"
19+
20+
# These attributes are optional, used as ENV variables when building Docker images and pushing them to ECR
21+
# For more info:
22+
# http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html
23+
# https://www.terraform.io/docs/providers/aws/r/codebuild_project.html
24+
25+
privileged_mode = true
26+
aws_region = "us-east-1"
27+
aws_account_id = "xxxxxxxxxx"
28+
image_repo_name = "ecr-repo-name"
29+
image_tag = "latest"
1830
}
1931
```
2032

21-
Grant appropriate permsissions to s3
2233

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-
```
2934

3035
## Input
3136

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-
| buildspec | "" | Optional buildspec declaration to use for building the project |
37+
| Name | Default | Description |
38+
|:-------------------:|:----------------------------:|:----------------------------------------------------------------------------------------------------------------------------------------------------:|
39+
| namespace | global | Namespace |
40+
| stage | default | Stage |
41+
| name | codebuild | Name |
42+
| build_image | aws/codebuild/docker:1.12.1 | 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` |
43+
| build_compute_type | BUILD_GENERAL1_SMALL | `CodeBuild` instance size. Possible values are: ```BUILD_GENERAL1_SMALL``` ```BUILD_GENERAL1_MEDIUM``` ```BUILD_GENERAL1_LARGE``` |
44+
| buildspec | "" | (Optional) `buildspec` declaration to use for building the project |
45+
| privileged_mode | "" | (Optional) If set to true, enables running the Docker daemon inside a Docker container on the `CodeBuild` instance. Used when building Docker images |
46+
| aws_region | "" | (Optional) AWS Region, _e.g._ `us-east-1`. Used as `CodeBuild` ENV variable when building Docker images |
47+
| aws_account_id | "" | (Optional) AWS Account ID. Used as `CodeBuild` ENV variable when building Docker images |
48+
| image_repo_name | "" | (Optional) ECR repository name to store the Docker image built by this module. Used as `CodeBuild` ENV variable when building Docker images |
49+
| image_tag | "" | (Optional) Docker image tag in the ECR repository, _e.g._ `latest`. Used as `CodeBuild` ENV variable when building Docker images |
50+
51+
4052

4153
## Output
4254

4355
| Name | Decription |
4456
|:------------:|:----------------------:|
4557
| project_name | CodeBuild project name |
46-
| project_id | CodeBuild project arn |
47-
| role_arn | IAM Role arn |
58+
| project_id | CodeBuild project ARN |
59+
| role_arn | IAM Role ARN |

main.tf

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
data "aws_caller_identity" "default" {}
2+
3+
data "aws_region" "default" {
4+
current = true
5+
}
6+
17
# Define composite variables for resources
28
module "label" {
39
source = "git::https://github.com/cloudposse/tf_label.git?ref=tags/0.2.0"
@@ -75,10 +81,30 @@ resource "aws_codebuild_project" "default" {
7581
}
7682

7783
environment {
78-
compute_type = "${var.instance_size}"
79-
image = "${var.image}"
84+
compute_type = "${var.build_compute_type}"
85+
image = "${var.build_image}"
8086
type = "LINUX_CONTAINER"
81-
privileged_mode = true
87+
privileged_mode = "${var.privileged_mode}"
88+
89+
environment_variable {
90+
"name" = "AWS_REGION"
91+
"value" = "${signum(length(var.aws_region)) == 1 ? var.aws_region : data.aws_region.default.name}"
92+
}
93+
94+
environment_variable {
95+
"name" = "AWS_ACCOUNT_ID"
96+
"value" = "${signum(length(var.aws_account_id)) == 1 ? var.aws_account_id : data.aws_caller_identity.default.account_id}"
97+
}
98+
99+
environment_variable {
100+
"name" = "IMAGE_REPO_NAME"
101+
"value" = "${var.image_repo_name}"
102+
}
103+
104+
environment_variable {
105+
"name" = "IMAGE_TAG"
106+
"value" = "${var.image_tag}"
107+
}
82108
}
83109

84110
source {

variables.tf

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ variable "name" {
1010
default = "codebuild"
1111
}
1212

13-
variable "image" {
14-
default = "alpine"
13+
variable "build_image" {
14+
default = "aws/codebuild/docker:1.12.1"
15+
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"
1516
}
1617

17-
variable "instance_size" {
18+
variable "build_compute_type" {
1819
default = "BUILD_GENERAL1_SMALL"
1920
}
2021

@@ -37,3 +38,32 @@ variable "tags" {
3738
type = "map"
3839
default = {}
3940
}
41+
42+
variable "privileged_mode" {
43+
default = false
44+
description = "(Optional) If set to true, enables running the Docker daemon inside a Docker container on the CodeBuild instance. Used when building Docker images"
45+
}
46+
47+
variable "aws_region" {
48+
type = "string"
49+
default = ""
50+
description = "(Optional) AWS Region, e.g. us-east-1. Used as CodeBuild ENV variable when building Docker images. For more info: http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html"
51+
}
52+
53+
variable "aws_account_id" {
54+
type = "string"
55+
default = ""
56+
description = "(Optional) AWS Account ID. Used as CodeBuild ENV variable when building Docker images. For more info: http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html"
57+
}
58+
59+
variable "image_repo_name" {
60+
type = "string"
61+
default = ""
62+
description = "(Optional) ECR repository name to store the Docker image built by this module. Used as CodeBuild ENV variable when building Docker images. For more info: http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html"
63+
}
64+
65+
variable "image_tag" {
66+
type = "string"
67+
default = ""
68+
description = "(Optional) Docker image tag in the ECR repository, e.g. 'latest'. Used as CodeBuild ENV variable when building Docker images. For more info: http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html"
69+
}

0 commit comments

Comments
 (0)