I’m using Terraform to create an AWS API Gateway and 2 Lambda functions.
I would like to know if there is a way to apply for_each
to only a particular block from locals
while using the modules. For the remaining variables in modules, I want it to either pick up from variables.tf
or from locals:
Below is my code:
locals {
lambda_apigateway = {
"digitalts-devtest-${var.environment}-auth" = {
service_name = "${var.environment}-auth"
function_handler = "auth.handler"
lambda_key = "customizedlayer.zip"
function_name = "${var.account}-${var.environment}"
source_dir = "../built/lambdahandlers/auth/"
output_path = "../auth.zip"
layer_name = var.layer_name
create_lambda_layer = var.create_lambda_layer
#layers = var.layers
layer_count = var.layer_count
filename = var.filename
#create_lambda_permission = var.create_lambda_permission
#create_lambda = var.create_lambda
environment_variables = {
"MATRIS_CREDENTIALS" = "MATRIS_CLIENT"
}
},
"digitalts-devtest-${var.environment}-healthz" = {
service_name = "${var.environment}-healthz"
function_handler = "healthz.handler"
lambda_key = "customizedlayer.zip"
function_name = "${var.account}-${var.environment}"
source_dir = "../built/lambdahandlers/healthz/"
output_path = "../healthz.zip"
layer_name = var.layer_name
create_lambda_layer = var.create_lambda_layer
#layers = var.layers
layer_count = var.layer_count
filename = var.filename
environment_variables = {
"MATRIS_CREDENTIALS" = "MATRIS_CLIENT"
}
}
}
api_gateway_variables = {
create_apigw_rest = var.create_apigw_rest
create_apigw = var.create_apigw
api_gateway_name = var.api_gateway_name
endpoint_configuration = var.endpoint_configuration
parent_path = var.parent_path
first_sub_path = var.first_sub_path
http_methods = var.http_methods
authorizations = var.authorizations
request_parameters = var.request_parameters
request_models = var.request_models
integration_http_methods = var.integration_http_methods
integration_types = var.integration_types
connection_types = var.connection_types
uri = var.uri
}
}
I tried the following code but it is applying the for_each
to all of the locals:
module "lambda_API_dts" {
source = "gitlab.***"
version = "0.0.49"
for_each = local.lambda_apigateway
service_name = each.key
lambda_key = each.value.lambda_key
function_name = each.value.function_name
source_dir = each.value.source_dir
output_path = each.value.output_path
runtime = var.runtime
function_handler = var.function_handler
timeout = var.timeout
memory_size = var.memory_size
environment_variables = each.value.environment_variables
subnet_ids = var.subnet_ids
VPCId = var.vpc_id
env = var.env
filename = var.filename
lambda_layer_bucket = var.lambda_layer_bucket
s3_key_lambda_layer = var.s3_key_lambda_layer
layer_count = each.value.layer_count
#api gateway variables
create_apigw_rest = local.create_apigw_rest
create_apigw = local.create_apigw
api_gateway_name = local.api_gateway_name
endpoint_configuration = local.endpoint_configuration
parent_path = local.parent_path
first_sub_path = local.first_sub_path
http_methods = local.http_methods
authorizations = local.authorizations
request_parameters = local.request_parameters
request_models = local.request_models
integration_http_methods = local.integration_http_methods
integration_types = local.integration_types
connection_types = local.connection_types
uri = local.uri
}
I want only 1 API gateway to be created & 2 Lambdas to be created.
2
Answers
Try using dynamic as follows:
@Shlomi,
dynamic
blocks won’t work here, in a module, as said in the documentation :Method 1 : Deport the
for_each
to the resource definitionIf you want to keep the root module as is for a particular reason, you can use
for_each
in the child module to loop on the desired resource. Below, you’ll find the root module & the child module :./main.tf
:./modules/lambda_API_dts/main.tf
:Method 2 : Split your modules by purpose
Here, your
lambda_API_dts
module seems to have the purpose of creating a AWS API gateway AND AWS lambda functions.If you really need to have your
for_each
block in the calling module, you can keep it there but you can also put thefor_each
at the resource level, in theresource
block, using my example in Method 1.This way, you will :
Hope this helps !