If i create multiple runners with below module - project_runner. Then can i use the output token for each runner once it is created in the subsequent lambda as env variable ? module.project_runner.runner_token -- will output the particular runner token every time it is created or it will take the first runner's token always?
main.tf
module "project_runner" {
source = "./modules/gitlab_user_runner"
runner_type = project_type
}
module "test-lambda" {
function_name = "test-lambda"
environment_variables = {
GITLAB_TOKEN = module.project_runner.runner_token
CONFIG_TOML = module.project_runner.config_toml
}
}
If you are creating multiple modules then you either need to give them different names or use a for_each.
For example, using different names.
module "project_runner_1" {
source = "./modules/gitlab_user_runner"
runner_type = var.project_type
}
module "test-lambda" {
function_name = "test-lambda"
environment_variables = {
GITLAB_TOKEN = module.project_runner_1.runner_token
CONFIG_TOML = module.project_runner_1.config_toml
}
}
module "project_runner_2" {
source = "./modules/gitlab_user_runner"
runner_type = var.project_type
}
module "test-lambda" {
function_name = "test-lambda"
environment_variables = {
GITLAB_TOKEN = module.project_runner_2.runner_token
CONFIG_TOML = module.project_runner_2.config_toml
}
}
The lambda environment variables can only reference one instance of the project runner module. I would not recommend doing it this way as each new instance requires you to copy and paste. Instead it would be better to use for_each or count to create multiple instances of the same module.
module "project_runner" {
count = 2
source = "./modules/gitlab_user_runner"
runner_type = var.project_type
}
module "test-lambda" {
count = 2
function_name = "test-lambda"
environment_variables = {
GITLAB_TOKEN = module.project_runner[count.index].runner_token
CONFIG_TOML = module.project_runner[count.index].config_toml
}
}
In the above example module.project_runner
is now a list with two items in it, each contains the output from the respective instances of the project_runner module.
[
{
runner_token = "foo"
config_toml = ...
},
{
runner_token = "bar"
config_toml = ...
}
]
When creating 2 instances of the test lambda the project runner output with the same index can be referenced using module.project_runner[count.index]
.
The first test lambda instance will be passed runner_token = "foo"
, and the second instance will be passed runner_token = "bar"
.
Using for_each allows for configuration to be supplied to each instance.
locals {
runner_config = {
runner1 = {
type = "project"
}
runner2 = {
type = "group"
}
}
}
module "project_runner" {
for_each = local.runner_config
source = "./modules/gitlab_user_runner"
runner_type = each.value.type
name = each.key
}
module "test-lambda" {
for_each = local.runner_config
function_name = "test-lambda-${each.key}"
environment_variables = {
GITLAB_TOKEN = module.project_runner[each.key].runner_token
CONFIG_TOML = module.project_runner[each.key].config_toml
}
}