Process variables in Yaml in Terraform

99 Views Asked by At

I am trying to use this module:

https://github.com/philips-labs/terraform-aws-github-runner/blob/main/modules/multi-runner/runners.tf

This is my yaml

matcherConfig:
  exactMatch: true
  labelMatchers:
    - [self-hosted, linux, arm64, amazon]
fifo: true
delay_webhook_event: 0
redrive_build_queue:
  enabled: false
  maxReceiveCount: null
runner_config:
  runner_os: linux
  runner_architecture: arm64
  runner_extra_labels: "ubuntu,amd64,default"
  runner_group_name: default
  runner_name_prefix: amazon-arm64_
  enable_organization_runners: true
  enable_runner_binaries_syncer: false
  enable_ssm_on_runners: true
  block_device_mappings: ${block_device_mappings}
  pool_runner_owner: "${pool_org}"
  pool_config: ${(pool_config)}
  runner_run_as: "ghrunner"
  create_service_linked_role_spot: true
  runner_iam_role_managed_policy_arns: "${managed_policy_arns}"
  runner_metadata_options:
    http_endpoint : enabled
    http_put_response_hop_limit : 1
    http_tokens : required
    instance_metadata_tags : enabled
  enable_jit_config: false
  instance_types:
    - c4.xlarge

I have defined locals as

locals {
  # Load runner configurations from Yaml files
  multi_runner_config = {
    for c in fileset("${path.module}/templates/runner-configs", "*.yaml") :
    trimsuffix(c, ".yaml") => {
      config = yamldecode(templatefile("${path.module}/templates/runner-configs/${c}", {
        pool_runner_owner                   = var.pool_org,
        pool_config                         = jsonencode(var.pool_config),
        runner_iam_role_managed_policy_arns = jsonencode(var.managed_policy_arns),
        block_device_mappings               = jsonencode(var.block_device_mappings),
        ami_owners                          = jsonencode(var.ami_owners)
        ami_filter                          = jsonencode(var.ami_filter)
      }))
    }
  }

}

getting error

  Error: 2023-09-29T17:52:14.696Z [ERROR] vertex "local.multi_runner_config" error: Invalid function argument
  Error: -29T17:52:14.696Z [ERROR] vertex "local.multi_runner_config" error: Invalid function argument
  Error: -29T17:52:14.696Z [ERROR] vertex "local.multi_runner_config (expand)" error: Invalid function argument
  Error: -29T17:52:14.696Z [ERROR] vertex "local.multi_runner_config (expand)" error: Invalid function argument
  ╷
  │ Error: Invalid function argument
  │ 
  │   on variables.tf line 168, in locals:
  │  168:       config = yamldecode(templatefile("${path.module}/templates/runner-configs/${c}", {
  │  169:         pool_runner_owner                   = var.pool_org,
  │  170:         pool_config                         = jsonencode(var.pool_config),
  │  171:         runner_iam_role_managed_policy_arns = jsonencode(var.managed_policy_arns),
  │  172:         block_device_mappings               = jsonencode(var.block_device_mappings),
  │  173:         ami_owners                          = jsonencode(var.ami_owners)
  │  174:         ami_filter                          = jsonencode(var.ami_filter)
  │  175:       }))
  │     ├────────────────
  │     │ var.ami_filter is a map of list of string
  │     │ var.ami_owners is a list of string
  │     │ var.block_device_mappings is a list of object
  │     │ var.managed_policy_arns is a list of string
  │     │ var.pool_config is a list of object
  │     │ var.pool_org is a string
  │ 
  │ Invalid value for "vars" parameter: vars map does not contain key
  │ "pool_org", referenced at
  │ ./templates/runner-configs/linux-arm64.yaml:20,25-33.

How can I solve this issue.

1

There are 1 best solutions below

4
Matthew Schuchard On

This can be fixed by resolving the naming conflict between the variable name in the template content and the templatefile second parameter argument. Either update the template via:

pool_runner_owner: "${pool_runner_owner}"

or the second parameter map entry via:

pool_org = var.pool_org