Passing a map to a module

2,706 views
Skip to first unread message

Franck Ratier

unread,
Nov 25, 2016, 12:41:35 PM11/25/16
to Terraform
Hello,

I'm trying to update a module I have to setup lambda functions with a variable number of environment variables (via the variables attribute of the aws_lambda_function resource), but I can't figure out the right syntax to pass a map to a module.

Is this possible? Has anyone an example of how this should be done?

Thanks,
Franck

Brian Lalor

unread,
Nov 25, 2016, 3:11:35 PM11/25/16
to terrafo...@googlegroups.com
I do it; something like this should work:

variable "ami-cluster-base" {
    type = "map"
    default = {
        us-west-2 = "ami-deadbeef"
    }
}

module "environment" {
    source = "../tf-environment"

    ami-cluster-base = "${var.ami-cluster-base}"
}


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/1201ddec-df8c-42bf-9560-e1fc5a7b92f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

— 
Brian Lalor

Franck Ratier

unread,
Nov 27, 2016, 12:14:52 PM11/27/16
to Terraform
Thank you for your reply, Brian.

At the moment what I do is the following, and I'm wondering if there's a more generic way to do it:

./modules/lambda_function/main.tf
variable "env_var1" { default = "" }
variable
"env_var2" { default = "" }
variable
"env_var3" { default = "" }

resource
"aws_lambda_function" "mod" {
 
...
  environment
{
    variables
{
        LAMBDA_ENV_VAR1
= "${var.env_var1}"
        LAMBDA_ENV_VAR2
= "${var.env_var2}"
        LAMBDA_ENV_VAR3
= "${var.env_var3}"
   
}
 
}
}

module "my_service_slack_notification" {
  source        
= "modules/lambda_function"
 
...
  env_var1      
= "${var.slack_encrypted_hook_url}"
  env_var2      
= "${var.slack_channel}"
}

So as you see I'm setting 3 environment variables when defining the module and when using it I can make use of some or all of them.

Since "variables" is a map I was wondering if I could pass it directly to the module without having to use these extra variables.

Franck

Khalid Hosein

unread,
Nov 27, 2016, 4:54:56 PM11/27/16
to Terraform
Hello Franck,

Yup, I think if you extrapolate on Brian's example, you might get this sort of thing:

./modules/lambda_function/main.tf
variable "lambda_env_vars" { type = "map" }


resource 
"aws_lambda_function" "mod" {
  
...
  environment 
{

    variables 
= "${var.lambda_env_vars}"
  
}
}

and in ./my_service.tf
module "my_service_slack_notification" {
  source        
= "modules/lambda_function"
 
...

  env_vars      
= "${var.slack_env_vars}"
}

variable
"slack_env_vars" {
  type
= "map"
 
default = {
    env_var1
= "foo"
    env_var2
= "bar"
 
}
}

Hope this helps!

-- Khalid

Franck Ratier

unread,
Nov 28, 2016, 4:24:57 AM11/28/16
to Terraform
Fantastic! Yes it helps, problem solved, thank you to both of you!

Franck
Reply all
Reply to author
Forward
0 new messages