Converting a string into a map

1,932 views
Skip to first unread message

Dan Jasek

unread,
Jun 3, 2016, 7:27:15 PM6/3/16
to Terraform
Is there any way to convert a string into a map object?

I am setting up an was_elastic_beanstalk_environment in a module and I would like to be able to pass environment variables into the module to be applied to the environment.  I can't find any way to add multiple setting values from variables in the module.

Ideally, I would like to be able to do this:

In the module:

resource "aws_elastic_beanstalk_environment" "myEnv"{
     
<....SNIP....>
 setting
{
   
namespace = "aws:elasticbeanstalk:environment"
   name
= "EnvironmentType"
   value
= "SingleInstance"
 
}
 setting
= ["${var.envSetting}"]
}

variable
"envSettings"{ }

In the calling code:

module "myMod" {
  envSetting
{
   
namespace = "aws:elasticbeanstalk:application:environment"
    name
= "FOO"
    value
= "theVal"
 
}


  envSetting
{

   
namespace = "
aws:elasticbeanstalk:application:environment"
    name
= "BAR"
    value
= "anotherVal"
  }
 

Obviously that isn't supported.  I've tried to find a way to create multiple setting variables from a single string, or to create an array of maps from a string that would be accepted by the setting variable, and have not had any luck.  Anyone have an idea on how to make this work?

Dan Lang

unread,
Jun 4, 2016, 9:19:27 AM6/4/16
to Terraform
You won't be able to do this until Terraform 0.7.0 is released.  With 0.7.0, lists and maps become first class types that can be passed to modules.  https://github.com/hashicorp/terraform/blob/master/CHANGELOG.md   The good news is there is already an RC out that you can test this with.

Dan Jasek

unread,
Jun 6, 2016, 1:25:28 PM6/6/16
to Terraform
Thanks for the info.  I have been playing around with the RC and I cannot figure out how to apply the new functionality to my problem.
Is there some new syntax I am missing?

This seems to work well:
variable "foo" {
  type = "list"
}

output "outFoo" {
  value = "${var.foo}"
}

Calling with:
provider "aws" {
  region = "us-east-1"
}

module "envTesting" {
  source = "../modules"
  foo {
    namespace = "aws:elasticbeanstalk:application:environment"
    name      = "FOO"
    value     = "aVal"
  }
  foo {
    namespace = "aws:elasticbeanstalk:application:environment"
    name      = "BAR"
    value     = "anotherVal"
  }
}

Output is a list of maps, as expected.

However, this fails:
resource "aws_elastic_beanstalk_environment" "myEnv" {
  name = "test_environment"
  application = "testing"

  setting = "${var.foo}"
}

variable "foo" {
  type = "list"
}

with the error:  aws_elastic_beanstalk_environment.myEnv: gob: type not registered for interface: map[string]ast.Variable

I also need a way to merge settings defined in the module's aws_elastic_beanstalk_environment directly, with settings passed in as variables.  Even if the above worked, I don't see a way to merge the data in the variable with settings defined in the resource.

Finally, while not required, it would be nice to be able to tweak the map coming from the variable.  So I could only pass in the name and value items in the variable and then apply the namespace before setting them on the resource.  There is not currently any way to do that, is there?

Dan Jasek

unread,
Jun 7, 2016, 12:26:24 PM6/7/16
to Terraform
I added a feature request to support this use case:  https://github.com/hashicorp/terraform/issues/7034
I think that being able to use the count functionality to create multiple setting entries would be the cleanest way to support this sort of thing.
Reply all
Reply to author
Forward
0 new messages