Closed aditya-ambati closed 4 years ago
Could you share the module source repo?
@tbugfinder thank you for your quick response. Here are my modules:
aditya@LAPTOP-6C2MPJDV:~/terraform_modules/resource_group$ tree
.├── main.tf
├── outputs.tf
├── vars.tf
└── versions.tf
contents of main.tf
resource "azurerm_resource_group" "rg" {
name = var.rg_name
location = var.location
}
contents of outputs.tf
output "id" {
value = azurerm_resource_group.rg.id
}
output "name" {
value = azurerm_resource_group.rg.name
}
variable "rg_name" {
default = ""
description = "Name of the resource group"
}
variable "location" {
default = "westus"
description = "Geographic location of the Resource Group"
}
Before I tried terragrunt, i used to have environments folders for each environment (eg: prod,stage) through which i call the modules and it was working fine but i had too much duplicate data. Below is how i used to call modules before:
module "resource_group" {
source = "../../resource_group"
rg_name = var.rg_name
location = var.location
}
Is there a duplicate outputs.tf around?
No, I have outputs.tf only in my terraform_modules/resource_groups/
Under terraform_modules, I have different modules folders like resource_group, virtual_machine etc, and each folder contains it's corresponding outputs.tf. I used to refer these outputs while calling the modules from my environments main.tf files. Let me know if its a bit confusing for you. I can clarify by providing further artifact's.
Did you verify that referenced repo+tag includes the rg resource? git::git@github.com:adi4dpeople/terraform_modules.git//resource_group?ref=v0.0.1
As I have already mentioned in the previous comments, in my terraform_modules/resource_group/main.tf it is there. It doesn't seem like the issue is with the module outputs because they have been working for a very long time. The only difference is I'm calling the modules from terragrunt instead of my environment directory. Could you please try to reproduce it at your end because it's only a single and small module.
resource "azurerm_resource_group" "rg" {
name = var.rg_name
location = var.location
}
output "id" {
value = azurerm_resource_group.rg.id
}
output "name" {
value = azurerm_resource_group.rg.name
}
@tbugfinder I have noticed that, when i run terragrunt plan there is .terragrunt-cache folder created which pulls all my modules from github to local. In there i see that module's resource_group/main.tf contents have been replaced by terragrunt with the main.tf contents in my live folder resource_group main.tf so that is why my outputs.tf is throwing error.
I tried removing the main.tf in live folder and re ran the terragrunt, but it throws below error:
Found remote_state settings in /home/aditya/terraform-live/prod/resource_group/terragrunt.hcl but no backend block in the Terraform code in /home/aditya/terraform-live/prod/resource_group/.terragrunt-cache/Hovi5Z9TKrGgHU_Lf1P2xFmhkm0/4M87gZKvnrwMknqj9CwuSBSfiHk/resource_group. You must define a backend block (it can be empty!) in your Terraform code or your remote state settings will have no effect! It should look something like this:
terraform {
backend "azurerm" {}
}
@adi4dpeople just rename main.tf in your live folder. can be any other .tf filename, as long as it does not conflict with .tf files in your source module.
alternatively, use terragrunt's generate
argument in your remote_state configuration. then you can remove that .tf file entirely from your live folder. terragrunt will write a backend config file for you.
remote_state {
backend = "azurerm"
config = {
key = "${path_relative_to_include()}/terraform.tfstate"
resource_group_name = "common-rg"
storage_account_name = "testsa01"
container_name = "tfstate"
}
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
}
Thanks a lot @lorengordon it worked 👍 My final terragrunt.hcl looks like below. I also had to add provider config along with back end config for restricting provider version
remote_state {
backend = "azurerm"
config = {
key = "${path_relative_to_include()}/terraform.tfstate"
resource_group_name = "bwudhubrg01"
storage_account_name = "bwudhubtfsa01"
container_name = "tfstate"
}
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
}
generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
provider "azurerm" {
version = "1.36.0"
}
EOF
}
Terragrunt version
terragrunt version v0.23.10
Terraform version
Terraform v0.12.24
Directory Structure terraform-live/ ├── prod │ └── resource_group │ ├── main.tf │ └── terragrunt.hcl └── terragrunt.hcl
contents of terraform-live/terragrunt.hcl
contents of terraform-live/prod/resource_group/main.tf
contents of terraform-live/prod/resource_group/terragrunt.hcl
When i run terragrunt plan, i get the following error:
Followed the directory structure suggested here https://terratest.gruntwork.io/docs/getting-started/quick-start/
Please let me know if this is a bug or I'm missing something here.