0

The expectation is that when running a terragrunt run-all apply from the root directory, a provider.tf file will be created in the sub directories. I've verified my backend is able to talk to my azure storage account and it will create a terraform.tfstate file there. I expect a provider.tf file to appear under each "service#" folder. I'm extremely new to terragrunt. This is my first exercise with it. I am not actually trying to deploy any terraform resources. Just have the provider.tf file created in my sub directories. TF version is 1.1.5. Terragrunt version is 0.36.1.

my folder structure

tfpractice
├───terragrunt.hcl
├───environment_vars.yaml
├───dev
│   ├───service1
│       ├───terragrunt.hcl       
│   └───service2
│       ├───terragrunt.hcl 
└───prod
    ├───service1
│       ├───terragrunt.hcl 
    └───service2
│       ├───terragrunt.hcl 

root terragrunt.hcl config

# Generate provider configuration for all child directories
generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite"
  contents = <<EOF
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "2.94.0"
    }
  }
  backend "azurerm" {}

}

provider "azurerm" {
  features {}
}
EOF
}


# Remote backend settings for all child directories
remote_state {
  backend = "azurerm"
  config = {
    resource_group_name  = local.env_vars.resource_group_name
    storage_account_name = local.env_vars.storage_account_name
    container_name       = local.env_vars.container_name 
    key            = "${path_relative_to_include()}/terraform.tfstate"
  }
}


# Collect values from environment_vars.yaml and set as local variables
locals {
  env_vars = yamldecode(file("environment_vars.yaml"))
}

environment_vars.yaml config

resource_group_name:  "my-tf-test"
storage_account_name: "mystorage"
container_name:       "tfstate"

terragrunt.hcl config in the service# folders

# Collect values from parent environment_vars.yaml file and set as local variables
locals {
  env_vars = yamldecode(file(find_in_parent_folders("environment_vars.yaml")))
}

# Include all settings from the root terragrunt.hcl file
include {
  path = find_in_parent_folders()
}

When i run the terragrunt run-all apply this is the output

Are you sure you want to run 'terragrunt apply' in each folder of the stack described above? (y/n) y

Initializing the backend...

Initializing the backend...

Initializing the backend...

Initializing the backend...

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Initializing provider plugins...
Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.


Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

No changes. Your infrastructure matches the configuration.


No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

It looks successful, however NO provider.tf files show up in ANY directory. Not even root. It just creates a terraform.tfstate file under the service# directories.

But...if I run a terragrunt init from the root directory, it will create the provider.tf file as expected in the root directory. This does NOT work in the service# directories although the terragrunt init is successful.

What am I missing? This is the most basic terragrunt use case, but the examples lead me to believe this should just work.

dcvl
  • 485
  • 1
  • 7
  • 21
  • Have you tried running it from `dev` or `prod` directories? – Marko E Feb 03 '22 at 07:50
  • This works, but not really. The issue is in the terragrunt.hcl config. If i use the same config as what I currently have in a service# directory, it works perfectly at the dev or prod level. However, terragrunt complains about having only one level of includes if I try to run a terragrunt run-all apply. It doesn't like nested includes apparently, meaning in dev and also in dev's service# directories. So while I can place the .hcl at the dev and prod folders and create a provider.tf as i'd expect, it will not go down to the service# folders. – dcvl Feb 03 '22 at 13:52
  • I think you need this block in the child `terragrunt.hcl` files: `include "root" { path = find_in_parent_folders() }`. And that is probably instead of the other `include` that you have. Can you try that? – Marko E Feb 03 '22 at 14:33
  • Nope. It complained about more than one level of includes again, so I deleted the includes from the dev and prod directory. No provider.tf files were created anywhere, but it did create a terraform.tfstate in each service#...which also shouldn't happen since the backend is specified. This isn't making a lot of sense – dcvl Feb 03 '22 at 21:44

1 Answers1

1

I got it to work, but the terragrunt run-all apply command doesn't work at all. Instead, at the root I have to run terragrunt apply. If you don't at the root, then all sub folders get grouped together rather than in a dev/prod sub folder. Then I have to go to every sub folder and run this again. It's the only way i've gotten it to work.

dcvl
  • 485
  • 1
  • 7
  • 21