简体   繁体   English

如何将aws_elastic_beanstalk_environment设置传递给Terraform模块

[英]How to pass aws_elastic_beanstalk_environment settings to a Terraform module

I'm using a Terraform module to manage AWS Elastic Beanstalk applications and environments, and want to pass a list of environment variables to the module. 我正在使用Terraform模块来管理AWS Elastic Beanstalk应用程序和环境,并希望将环境变量列表传递给模块。

For lack of a better solution, I'm currently passing in a flat list of names and values, and declare a fixed number of setting stanzas (see below). 由于缺乏更好的解决方案,我目前正在传递一个名单和值的平面列表,并声明固定数量的setting节(见下文)。 This seems to work, unless of course someone's going to pass in more environment variables than I expected. 这似乎有效,除非当然有人会传递比我预期更多的环境变量。

So - is there a better way to achieve this? 那么 - 有更好的方法来实现这一目标吗?

# 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)}"
  }
}

In HCL repeated object blocks are equivalent to a list (see here . Therefore you can pass a variable (list of maps) to settings . 在HCL中,重复的对象块相当于一个列表(请参见此处 。因此,您可以将变量(地图列表)传递给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}"]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Terraform aws_elastic_beanstalk_environment “错误:缺少必需的参数” - Terraform aws_elastic_beanstalk_environment “Error: Missing required argument” 终止 AWS Elastic Beanstalk 环境 - Terminating AWS Elastic Beanstalk environment 如何在Amazon Elastic Beanstalk(AWS EBS)上传递自定义环境变量? - How do you pass custom environment variable on Amazon Elastic Beanstalk (AWS EBS)? 开发环境和AWS elastic beanstalk - Development environment and AWS elastic beanstalk 如何在AWS Elastic Beanstalk docker实例中更改logrotate的设置 - How to change settings for logrotate in AWS Elastic Beanstalk docker instances 如何在AWS Elastic Beanstalk环境中配置负载均衡器? - How do I configure a load balancer in an AWS Elastic Beanstalk environment? AWS Elastic Beanstalk:如何在ebextensions中使用环境变量? - AWS Elastic Beanstalk: How to use environment variables in ebextensions? AWS Elastic Beanstalk Python:如何配置环境以使用硒? - AWS Elastic Beanstalk Python: How to configure the environment to use selenium? 如何使用 java sdk 创建 AWS Elastic Beanstalk 环境? - How to create AWS Elastic Beanstalk environment using java sdk? 如何使用 AWS Elastic Beanstalk 环境将自定义日志设置为 S3? - How to set a custom logs to S3 with AWS Elastic Beanstalk environment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM