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
) optionalName of the S3 bucket where s3 access log will be sent to
Default value:
""
artifact_location
(string
) optionalLocation of artifact. Applies only for artifact of type S3
Default value:
""
artifact_type
(string
) optionalThe 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
) optionalGenerates a publicly-accessible URL for the projects build badge. Available as badge_url attribute when enabled
Default value:
false
build_compute_type
(string
) optionalInstance type of the build instance
Default value:
"BUILD_GENERAL1_SMALL"
build_image
(string
) optionalDocker 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
) optionalType 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
) optionalHow 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
) optionalThe type of build environment, e.g. 'LINUX_CONTAINER' or 'WINDOWS_CONTAINER'
Default value:
"LINUX_CONTAINER"
buildspec
(string
) optionalOptional buildspec declaration to use for building the project
Default value:
""
cache_bucket_suffix_enabled
(bool
) optionalThe 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
) optionalHow many days should the build cache be kept. It only works when cache_type is 'S3'
Default value:
7
cache_type
(string
) optionalThe 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
) optionalSpecify 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)
) optionalJSON encoded IAM policy to add to the IAM service account permissions.
Default value:
[ ]
default_permissions_enabled
(bool
) optionalWhen '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 thecustom_policy
variable.Default value:
true
description
(string
) optionalShort description of the CodeBuild project
Default value:
"Managed by Terraform"
encryption_enabled
(bool
) optionalWhen set to 'true' the resource will have AES256 encryption enabled by default
Default value:
false
encryption_key
(string
) optionalAWS 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
optionalA 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)
) optionalList 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
) optionalIf set to true, fetches Git submodules for the AWS CodeBuild build project.
Default value:
false
file_system_locations
(any
) optionalA set of file system locations to to mount inside the build. File system locations are documented below.
Default value:
{ }
git_clone_depth
(number
) optionalTruncate 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
) optionalStorage type of GITHUB_TOKEN environment variable (
PARAMETER_STORE
,PLAINTEXT
,SECRETS_MANAGER
)Default value:
"PARAMETER_STORE"
iam_permissions_boundary
(string
) optionalARN of the policy that is used to set the permissions boundary for the role.
Default value:
null
iam_policy_path
(string
) optionalPath to the policy.
Default value:
"/service-role/"
iam_role_path
(string
) optionalPath 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)
) optionalSpecifies 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
) optionalConfiguration for the builds to store log data to CloudWatch or S3.
Default value:
{ }
private_repository
(bool
) optionalSet 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
) optionalSet 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
) optionalUse an existing s3 bucket name for cache. Relevant if
cache_type
is set toS3
.Default value:
null
secondary_artifact_encryption_enabled
(bool
) optionalSet to true to enable encryption on the secondary artifact bucket
Default value:
false
secondary_artifact_identifier
(string
) optionalSecondary artifact identifier. Must match the identifier in the build spec
Default value:
null
secondary_artifact_location
(string
) optionalLocation 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
) optionalThe type of authentication used to connect to a GitHub, GitHub Enterprise, or Bitbucket repository.
Default value:
"PERSONAL_ACCESS_TOKEN"
source_credential_server_type
(string
) optionalThe source provider used for this project.
Default value:
"GITHUB"
source_credential_token
(string
) optionalFor GitHub or GitHub Enterprise, this is the personal access token. For Bitbucket, this is the app password.
Default value:
""
source_credential_user_name
(string
) optionalThe 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
) optionalThe location of the source code from git or s3
Default value:
""
source_type
(string
) optionalThe 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
) optionalA version of the build input to be built for this project. If not specified, the latest version is used.
Default value:
""
versioning_enabled
(bool
) optionalA state of versioning. Versioning is a means of keeping multiple variants of an object in the same bucket
Default value:
true
vpc_config
(any
) optionalConfiguration 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.
context.tf
file of this module and part of the terraform-null-label pattern.additional_tag_map
(map(string)
) optionalAdditional key-value pairs to add to each map in
tags_as_list_of_maps
. Not added totags
orid
.
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)
) optionalID element. Additional attributes (e.g.
workers
orcluster
) to add toid
,
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 thedelimiter
and treated as a single ID element.Required: No
Default value:
[ ]
context
(any
) optionalSingle object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables asnull
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
) optionalDelimiter 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
) optionalDescribe 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 isany
so the map values can later be enhanced to provide additional options.)
format
is a Terraform format string to be passed to theformat()
function.
labels
is a list of labels, in order, to pass toformat()
function.
Label values will be normalized before being passed toformat()
so they will be
identical to how they appear inid
.
Default is{}
(descriptors
output will be empty).Required: No
Default value:
{ }
enabled
(bool
) optionalSet to false to prevent the module from creating any resources
Required: NoDefault value:
null
environment
(string
) optionalID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'
Required: NoDefault value:
null
id_length_limit
(number
) optionalLimit
id
to this many characters (minimum 6).
Set to0
for unlimited length.
Set tonull
for keep the existing setting, which defaults to0
.
Does not affectid_full
.Required: No
Default value:
null
label_key_case
(string
) optionalControls the letter case of the
tags
keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via thetags
input.
Possible values:lower
,title
,upper
.
Default value:title
.Required: No
Default value:
null
label_order
(list(string)
) optionalThe 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
) optionalControls 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 thetags
input.
Possible values:lower
,title
,upper
andnone
(no transformation).
Set this totitle
and setdelimiter
to""
to yield Pascal Case IDs.
Default value:lower
.Required: No
Default value:
null
labels_as_tags
(set(string)
) optionalSet 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 thetags
output.
Set to[]
to suppress all generated tags.
Notes:
The value of thename
tag, if included, will be theid
, not thename
.
Unlike othernull-label
inputs, the initial setting oflabels_as_tags
cannot be
changed in later chained modules. Attempts to change it will be silently ignored.Required: No
Default value:
[
"default"
]name
(string
) optionalID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as atag
.
The "name" tag is set to the fullid
string. There is no tag with the value of thename
input.Required: No
Default value:
null
namespace
(string
) optionalID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique
Required: NoDefault value:
null
regex_replace_chars
(string
) optionalTerraform 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
) optionalID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'
Required: NoDefault value:
null
tags
(map(string)
) optionalAdditional 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
) optionalID element (Rarely used, not included by default). A customer identifier, indicating who this instance of a resource is for
Required: NoDefault 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
Name | Version | Source | Description |
---|---|---|---|
this | 0.25.0 | cloudposse/label/null | n/a |
Resources
The following resources are used by this module:
aws_codebuild_project.default
(resource)aws_codebuild_source_credential.authorization
(resource)aws_iam_policy.default
(resource)aws_iam_policy.default_cache_bucket
(resource)aws_iam_role.default
(resource)aws_iam_role_policy_attachment.default
(resource)aws_iam_role_policy_attachment.default_cache_bucket
(resource)aws_s3_bucket.cache_bucket
(resource)aws_s3_bucket_acl.default
(resource)aws_s3_bucket_lifecycle_configuration.default
(resource)aws_s3_bucket_logging.default
(resource)aws_s3_bucket_ownership_controls.s3_bucket_acl_ownership
(resource)aws_s3_bucket_public_access_block.default
(resource)aws_s3_bucket_server_side_encryption_configuration.default
(resource)aws_s3_bucket_versioning.default
(resource)random_string.bucket_prefix
(resource)
Data Sources
The following data sources are used by this module:
aws_caller_identity.default
(data source)aws_iam_policy_document.combined_permissions
(data source)aws_iam_policy_document.permissions
(data source)aws_iam_policy_document.permissions_cache_bucket
(data source)aws_iam_policy_document.role
(data source)aws_iam_policy_document.vpc_permissions
(data source)aws_region.default
(data source)aws_s3_bucket.secondary_artifact
(data source)