I am trying to set up an AWS CodeBuild project in Terraform:

resource "aws_codebuild_project" "example" {
  name          = "test-project"
  description   = "test_codebuild_project"
  build_timeout = "5"
  service_role  = "${aws_iam_role.example.arn}"

  artifacts {
    type      = "S3"
    location  = "${aws_s3_bucket.example.bucket}"
    name      = "example.zip"
    packaging = "ZIP"
    path      = "dev/api/${var.app_version}"

This example works fine, but it will only ever save the build artifact example.zip to the folder set when terraform apply was run, since ${var.app_version} doesn't change on subsequent runs of this build.

How do I achieve something that, for example, takes a version number from the package.json file in the repo and uses that for ${var.app_version}?


This should not be done from the terraform side. The AWS CodeBuild docs mention how to achieve something like this by using the buildspec, although you might be constrained by only using the env variables provided by CodeBuild to add the variation to the name.

version: 0.2         
      - rspec HelloWorld_spec.rb
    - '**/*'

You can find the extended explanation here: https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.