#terragrunt - August 2020

terragruntArchived
44 messages

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

August 2020
Barak Schoster
Barak Schosterover 6 years ago(edited)
Howdy!
is there a way to migrate terrgrunt into “raw” terraform code?
joshmyers
joshmyersover 6 years ago
Yeah, re write it in not as DRY code.
Joe Presley
Joe Presleyabout 6 years ago
I’m new to terragrunt and have taken over a code base created by someone else. I have a terragrunt.hcl file with a terraform source block like this
terraform {
  source = "${get_terragrunt_dir()}/../../modules/datapipelines-infra"
}
Joe Presley
Joe Presleyabout 6 years ago
I’d like to create another module and call it in the same terragrunt.hcl file. Can I create another terraform block with a different source?
Joe Presley
Joe Presleyabout 6 years ago
I’ve tried googling and looking at the documentation but haven’t had any luck finding an answer.
Jonathan Tobe
Jonathan Tobeabout 6 years ago
@Jonathan Tobe has joined the channel
Callum Robertson
Callum Robertsonabout 6 years ago
hey terragrunt people
Callum Robertson
Callum Robertsonabout 6 years ago
is there any way to include a common templates in the live-infrastructure to modules?
Callum Robertson
Callum Robertsonabout 6 years ago
├── functions
│   ├── function1
│   │   └── terragrunt.hcl
│   ├── function2
│   │   └── terragrunt.hcl
├── project.tfvars
├── resolvers
│   ├── resolver1
│   │   └── terragrunt.hcl
│   └── resolver2
│       └── terragrunt.hcl
└── templates
Callum Robertson
Callum Robertsonabout 6 years ago
e.g. can I copy all the contents of the templates dir into the module dir when I call the module?
loren
lorenabout 6 years ago
if your module is already assuming a path to the templates dir, why not just pass it in as a variable?
Callum Robertson
Callum Robertsonabout 6 years ago
sorry I don't follow mate - can you give me an example?
loren
lorenabout 6 years ago
your module must be hardcoding/expecting the path templates or it doesn't make sense to copy the directory to the module dir
loren
lorenabout 6 years ago
so, instead of hardcoding, just expose the path as a variable (with a default of templates if you like)
Callum Robertson
Callum Robertsonabout 6 years ago
yeah I understand, unfortunately the way we designed the module won't allow for this and we don't want to update our appsync templates in each module
loren
lorenabout 6 years ago
then, pass the value for the path using terragrunt. then you do not need to copy the templates anywhere
Callum Robertson
Callum Robertsonabout 6 years ago
I fixed it using the following:
Callum Robertson
Callum Robertsonabout 6 years ago
terraform {
  source = "git@github.com:Callumccr/tf-mod-aws-appsync-resolver?ref=master"

  before_hook "copy_templates" {
    commands     = get_terraform_commands_that_need_vars()
    execute      = ["cp", "-a", "${get_parent_terragrunt_dir()}/humn-core/eu-west-1/dev/appsync/templates/.", "${get_terragrunt_dir()}/templates/"]
    run_on_error = true
  }
}
Callum Robertson
Callum Robertsonabout 6 years ago
that seems to work well!
Callum Robertson
Callum Robertsonabout 6 years ago
I can't pass the value of the template either due to variable interpolation 😞
loren
lorenabout 6 years ago
yes, a before_hook is the other way. you'll need to update your .gitignore also, of course, otherwise you'll accidentally commit them
Callum Robertson
Callum Robertsonabout 6 years ago
@loren I had to have the templates in the module dir because the module has the following logic:
Callum Robertson
Callum Robertsonabout 6 years ago
  request_template  = file("${path.module}/templates/${each.value.request_template}")
  response_template = file("${path.module}/templates/${each.value.response_template}")
Callum Robertson
Callum Robertsonabout 6 years ago
if I use the file() in the terragrunt.hcl and pass it as a variable, the interpolation of the content causes the resource to fail
Callum Robertson
Callum Robertsonabout 6 years ago
😞
loren
lorenabout 6 years ago
i'm not saying to use file() in terragrunt.hcl
loren
lorenabout 6 years ago
  request_template  = file("${var.template_dir}/${each.value.request_template}")
  response_template = file("${var.template_dir}}/${each.value.response_template}")

Callum Robertson
Callum Robertsonabout 6 years ago
ah...
loren
lorenabout 6 years ago
the modules are making a bad assumption that the templates directory is local to the module
Callum Robertson
Callum Robertsonabout 6 years ago
I didn't think to try that 😂
loren
lorenabout 6 years ago
but the module is not itself actually providing the templates
Callum Robertson
Callum Robertsonabout 6 years ago
yeah I didn't think I could use relative paths outside what was local to the module
loren
lorenabout 6 years ago
with terragrunt, you would provide the full path, not a relative path...
loren
lorenabout 6 years ago
but you can get most of the same behavior in the module (when using the module directly as a root config), by setting the default value of the variable to templates
loren
lorenabout 6 years ago
you would just override the default value with terragrunt to pass the directory that actually contains your templates
Callum Robertson
Callum Robertsonabout 6 years ago
worked very well!
Callum Robertson
Callum Robertsonabout 6 years ago
template_location = "${get_parent_terragrunt_dir()}/humn-core/eu-west-1/dev/appsync/templates"
Callum Robertson
Callum Robertsonabout 6 years ago
as part of my terragrunt.hcl
loren
lorenabout 6 years ago
nice
Callum Robertson
Callum Robertsonabout 6 years ago
thanks @loren - that's much cleaner that doing a cp -a 😂
loren
lorenabout 6 years ago
trust me, i've been there, done exactly that 😂
natalie
natalieabout 6 years ago
@natalie has joined the channel
Makeshift (Connor Bell)
Makeshift (Connor Bell)about 6 years ago(edited)
I think I'm really misunderstanding how --terragrunt-source is meant to work. In my case I have a format like this:
terragrunt/
  ├── terragrunt.hcl
modules/
  ├── module_1/
  └── child_module/

module_1 references child_module via source = ../child_module. How do I make that relative pathing work?
moos3
moos3about 6 years ago
so I have the following as a env.yaml
subnets: [  {
    subnet_name: "us-east1-dev-compute",
    subnet_region: "us-east1",
    cidr: 10.66.0.0/16,
    subnet_private_access: true,
    subnet_flow_logs: "true",
    subnet_flow_logs_interval: "INTERVAL_15_MIN",
    subnet_flow_logs_sampling: 0.5,
    subnet_flow_logs_metadata: "INCLUDE_ALL_METADATA",
    subnet_secondary_ranges: {
       pods: [{
          range_name: "us-east1-dev-pods",
          ip_cidr_range: "10.67.0.0/17"
        }],
        services: [{
          range_name: "us-east1-dev-services",
          ip_cidr_range: "10.67.128.0/17"
        }],
    },
  },]

in my module i'm doing this
locals {
  subnets = {
    for x in var.subnets :
    "${x.subnet_region}/${x.subnet_name}" => x
  }
}


/******************************************
	Subnet configuration
 *****************************************/
resource "google_compute_subnetwork" "subnetwork" {
  for_each                 = local.subnets
  name                     = each.value.subnet_name
  ip_cidr_range            = each.value.cidr
  region                   = each.value.subnet_region
  private_ip_google_access = lookup(each.value, "subnet_private_access", "false")
  dynamic "log_config" {
    for_each = lookup(each.value, "subnet_flow_logs", false) ? [{
      aggregation_interval = lookup(each.value, "subnet_flow_logs_interval", "INTERVAL_5_SEC")
      flow_sampling        = lookup(each.value, "subnet_flow_logs_sampling", "0.5")
      metadata             = lookup(each.value, "subnet_flow_logs_metadata", "INCLUDE_ALL_METADATA")
    }] : []
    content {
      aggregation_interval = log_config.value.aggregation_interval
      flow_sampling        = log_config.value.flow_sampling
      metadata             = log_config.value.metadata
    }
  }
  network     = var.network_self_link
  project     = var.host_project_id
  description = lookup(each.value, "description", null)
  secondary_ip_range = [
    for i in range(
      length(
        contains(
        keys(var.secondary_ranges), each.value.subnet_name) == true
        ? var.secondary_ranges[each.value.subnet_name]
        : []
    )) :
    var.secondary_ranges[each.value.subnet_name][i]
  ]
}

Trying for the life me to figure out why its saying this

Error: Invalid value for input variable

The environment variable TF_VAR_subnets does not contain a valid value for
variable "subnets": element 0: element "subnet_secondary_ranges": string
required.