Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Environment Variables in AWS Codebuild with Terraform

I am using Terraform to provision AWS CodeBuild. In the environment section, I have configured the following:

  environment {
    compute_type                = "BUILD_GENERAL1_SMALL"
    image                       = "aws/codebuild/standard:3.0"
    type                        = "LINUX_CONTAINER"
    image_pull_credentials_type = "CODEBUILD"

    environment_variable {
      name  = "SOME_KEY1"
      value = "SOME_VALUE1"
    }

    environment_variable {
      name  = "SOME_KEY2"
      value = "SOME_VALUE2"
    }

  }

I have more than 20 environment variables to configure in my Codebuild Project.

Is it possible to create a list and define a single environment_variable parameter to configure all environment variables?

like image 410
Deependra Dangal Avatar asked Apr 19 '26 05:04

Deependra Dangal


1 Answers

You could achieve this by using dynamic blocks.

variable "env_vars" {
  default = {
    SOME_KEY1 = "SOME_VALUE1"
    SOME_KEY2 = "SOME_VALUE2"
  }
} 

resource "aws_codebuild_project" "test" {
  # ...

  environment {
    compute_type                = "BUILD_GENERAL1_SMALL"
    image                       = "aws/codebuild/standard:3.0"
    type                        = "LINUX_CONTAINER"
    image_pull_credentials_type = "CODEBUILD"

    dynamic "environment_variable" {
      for_each = var.env_vars
      content {
        name  = environment_variable.key
        value = environment_variable.value
      }
    }
  }
}

This will loop over the map of env_vars set in the locals here (but could be passed as a variable) and create an environment_variable block for each, setting the name to the key of the map and the value to the value of the map.

like image 135
ydaetskcoR Avatar answered Apr 23 '26 19:04

ydaetskcoR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!