#terragrunt (2023-07)

terragrunt

Terragrunt discussions

Archive: https://archive.sweetops.com/terragrunt/

2023-07-17

susie-h avatar
susie-h

Reposting here instead of #help.

Calling cp’s api gateway module looks like this in terraform:

module "api_gateway" {
  source = "git::<https://github.com/cloudposse/terraform-aws-api-gateway.git?ref=0.3.1>"
  logging_level = var.logging_level
  context       = module.this.context
  openapi_config = jsondecode(file("${path.module}/${var.filename}"))
} 

In teragrunt, i wrote it as:

inputs = {
  context       = module.this.context
  openapi_config = jsondecode(file("dev-io.json"))
}

Which errors out with Unknown variable; There is no variable named "module". The api gateway module expects to be passed the context variable which is another child module calling the labels module. The cp example i’m following is found here - https://github.com/cloudposse/terraform-aws-api-gateway/tree/main/examples/complete

How can I recreate this module call in terragrunt?

Andriy Knysh (Cloud Posse) avatar
Andriy Knysh (Cloud Posse)

the context variable is a map of namespace, tenant, environment, stage, name, attributes

Andriy Knysh (Cloud Posse) avatar
Andriy Knysh (Cloud Posse)

you can provide values fr these vars directly when instantiating the module

Andriy Knysh (Cloud Posse) avatar
Andriy Knysh (Cloud Posse)
module "api_gateway" {
  source = "git::<https://github.com/cloudposse/terraform-aws-api-gateway.git?ref=0.3.1>"
  logging_level = var.logging_level
  openapi_config = jsondecode(file("${path.module}/${var.filename}"))

  namespace = ...
  tenant = ...
  environment = ...
  stage = ...
  name = ...
} 
Andriy Knysh (Cloud Posse) avatar
Andriy Knysh (Cloud Posse)

they all are not required (we use them all to uniquely and consistently name ALL AWS resources in all orgs/account/OUs/regions)

Andriy Knysh (Cloud Posse) avatar
Andriy Knysh (Cloud Posse)

you can provide just name for example (the rest will be set to null)

susie-h avatar
susie-h

Andriy, I understand what you’re saying. Pull out the few that api-gw is using and create them as terragrunt inputs.

Is there a possible solution to interact with context as a child module as you have in the terraform code? That would be really helpful for repeated use of cloudposse modules with terragrunt.

Andriy Knysh (Cloud Posse) avatar
Andriy Knysh (Cloud Posse)

(I’m not familiar with terragrunt, so maybe other people could answer. But it would look strange if terragrunt did not support child modules0

susie-h avatar
susie-h

I removed the line context = module.this.context and added the variables i needed, but the api-gw module seems to call context and throws an error:

│ Error: Reference to undeclared module
│ 
│   on main.tf line 36, in module "cloudwatch_log_group":
│   36:   context = module.this.context
│ 
│ No module call named "this" is declared in the root module.
susie-h avatar
susie-h

I deleted the terragrunt_cache and redownloaded the module. that seems to work. thank you @Andriy Knysh (Cloud Posse)

1
susie-h avatar
susie-h

As for terragrunt supporting child modules, it does but i don’t know how the code looks with how you use the labels module.

loren avatar

The easiest thing to do would probably be to just write the same .tf alongside the terragrunt.hcl, and use source = "."

loren avatar

if you don’t want to do that, then you could use a second terragrunt config to point to the label module (which is where the context comes from), and then use the terragrunt dependency feature to read its outputs in your other terragrunt configs

susie-h avatar
susie-h

@loren the sub-module suggestion was an idea i had, but being new to terragrunt i wasn’t confident with how many sub-levels it would allow or how to reference. no harm in testing

1
loren avatar

in general, each terragrunt config must point at a single module. if you have several modules that you want to integrate, you can write the terraform that does it (which by definition becomes a terraform “module”), and then point terragrunt at that.

or the other option is to use terragrunt dependencies to pass the values around between separate terragrunt configs (each of which point at a single terraform module)

2023-07-18

    keyboard_arrow_up