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"
}
]
}

Requirements

NameVersion
terraform>= 1.3
aws>= 5.0
random>= 2.1

Providers

NameVersion
aws>= 5.0
random>= 2.1

Modules

NameSourceVersion
thiscloudposse/label/null0.25.0

Resources

NameType
aws_codebuild_project.defaultresource
aws_codebuild_source_credential.authorizationresource
aws_iam_policy.defaultresource
aws_iam_policy.default_cache_bucketresource
aws_iam_role.defaultresource
aws_iam_role_policy_attachment.defaultresource
aws_iam_role_policy_attachment.default_cache_bucketresource
aws_s3_bucket.cache_bucketresource
aws_s3_bucket_acl.defaultresource
aws_s3_bucket_lifecycle_configuration.defaultresource
aws_s3_bucket_logging.defaultresource
aws_s3_bucket_ownership_controls.s3_bucket_acl_ownershipresource
aws_s3_bucket_public_access_block.defaultresource
aws_s3_bucket_server_side_encryption_configuration.defaultresource
aws_s3_bucket_versioning.defaultresource
random_string.bucket_prefixresource
aws_caller_identity.defaultdata source
aws_iam_policy_document.combined_permissionsdata source
aws_iam_policy_document.permissionsdata source
aws_iam_policy_document.permissions_cache_bucketdata source
aws_iam_policy_document.roledata source
aws_iam_policy_document.vpc_permissionsdata source
aws_region.defaultdata source
aws_s3_bucket.secondary_artifactdata source

Inputs

NameDescriptionTypeDefaultRequired
access_log_bucket_nameName of the S3 bucket where s3 access log will be sent tostring""no
additional_tag_mapAdditional 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.
map(string){}no
artifact_locationLocation of artifact. Applies only for artifact of type S3string""no
artifact_typeThe build output artifact's type. Valid values for this parameter are: CODEPIPELINE, NO_ARTIFACTS or S3string"CODEPIPELINE"no
attributesID 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.
list(string)[]no
aws_account_id(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.htmlstring""no
aws_region(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.htmlstring""no
badge_enabledGenerates a publicly-accessible URL for the projects build badge. Available as badge_url attribute when enabledboolfalseno
build_compute_typeInstance type of the build instancestring"BUILD_GENERAL1_SMALL"no
build_imageDocker 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.htmlstring"aws/codebuild/standard:2.0"no
build_image_pull_credentials_typeType 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.string"CODEBUILD"no
build_timeoutHow 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 completednumber60no
build_typeThe type of build environment, e.g. 'LINUX_CONTAINER' or 'WINDOWS_CONTAINER'string"LINUX_CONTAINER"no
buildspecOptional buildspec declaration to use for building the projectstring""no
cache_bucket_suffix_enabledThe 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 'S3booltrueno
cache_expiration_daysHow many days should the build cache be kept. It only works when cache_type is 'S3'number7no
cache_typeThe 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 insidestring"NO_CACHE"no
concurrent_build_limitSpecify 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.numbernullno
contextSingle 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.
any
{
"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
}
no
custom_policyJSON encoded IAM policy to add to the IAM service account permissions.list(string)[]no
default_permissions_enabledWhen '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.booltrueno
delimiterDelimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.
stringnullno
descriptionShort description of the CodeBuild projectstring"Managed by Terraform"no
descriptor_formatsDescribe 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).
any{}no
enabledSet to false to prevent the module from creating any resourcesboolnullno
encryption_enabledWhen set to 'true' the resource will have AES256 encryption enabled by defaultboolfalseno
encryption_keyAWS Key Management Service (AWS KMS) customer master key (CMK) to be used for encrypting the build project's build output artifacts.stringnullno
environmentID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'stringnullno
environment_variablesA 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'
list(object(
{
name = string
value = string
type = string
}
))
[
{
"name": "NO_ADDITIONAL_BUILD_VARS",
"type": "PLAINTEXT",
"value": "TRUE"
}
]
no
extra_permissionsList of action strings which will be added to IAM service account permissions. Only used if default_permissions_enabled is set to true.list(string)[]no
fetch_git_submodulesIf set to true, fetches Git submodules for the AWS CodeBuild build project.boolfalseno
file_system_locationsA set of file system locations to to mount inside the build. File system locations are documented below.any{}no
git_clone_depthTruncate git history to this many commits.numbernullno
github_token(Optional) GitHub auth token environment variable (GITHUB_TOKEN)string""no
github_token_typeStorage type of GITHUB_TOKEN environment variable (PARAMETER_STORE, PLAINTEXT, SECRETS_MANAGER)string"PARAMETER_STORE"no
iam_permissions_boundaryARN of the policy that is used to set the permissions boundary for the role.stringnullno
iam_policy_pathPath to the policy.string"/service-role/"no
iam_role_pathPath to the role.stringnullno
id_length_limitLimit 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.
numbernullno
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. For more info: http://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.htmlstring"UNSET"no
image_tag(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.htmlstring"latest"no
label_key_caseControls 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.
stringnullno
label_orderThe 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.
list(string)nullno
label_value_caseControls 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.
stringnullno
labels_as_tagsSet 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.
set(string)
[
"default"
]
no
local_cache_modesSpecifies settings that AWS CodeBuild uses to store and reuse build dependencies. Valid values: LOCAL_SOURCE_CACHE, LOCAL_DOCKER_LAYER_CACHE, and LOCAL_CUSTOM_CACHElist(string)[]no
logs_configConfiguration for the builds to store log data to CloudWatch or S3.any{}no
nameID 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.
stringnullno
namespaceID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally uniquestringnullno
private_repositorySet to true to login into private repository with credentials supplied in source_credential variable.boolfalseno
privileged_mode(Optional) If set to true, enables running the Docker daemon inside a Docker container on the CodeBuild instance. Used when building Docker imagesboolfalseno
regex_replace_charsTerraform 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.
stringnullno
report_build_statusSet 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 GITHUBboolfalseno
s3_cache_bucket_nameUse an existing s3 bucket name for cache. Relevant if cache_type is set to S3.stringnullno
secondary_artifact_encryption_enabledSet to true to enable encryption on the secondary artifact bucketboolfalseno
secondary_artifact_identifierSecondary artifact identifier. Must match the identifier in the build specstringnullno
secondary_artifact_locationLocation of secondary artifact. Must be an S3 referencestringnullno
secondary_sources(Optional) secondary source for the codebuild project in addition to the primary location
list(object(
{
git_clone_depth = number
location = string
source_identifier = string
type = string
fetch_submodules = bool
insecure_ssl = bool
report_build_status = bool
}))
[]no
source_credential_auth_typeThe type of authentication used to connect to a GitHub, GitHub Enterprise, or Bitbucket repository.string"PERSONAL_ACCESS_TOKEN"no
source_credential_server_typeThe source provider used for this project.string"GITHUB"no
source_credential_tokenFor GitHub or GitHub Enterprise, this is the personal access token. For Bitbucket, this is the app password.string""no
source_credential_user_nameThe Bitbucket username when the authType is BASIC_AUTH. This parameter is not valid for other types of source providers or connections.string""no
source_locationThe location of the source code from git or s3string""no
source_typeThe type of repository that contains the source code to be built. Valid values for this parameter are: CODECOMMIT, CODEPIPELINE, GITHUB, GITHUB_ENTERPRISE, BITBUCKET or S3string"CODEPIPELINE"no
source_versionA version of the build input to be built for this project. If not specified, the latest version is used.string""no
stageID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'stringnullno
tagsAdditional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.
map(string){}no
tenantID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is forstringnullno
versioning_enabledA state of versioning. Versioning is a means of keeping multiple variants of an object in the same bucketbooltrueno
vpc_configConfiguration for the builds to run inside a VPC.any{}no

Outputs

NameDescription
badge_urlThe URL of the build badge when badge_enabled is enabled
cache_bucket_arnCache S3 bucket ARN
cache_bucket_nameCache S3 bucket name
project_arnProject ARN
project_idProject ID
project_nameProject name
role_arnIAM Role ARN
role_idIAM Role ID