gpt4 book ai didi

amazon-web-services - 如何将 aws_elastic_beanstalk_environment 设置传递给 Terraform 模块

转载 作者:行者123 更新时间:2023-12-04 15:31:49 24 4
gpt4 key购买 nike

我正在使用 Terraform 模块来管理 AWS Elastic Beanstalk 应用程序和环境,并希望将环境变量列表传递给模块。

由于缺乏更好的解决方案,我目前正在传入一个名称和值的平面列表,并声明一个固定数量的 setting节(见下文)。这似乎有效,除非当然有人会传递比我预期更多的环境变量。

那么 - 有没有更好的方法来实现这一目标?

# file: main.tf
variable env_vars {
default = ["FIRST_ENV_VAR", "1", "SECOND_ENV_VAR", "2"]
}

provider "aws" {
region = "eu-central-1"
}

module "beanstalk-app" {
source = "./beanstalk"
env_vars = "${var.env_vars}"
}

# file: beanstalk/main.tf
variable "env_vars" {
type = "list"
}

resource "aws_elastic_beanstalk_application" "app" {
name = "myapp"
}

resource "aws_elastic_beanstalk_environment" "env" {
name = "myapp-env"
application = "${aws_elastic_beanstalk_application.app.name}"
solution_stack_name = "64bit Amazon Linux 2016.03 v2.1.3 running Tomcat 8 Java 8"

setting {
namespace = "aws:elasticbeanstalk:application:environment"
name = "${element(var.env_vars, 0)}"
value = "${element(var.env_vars, 1)}"
}

setting {
namespace = "aws:elasticbeanstalk:application:environment"
name = "${element(var.env_vars, 2)}"
value = "${element(var.env_vars, 3)}"
}

setting {
namespace = "aws:elasticbeanstalk:application:environment"
name = "${element(var.env_vars, 4)}"
value = "${element(var.env_vars, 5)}"
}
}

最佳答案

在 HCL 中,重复的对象 block 等价于列表(参见 here
.因此,您可以将变量(映射列表)传递给 settings .

variable "settings" {
type = "list"
default = [
{
namespace = "aws:elasticbeanstalk:application:environment"
name = "FOO"
value = "BAR"
},
{
namespace = "aws:elasticbeanstalk:application:environment"
name = "BAZ"
value = "HAZ"
},
]
}

resource "aws_elastic_beanstalk_environment" "env" {
name = "myapp-env"
application = "${aws_elastic_beanstalk_application.app.name}"
solution_stack_name = "64bit Amazon Linux 2016.03 v2.1.3 running Tomcat 8 Java 8"
setting = ["${var.settings}"]
}

关于amazon-web-services - 如何将 aws_elastic_beanstalk_environment 设置传递给 Terraform 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39185178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com