0

I'm using aws version 3.50.0, but with for_each to read from _variables.tf. My main.tf is given below. When I comment out environment section as shown below, everything works. But how to add environment variables? When I uncomment and run the plan it throws this error:

│ Error: Unsupported block type
│ 
│   on main.tf line 22, in module "lambda_function":
│   22:   environment {
│ 
│ Blocks of type "environment" are not expected here.
│
│ make: *** [Makefile:44: plan] Error 1
______________________________________________________


locals {
  functions = {
    for function in local.workspace:
    function.name => function
  }
}

module "lambda_function" {

  source = "terraform-aws-modules/lambda/aws"

  for_each = local.functions

  function_name                  = "${element(split("-", terraform.workspace), 4)}-${each.value.name}"
  description                    = lookup(each.value, "description", "Not applicable")
  handler                        = "${each.value.name}.lambda_handler"
  memory_size                    = 128
  reserved_concurrent_executions = lookup(each.value, "reserved_concurrent_executions", 5)
  source_path = "${each.value.name}.py"
  runtime                        = "python3.8"
  
  # environment {
  #     variables = {
  #       LOTJM_ENV = "${element(split("-", terraform.workspace), 4)}"
  #     }
  # }
  
  tags = {
    Name = "${element(split("-", terraform.workspace), 4)}-${each.value.name}"
  }
}
2
  • 2
    By reading the docs and using a environment_variables variable / parameter instead, not a block and not an arbitrarily named one. Commented Jul 19, 2021 at 10:56
  • If you look at the documentation of the module it says to use: environment_variables. That should fix the issue for you. I also have a terraform module for lambda that you can use if you need help: github.com/tomarv2/terraform-aws-lambda Commented Jul 19, 2021 at 16:25

1 Answer 1

3

According to the documentation for this module, there's an optional input variable called environment_variables which is documented as follows:

environment_variables map(string)

Description: A map that defines environment variables for the Lambda Function.

Default: {}

This suggests that the intended way to set environment variables for this module would be something like the following:

module "lambda_function" {
  source   = "terraform-aws-modules/lambda/aws"
  for_each = local.functions

  # ...

  environment_variables = {
    LOTJM_ENV = element(split("-", terraform.workspace), 4)
  }
}

This is a similar approach as for setting the tags variable, which is also declared as expecting a map(string) value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.