Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform show -json module map is empty #25494

Closed
wils0ns opened this issue Jul 7, 2020 · 12 comments
Closed

terraform show -json module map is empty #25494

wils0ns opened this issue Jul 7, 2020 · 12 comments
Labels
bug cli explained a Terraform Core team member has described the root cause of this issue in code good first issue v0.12 Issues (primarily bugs) reported against v0.12 releases v0.13 Issues (primarily bugs) reported against v0.13 releases v0.14 Issues (primarily bugs) reported against v0.14 releases

Comments

@wils0ns
Copy link

wils0ns commented Jul 7, 2020

Terraform Version

Terraform v0.12.28
+ provider.google v3.28.0

Terraform Configuration Files

# ./main.tf
module "project" {
  source          = "../../../../blueprints/gcp/project"
  billing_account = "002A75-ABB3C9-680D62"
  folder_id       = "746484843613"
  project_id      = "di-test-mod-gcp-project-1"
  owner           = "owner@example.com"

  labels = {
    ac-number       = "us106088"
    env             = "non-prod"
    financial-owner = "test_owner"
    technical-owner = "wdossantosjunior"
  }
}
# ../../../../blueprints/gcp/project/main.tf
terraform {
  required_version = ">= 0.12"

  required_providers {
    google = ">= 3.13, < 4.0"
  }
}

module "project" {
  source = "../../../modules/gcp/project"

  billing_account = var.billing_account
  folder_id       = var.folder_id
  project_id      = var.project_id
  owner           = var.owner
  labels          = var.labels
}
# ../../../modules/gcp/project/main.tf
resource "google_project" "default" {
  auto_create_network = false
  billing_account     = var.billing_account
  folder_id           = var.folder_id
  labels              = var.labels
  name                = var.project_name != "" ? var.project_name : var.project_id
  project_id          = var.project_id

  lifecycle {
    prevent_destroy = true
  }
}

provider "google" {
  alias   = "dynamic"
  project = google_project.default.project_id
}

resource "google_project_iam_member" "owner" {
  project = google_project.default.project_id

  role   = "roles/owner"
  member = "user:${var.owner}"
}

resource "google_project_iam_audit_config" "audit_config" {
  project = google_project.default.project_id

  service = "allServices"

  audit_log_config {
    log_type = "ADMIN_READ"
  }

  audit_log_config {
    log_type = "DATA_READ"
  }

  audit_log_config {
    log_type = "DATA_WRITE"
  }
}

module "log_exporter" {
  source = "../bigquery-log-exporter"

  project  = google_project.default
  location = "US"

  sink = {
    name   = "audit_logs"
    filter = <<EOF
      logName = ("projects/${google_project.default.project_id}/logs/cloudaudit.googleapis.com%2Factivity"
      OR "projects/${google_project.default.project_id}/logs/cloudaudit.googleapis.com%2Fsystem_events"
      OR "projects/${google_project.default.project_id}/logs/cloudaudit.googleapis.com%2Fdata_access")
    EOF
  }

  bigquery_dataset = {
    name                       = "audit_logs"
    delete_contents_on_destroy = false
    readers                    = []
  }

  providers = {
    google = google.dynamic
  }
}

output "project_id" {
  description = "The project ID"
  value       = google_project.default.project_id
}

output "number" {
  description = "The numeric identifier of the project"
  value       = google_project.default.number
}

Debug Output

https://gist.github.com/wilson-codeminus/41d0951d92c1bd253e5ea12c62aceb5b

Crash Output

N/A

Expected Behavior

Expected to get a JSON representation of the resources present within the terraform state file

Actual Behavior

Module map object is empty:

$ terraform show -json
{"format_version":"0.1","terraform_version":"0.12.28","values":{"root_module":{}}}

Steps to Reproduce

  1. terraform init
  2. terraform apply
  3. terraform show -json

Additional Context

N/A

References

N/A

@jbardin
Copy link
Member

jbardin commented Jul 8, 2020

Hi @wilson-codeminus,

From the steps listed here, I would not expect there to be anything in the state, since there has not been anything applied.
Are you expecting to be working with an existing state file?

@jbardin jbardin added the waiting-response An issue/pull request is waiting for a response from the community label Jul 8, 2020
@wils0ns
Copy link
Author

wils0ns commented Jul 8, 2020

Yes. You're right, I forgot to add terraform apply to the steps to reproduce. I will edit it

@ghost ghost removed the waiting-response An issue/pull request is waiting for a response from the community label Jul 8, 2020
@jbardin
Copy link
Member

jbardin commented Jul 8, 2020

Thanks @wilson-codeminus, I'm not sure how to reproduce this yet.

Is it possible to create an example showing the module configuration as well?
Do you have any other configuration in the root module?
Does terraform show display any resources without the -json flag?

@wils0ns
Copy link
Author

wils0ns commented Jul 8, 2020

Ok. I will expand to show the module and submodule as well.
No. That is all that is contained in the root module.
Yes. terraform show displays all the resources without the -json flag.

@jbardin jbardin added bug v0.12 Issues (primarily bugs) reported against v0.12 releases labels Jul 8, 2020
@insertjokehere
Copy link

I'm seeing something similar where resources defined in a module that is itself defined in a module don't appear in terraform show -json but do in a normal terraform show.

I'm trying to cut my config down to the minimum to reproduce this, will update if I work it out

@alisdair alisdair added the confirmed a Terraform Core team member has reproduced this issue label Sep 18, 2020
@alisdair
Copy link
Contributor

Here's a very simple reproduction:

main.tf

module "a" {
  source = "./a"
}

a/main.tf:

module "b" {
  source = "./b"
}

a/b/main.tf:

resource "null_resource" "none" {}

Reproduction:

  • terraform init
  • terraform apply -auto-approve
  • terraform show (shows the resource)
  • terraform show -json (shows an empty state)

Confirmed on Terraform 0.12.28 and 0.13.3.

@alisdair
Copy link
Contributor

I believe the issue is in this loop:

// build a map of module -> [child module addresses]
moduleMap := make(map[string][]addrs.ModuleInstance)
for _, mod := range s.Modules {
if mod.Addr.IsRoot() {
continue
} else {
parent := mod.Addr.Parent().String()
moduleMap[parent] = append(moduleMap[parent], mod.Addr)
}
}

For my simple repro case above, this results in a moduleMap containing one key ("module.a"), as the only module in state is module.a.module.b.

This results in an empty output, because the following line only considers modules starting from root:

// use the state and module map to build up the module structure
ret.ChildModules, err = marshalModules(s, schemas, moduleMap[""], moduleMap)

The solution to this will have to ensure that we can cope with intermediate modules which do not have any resources, but do have child modules. It's not immediately obvious to me how we fix this, but I think it will be isolated to marshalRootModule and marshalModules.

Workaround: for anyone else hitting this bug, adding a null_resource instance in any modules which exclusively call other modules will ensure that their descendants are rendered in terraform show -json

@alisdair alisdair added explained a Terraform Core team member has described the root cause of this issue in code good first issue and removed confirmed a Terraform Core team member has reproduced this issue labels Sep 18, 2020
@pselle pselle added v0.13 Issues (primarily bugs) reported against v0.13 releases v0.14 Issues (primarily bugs) reported against v0.14 releases labels Jan 27, 2021
@sergioUjo
Copy link

sergioUjo commented Jan 29, 2021

Is this duplicated from #27351?

@alisdair
Copy link
Contributor

@sergioUjo Yes, thanks for catching that! I can confirm the bug is now fixed, due to changes merged in #27352. Closing.

@dawidmalina
Copy link

dawidmalina commented Feb 10, 2021

I have exactly the same issue with Terraform v0.14.6. All nested modules are ignored. Are you planning to backport this change to 0.14.x ?

@alisdair
Copy link
Contributor

The backport is merged (thanks @aliscott!) and scheduled for the 0.14.7 release.

@ghost
Copy link

ghost commented Mar 1, 2021

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked as resolved and limited conversation to collaborators Mar 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug cli explained a Terraform Core team member has described the root cause of this issue in code good first issue v0.12 Issues (primarily bugs) reported against v0.12 releases v0.13 Issues (primarily bugs) reported against v0.13 releases v0.14 Issues (primarily bugs) reported against v0.14 releases
Projects
None yet
Development

No branches or pull requests

8 participants