如何使用 Terraform 为 Elastic Beanstalk 环境定义条件(每个环境)设置?
Posted
技术标签:
【中文标题】如何使用 Terraform 为 Elastic Beanstalk 环境定义条件(每个环境)设置?【英文标题】:How can one define a conditional (per-environment) setting for Elastic Beanstalk environment with Terraform? 【发布时间】:2022-01-07 01:14:31 【问题描述】:期望的结果
我想基于另一个变量 environment
有条件地为 aws_elastic_beanstalk_environment
Terraform 资源定义 setting
块。
当前尝试
对于这种情况,我通常会使用count
:
resource "aws_elastic_beanstalk_environment" "backend_prod"
name = "backend-$var.env"
application = aws_elastic_beanstalk_application.backend.name
solution_stack_name = "64bit Amazon Linux 2 v3.4.9 running Docker"
wait_for_ready_timeout = "10m"
# other settings omitted
setting
count = var.environment == "prod" ? 1 : 0
namespace = "aws:elasticbeanstalk:application:environment"
name = "API_KEY"
value = var.api_key
目前的结果
不幸的是,每当我运行 terraform plan
时,都会导致:
An argument named "count" is not expected here.
【问题讨论】:
【参考方案1】:您可以使用dynamic block:
resource "aws_elastic_beanstalk_environment" "backend_prod"
name = "backend-$var.env"
application = aws_elastic_beanstalk_application.backend.name
solution_stack_name = "64bit Amazon Linux 2 v3.4.9 running Docker"
wait_for_ready_timeout = "10m"
# other settings omitted
dynamic "setting"
for_each = var.environment == "prod" ? [1] : []
content
namespace = "aws:elasticbeanstalk:application:environment"
name = "API_KEY"
value = var.api_key
【讨论】:
以上是关于如何使用 Terraform 为 Elastic Beanstalk 环境定义条件(每个环境)设置?的主要内容,如果未能解决你的问题,请参考以下文章
使用 terraform 为 Elastic Beanstalk 启用托管更新
使用 Terraform 关闭/打开 Elastic Beanstalk 负载均衡器?
无法使用 Terraform 在自定义 VPC 中创建 Elastic Beanstalk 应用程序