Skip to main content

Module: codebuild

Terraform module to create AWS CodeBuild project for AWS CodePipeline.

Usage

Include this module in your existing terraform code:

module "build" {
source = "cloudposse/codebuild/aws"
# Cloud Posse recommends pinning every module to a specific version
# version = "x.x.x"
namespace = "eg"
stage = "staging"
name = "app"

# https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html
build_image = "aws/codebuild/standard:2.0"
build_compute_type = "BUILD_GENERAL1_SMALL"
build_timeout = 60

# These attributes are optional, used as ENV variables when building Docker images and pushing them to ECR
# For more info:
# http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html
# https://www.terraform.io/docs/providers/aws/r/codebuild_project.html

privileged_mode = true
aws_region = "us-east-1"
aws_account_id = "xxxxxxxxxx"
image_repo_name = "ecr-repo-name"
image_tag = "latest"

# Optional extra environment variables
environment_variables = [
{
name = "JENKINS_URL"
value = "https://jenkins.example.com"
type = "PLAINTEXT"
},
{
name = "COMPANY_NAME"
value = "Amazon"
type = "PLAINTEXT"
},
{
name = "TIME_ZONE"
value = "Pacific/Auckland"
type = "PLAINTEXT"
}
]
}

Variables

Required Variables

Optional Variables

access_log_bucket_name (string) optional

Name of the S3 bucket where s3 access log will be sent to


Default value: ""

artifact_location (string) optional

Location of artifact. Applies only for artifact of type S3


Default value: ""

artifact_type (string) optional

The build output artifact's type. Valid values for this parameter are: CODEPIPELINE, NO_ARTIFACTS or S3


Default value: "CODEPIPELINE"

aws_account_id (string) optional

(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


Default value: ""

aws_region (string) optional

(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


Default value: ""

badge_enabled (bool) optional

Generates a publicly-accessible URL for the projects build badge. Available as badge_url attribute when enabled


Default value: false

build_compute_type (string) optional

Instance type of the build instance


Default value: "BUILD_GENERAL1_SMALL"

build_image (string) optional

Docker image for build environment, e.g. 'aws/codebuild/standard:2.0' 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


Default value: "aws/codebuild/standard:2.0"

build_image_pull_credentials_type (string) optional

Type of credentials AWS CodeBuild uses to pull images in your build.Valid values: CODEBUILD, SERVICE_ROLE. When you use a cross-account or private registry image, you must use SERVICE_ROLE credentials.


Default value: "CODEBUILD"

build_timeout (number) optional

How long in minutes, from 5 to 480 (8 hours), for AWS CodeBuild to wait until timing out any related build that does not get marked as completed


Default value: 60

build_type (string) optional

The type of build environment, e.g. 'LINUX_CONTAINER' or 'WINDOWS_CONTAINER'


Default value: "LINUX_CONTAINER"

buildspec (string) optional

Optional buildspec declaration to use for building the project


Default value: ""

cache_bucket_suffix_enabled (bool) optional

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. It only works when cache_type is 'S3


Default value: true

cache_expiration_days (number) optional

How many days should the build cache be kept. It only works when cache_type is 'S3'


Default value: 7

cache_type (string) optional

The type of storage that will be used for the AWS CodeBuild project cache. Valid values: NO_CACHE, LOCAL, and S3. Defaults to NO_CACHE. If cache_type is S3, it will create an S3 bucket for storing codebuild cache inside


Default value: "NO_CACHE"

concurrent_build_limit (number) optional

Specify a maximum number of concurrent builds for the project. The value specified must be greater than 0 and less than the account concurrent running builds limit.


Default value: null

custom_policy (list(string)) optional

JSON encoded IAM policy to add to the IAM service account permissions.


Default value: [ ]

default_permissions_enabled (bool) optional

When 'true' default base IAM permissions to get up and running with CodeBuild are attached. Those who want a least privileged policy can instead set to false and use the custom_policy variable.


Default value: true

description (string) optional

Short description of the CodeBuild project


Default value: "Managed by Terraform"

encryption_enabled (bool) optional

When set to 'true' the resource will have AES256 encryption enabled by default


Default value: false

encryption_key (string) optional

AWS Key Management Service (AWS KMS) customer master key (CMK) to be used for encrypting the build project's build output artifacts.


Default value: null

environment_variables optional

A list of maps, that contain the keys 'name', 'value', and 'type' to be used as additional environment variables for the build. Valid types are 'PLAINTEXT', 'PARAMETER_STORE', or 'SECRETS_MANAGER'


Type:

list(object(
{
name = string
value = string
type = string
}
))

Default value:

[
{
"name": "NO_ADDITIONAL_BUILD_VARS",
"type": "PLAINTEXT",
"value": "TRUE"
}
]
extra_permissions (list(string)) optional

List of action strings which will be added to IAM service account permissions. Only used if default_permissions_enabled is set to true.


Default value: [ ]

fetch_git_submodules (bool) optional

If set to true, fetches Git submodules for the AWS CodeBuild build project.


Default value: false

file_system_locations (any) optional

A set of file system locations to to mount inside the build. File system locations are documented below.


Default value: { }

git_clone_depth (number) optional

Truncate git history to this many commits.


Default value: null

github_token (string) optional

(Optional) GitHub auth token environment variable (GITHUB_TOKEN)


Default value: ""

github_token_type (string) optional

Storage type of GITHUB_TOKEN environment variable (PARAMETER_STORE, PLAINTEXT, SECRETS_MANAGER)


Default value: "PARAMETER_STORE"

iam_permissions_boundary (string) optional

ARN of the policy that is used to set the permissions boundary for the role.


Default value: null

iam_policy_path (string) optional

Path to the policy.


Default value: "/service-role/"

iam_role_path (string) optional

Path to the role.


Default value: null

image_repo_name (string) optional

(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


Default value: "UNSET"

image_tag (string) optional

(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


Default value: "latest"

local_cache_modes (list(string)) optional

Specifies settings that AWS CodeBuild uses to store and reuse build dependencies. Valid values: LOCAL_SOURCE_CACHE, LOCAL_DOCKER_LAYER_CACHE, and LOCAL_CUSTOM_CACHE


Default value: [ ]

logs_config (any) optional

Configuration for the builds to store log data to CloudWatch or S3.


Default value: { }

private_repository (bool) optional

Set to true to login into private repository with credentials supplied in source_credential variable.


Default value: false

privileged_mode (bool) optional

(Optional) If set to true, enables running the Docker daemon inside a Docker container on the CodeBuild instance. Used when building Docker images


Default value: false

report_build_status (bool) optional

Set to true to report the status of a build's start and finish to your source provider. This option is only valid when the source_type is BITBUCKET or GITHUB


Default value: false

s3_cache_bucket_name (string) optional

Use an existing s3 bucket name for cache. Relevant if cache_type is set to S3.


Default value: null

secondary_artifact_encryption_enabled (bool) optional

Set to true to enable encryption on the secondary artifact bucket


Default value: false

secondary_artifact_identifier (string) optional

Secondary artifact identifier. Must match the identifier in the build spec


Default value: null

secondary_artifact_location (string) optional

Location of secondary artifact. Must be an S3 reference


Default value: null

secondary_sources optional

(Optional) secondary source for the codebuild project in addition to the primary location


Type:

list(object(
{
git_clone_depth = number
location = string
source_identifier = string
type = string
fetch_submodules = bool
insecure_ssl = bool
report_build_status = bool
}))

Default value: [ ]

source_credential_auth_type (string) optional

The type of authentication used to connect to a GitHub, GitHub Enterprise, or Bitbucket repository.


Default value: "PERSONAL_ACCESS_TOKEN"

source_credential_server_type (string) optional

The source provider used for this project.


Default value: "GITHUB"

source_credential_token (string) optional

For GitHub or GitHub Enterprise, this is the personal access token. For Bitbucket, this is the app password.


Default value: ""

source_credential_user_name (string) optional

The Bitbucket username when the authType is BASIC_AUTH. This parameter is not valid for other types of source providers or connections.


Default value: ""

source_location (string) optional

The location of the source code from git or s3


Default value: ""

source_type (string) optional

The type of repository that contains the source code to be built. Valid values for this parameter are: CODECOMMIT, CODEPIPELINE, GITHUB, GITHUB_ENTERPRISE, BITBUCKET or S3


Default value: "CODEPIPELINE"

source_version (string) optional

A version of the build input to be built for this project. If not specified, the latest version is used.


Default value: ""

versioning_enabled (bool) optional

A state of versioning. Versioning is a means of keeping multiple variants of an object in the same bucket


Default value: true

vpc_config (any) optional

Configuration for the builds to run inside a VPC.


Default value: { }

Context Variables

The following variables are defined in the context.tf file of this module and part of the terraform-null-label pattern.

additional_tag_map (map(string)) optional

Additional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration.


Required: No

Default value: { }

attributes (list(string)) optional

ID element. Additional attributes (e.g. workers or cluster) to add to id,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the delimiter
and treated as a single ID element.


Required: No

Default value: [ ]

context (any) optional

Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as null to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional_tag_map, which are merged.


Required: No

Default value:

{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
delimiter (string) optional

Delimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.


Required: No

Default value: null

descriptor_formats (any) optional

Describe additional descriptors to be output in the descriptors output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
\{<br/> format = string<br/> labels = list(string)<br/> \}
(Type is any so the map values can later be enhanced to provide additional options.)
format is a Terraform format string to be passed to the format() function.
labels is a list of labels, in order, to pass to format() function.
Label values will be normalized before being passed to format() so they will be
identical to how they appear in id.
Default is {} (descriptors output will be empty).


Required: No

Default value: { }

enabled (bool) optional

Set to false to prevent the module from creating any resources
Required: No

Default value: null

environment (string) optional

ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'
Required: No

Default value: null

id_length_limit (number) optional

Limit id to this many characters (minimum 6).
Set to 0 for unlimited length.
Set to null for keep the existing setting, which defaults to 0.
Does not affect id_full.


Required: No

Default value: null

label_key_case (string) optional

Controls the letter case of the tags keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the tags input.
Possible values: lower, title, upper.
Default value: title.


Required: No

Default value: null

label_order (list(string)) optional

The order in which the labels (ID elements) appear in the id.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.


Required: No

Default value: null

label_value_case (string) optional

Controls the letter case of ID elements (labels) as included in id,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the tags input.
Possible values: lower, title, upper and none (no transformation).
Set this to title and set delimiter to "" to yield Pascal Case IDs.
Default value: lower.


Required: No

Default value: null

labels_as_tags (set(string)) optional

Set of labels (ID elements) to include as tags in the tags output.
Default is to include all labels.
Tags with empty values will not be included in the tags output.
Set to [] to suppress all generated tags.
Notes:
The value of the name tag, if included, will be the id, not the name.
Unlike other null-label inputs, the initial setting of labels_as_tags cannot be
changed in later chained modules. Attempts to change it will be silently ignored.


Required: No

Default value:

[
"default"
]
name (string) optional

ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a tag.
The "name" tag is set to the full id string. There is no tag with the value of the name input.


Required: No

Default value: null

namespace (string) optional

ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique
Required: No

Default value: null

regex_replace_chars (string) optional

Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits.


Required: No

Default value: null

stage (string) optional

ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'
Required: No

Default value: null

tags (map(string)) optional

Additional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.


Required: No

Default value: { }

tenant (string) optional

ID element (Rarely used, not included by default). A customer identifier, indicating who this instance of a resource is for
Required: No

Default value: null

Outputs

badge_url

The URL of the build badge when badge_enabled is enabled

cache_bucket_arn

Cache S3 bucket ARN

cache_bucket_name

Cache S3 bucket name

project_arn

Project ARN

project_id

Project ID

project_name

Project name

role_arn

IAM Role ARN

role_id

IAM Role ID

Dependencies

Requirements

  • terraform, version: >= 1.3
  • aws, version: >= 5.0
  • random, version: >= 2.1

Providers

  • aws, version: >= 5.0
  • random, version: >= 2.1

Modules

NameVersionSourceDescription
this0.25.0cloudposse/label/nulln/a

Resources

The following resources are used by this module:

Data Sources

The following data sources are used by this module: