使用 Terraform 的 AWS Codebuild 中的多个环境变量
Multiple Environment Variables in AWS Codebuild with Terraform
我正在使用 Terraform 来配置 AWS CodeBuild。在环境部分,我配置了以下内容:
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"
}
}
我的 Codebuild 项目中有 20 多个环境变量需要配置。
是否可以创建一个列表并定义单个 environment_variable 参数来配置所有环境变量?
您可以使用 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
}
}
}
}
这将遍历在本地设置的 env_vars
的地图(但可以作为变量传递)并为每个创建一个 environment_variable
块,将名称设置为键地图和地图的价值。
我正在使用 Terraform 来配置 AWS CodeBuild。在环境部分,我配置了以下内容:
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"
}
}
我的 Codebuild 项目中有 20 多个环境变量需要配置。
是否可以创建一个列表并定义单个 environment_variable 参数来配置所有环境变量?
您可以使用 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
}
}
}
}
这将遍历在本地设置的 env_vars
的地图(但可以作为变量传递)并为每个创建一个 environment_variable
块,将名称设置为键地图和地图的价值。