#terragrunt (2023-07)
Terragrunt discussions
Archive: https://archive.sweetops.com/terragrunt/
2023-07-17
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?
the context
variable is a map of namespace, tenant, environment, stage, name, attributes
you can provide values fr these vars directly when instantiating the module
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 = ...
}
they all are not required (we use them all to uniquely and consistently name ALL AWS resources in all orgs/account/OUs/regions)
you can provide just name
for example (the rest will be set to null
)
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.
(I’m not familiar with terragrunt, so maybe other people could answer. But it would look strange if terragrunt did not support child modules0
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.
I deleted the terragrunt_cache and redownloaded the module. that seems to work. thank you @Andriy Knysh (Cloud Posse)
As for terragrunt supporting child modules, it does but i don’t know how the code looks with how you use the labels module.
The easiest thing to do would probably be to just write the same .tf alongside the terragrunt.hcl, and use source = "."
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
@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
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)