[GH-ISSUE #6] Bug: KeyError: 'all_module' when no module #1

Closed
opened 2026-05-06 12:36:05 +02:00 by BreizhHardware · 4 comments

Originally created by @Leylan on GitHub (Apr 24, 2023).
Original GitHub issue: https://github.com/patrickchugh/terravision/issues/6

Hello,
I have great interest in your project and following it closely.

I've stumble upon what seems to be a bug. When no module is specified in any tf file, terravision raise it as an error.

Parsing locals...
Traceback (most recent call last):
File "//terravision/terravision", line 172, in
cli(
File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1130, in call
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "//terravision/terravision", line 110, in draw
tfdata = compile_tfdata(source, varfile, annotate)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "//terravision/terravision", line 25, in compile_tfdata
tfdata = interpreter.get_metadata(tfdata)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/terravision/modules/interpreter.py", line 369, in get_metadata
for _, module_list in tfdata['all_module'].items() :
~~~~~~^^^^^^^^^^^^^^
KeyError: 'all_module'

I have tested it on my terraform modules and with futurice terraform_examples aswell by removing the module manually

Originally created by @Leylan on GitHub (Apr 24, 2023). Original GitHub issue: https://github.com/patrickchugh/terravision/issues/6 Hello, I have great interest in your project and following it closely. I've stumble upon what seems to be a bug. When no module is specified in any tf file, terravision raise it as an error. Parsing locals... Traceback (most recent call last): File "//terravision/terravision", line 172, in <module> cli( File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1130, in __call__ return self.main(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1055, in main rv = self.invoke(ctx) ^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1657, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1404, in invoke return ctx.invoke(self.callback, **ctx.params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/click/core.py", line 760, in invoke return __callback(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "//terravision/terravision", line 110, in draw tfdata = compile_tfdata(source, varfile, annotate) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "//terravision/terravision", line 25, in compile_tfdata tfdata = interpreter.get_metadata(tfdata) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/terravision/modules/interpreter.py", line 369, in get_metadata for _, module_list in tfdata['all_module'].items() : ~~~~~~^^^^^^^^^^^^^^ KeyError: 'all_module' I have tested it on my terraform modules and with futurice terraform_examples aswell by removing the module manually
Author
Owner

@patrickchugh commented on GitHub (Apr 24, 2023):

Hi thanks for the feedback can you post your terraform code so I can try to reproduce the bug?

<!-- gh-comment-id:1519377126 --> @patrickchugh commented on GitHub (Apr 24, 2023): Hi thanks for the feedback can you post your terraform code so I can try to reproduce the bug?
Author
Owner

@Leylan commented on GitHub (Apr 24, 2023):

Sure

backend.tf

terraform {
  required_version = ">=1.3.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }

  backend "s3" {
    bucket = "terraformtfstatebucket"
    region = "ap-northeast-1"
  }
}

provider "aws" {
  region = "ap-northeast-1"
}

It get stuck unless I add an empty dummy module as below

module "dummy" {
  source = "devops-rob/resource/null"
}

s3.tf

resource "aws_s3_bucket" "create-repo" {
  bucket = var.bucket_name

  tags = {
    Name        = var.bucket_name
  }
}

resource "aws_cloudfront_origin_access_identity" "create-repo" {
  comment = "Create-repo s3 bucket oai"
}

resource "aws_s3_bucket_policy" "allow_access_from_cloudfront" {
  bucket = aws_s3_bucket.create-repo.id
  policy = data.aws_iam_policy_document.allow_access_from_cloudfront.json
}

data "aws_iam_policy_document" "allow_access_from_cloudfront" {
  statement {
    principals {
      type        = "AWS"
      identifiers = [aws_cloudfront_origin_access_identity.create-repo.iam_arn]
    }

    actions = [
      "s3:GetObject*",
      "s3:GetBucket*",
      "s3:List*"
    ]

    resources = [
      aws_s3_bucket.create-repo.arn,
      "${aws_s3_bucket.create-repo.arn}/*",
    ]
  }
}

cloudfront.tf

resource "aws_cloudfront_distribution" "s3_distribution" {
  origin {
    domain_name              = aws_s3_bucket.create-repo.bucket_regional_domain_name
    origin_id                = "origin"
    connection_attempts      = 3
    connection_timeout       = 10

    s3_origin_config {
      origin_access_identity   = aws_cloudfront_origin_access_identity.create-repo.cloudfront_access_identity_path
    }
  }

  enabled             = true
  is_ipv6_enabled     = true
  http_version        = "http2"
  default_root_object = "index.html"

  custom_error_response {
    error_code = 404
    response_code = 200
    response_page_path = "/index.html"
  }

  default_cache_behavior {
    allowed_methods  = ["GET", "HEAD"]
    cached_methods   = ["GET", "HEAD"]
    target_origin_id = "origin"
    compress         = true
    viewer_protocol_policy = "redirect-to-https"

    forwarded_values {
      query_string = false

      cookies {
        forward = "none"
      }
    }
  }

  restrictions {
    geo_restriction {
      restriction_type = "none"
      locations        = []
    }

  }
  viewer_certificate {
    cloudfront_default_certificate = true
  }
}

variables.tf

variable "bucket_name" {
  description = "S3バケツ名"
}
<!-- gh-comment-id:1519410819 --> @Leylan commented on GitHub (Apr 24, 2023): Sure backend.tf ``` terraform { required_version = ">=1.3.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 4.0" } } backend "s3" { bucket = "terraformtfstatebucket" region = "ap-northeast-1" } } provider "aws" { region = "ap-northeast-1" } ``` It get stuck unless I add an empty dummy module as below ``` module "dummy" { source = "devops-rob/resource/null" } ``` s3.tf ``` resource "aws_s3_bucket" "create-repo" { bucket = var.bucket_name tags = { Name = var.bucket_name } } resource "aws_cloudfront_origin_access_identity" "create-repo" { comment = "Create-repo s3 bucket oai" } resource "aws_s3_bucket_policy" "allow_access_from_cloudfront" { bucket = aws_s3_bucket.create-repo.id policy = data.aws_iam_policy_document.allow_access_from_cloudfront.json } data "aws_iam_policy_document" "allow_access_from_cloudfront" { statement { principals { type = "AWS" identifiers = [aws_cloudfront_origin_access_identity.create-repo.iam_arn] } actions = [ "s3:GetObject*", "s3:GetBucket*", "s3:List*" ] resources = [ aws_s3_bucket.create-repo.arn, "${aws_s3_bucket.create-repo.arn}/*", ] } } ``` cloudfront.tf ``` resource "aws_cloudfront_distribution" "s3_distribution" { origin { domain_name = aws_s3_bucket.create-repo.bucket_regional_domain_name origin_id = "origin" connection_attempts = 3 connection_timeout = 10 s3_origin_config { origin_access_identity = aws_cloudfront_origin_access_identity.create-repo.cloudfront_access_identity_path } } enabled = true is_ipv6_enabled = true http_version = "http2" default_root_object = "index.html" custom_error_response { error_code = 404 response_code = 200 response_page_path = "/index.html" } default_cache_behavior { allowed_methods = ["GET", "HEAD"] cached_methods = ["GET", "HEAD"] target_origin_id = "origin" compress = true viewer_protocol_policy = "redirect-to-https" forwarded_values { query_string = false cookies { forward = "none" } } } restrictions { geo_restriction { restriction_type = "none" locations = [] } } viewer_certificate { cloudfront_default_certificate = true } } ``` variables.tf ``` variable "bucket_name" { description = "S3バケツ名" } ```
Author
Owner

@patrickchugh commented on GitHub (May 14, 2023):

@Leylan Hi Sorry for the delayed response was implementing some other fixes. I have updated the code can you git pull the latest code and try again. Here is my output from your sample files:

$ terravision graphdata --source  ~/git/no-module-test/

Preflight check..
  dot command detected: /opt/local/bin/dot
  gvpr command detected: /opt/local/bin/gvpr
  git command detected: /usr/bin/git
  Added Source Location: /Users/parchugh/git/no-module-test/
Reading Terraforms..
  Parsing /Users/parchugh/git/no-module-test/s3.tf
    Found 3 resource stanza(s)
    Found 1 data stanza(s)
  Parsing /Users/parchugh/git/no-module-test/cloudfront.tf
    Found 1 resource stanza(s)
  Parsing /Users/parchugh/git/no-module-test/variables.tf
    Found 1 variable stanza(s)
  Parsing /Users/parchugh/git/no-module-test/backend.tf
Processing Variables..
    aws_s3_bucket.create-repo
    aws_cloudfront_origin_access_identity.create-repo
    aws_s3_bucket_policy.allow_access_from_cloudfront
    aws_cloudfront_distribution.s3_distribution

  Locals list :

  Module list :

  Resource list :
    no-module-test/s3.tf: aws_s3_bucket.create-repo
    no-module-test/s3.tf: aws_cloudfront_origin_access_identity.create-repo
    no-module-test/s3.tf: aws_s3_bucket_policy.allow_access_from_cloudfront
    no-module-test/cloudfront.tf: aws_cloudfront_distribution.s3_distribution

  Data list :
    no-module-test/s3.tf: aws_iam_policy_document.allow_access_from_cloudfront

  Variable List:

    Module: main
      var.bucket_name = 
      var.variable = [{'bucket_name': {'description': 'S3バケツ名'}}]

  Conditional Resource List:

Computing Relations between 4 out of 4 resources...
   aws_s3_bucket_policy.allow_access_from_cloudfront --> aws_s3_bucket.create-repo
   aws_s3_bucket.create-repo --> aws_cloudfront_distribution.s3_distribution (Reversed)
   aws_cloudfront_origin_access_identity.create-repo --> aws_cloudfront_distribution.s3_distribution (Reversed)

Unprocessed Graph Dictionary:
{
    "aws_cloudfront_distribution.s3_distribution": [],
    "aws_cloudfront_origin_access_identity.create-repo": [
        "aws_cloudfront_distribution.s3_distribution"
    ],
    "aws_s3_bucket.create-repo": [
        "aws_cloudfront_distribution.s3_distribution"
    ],
    "aws_s3_bucket_policy.allow_access_from_cloudfront": [
        "aws_s3_bucket.create-repo"
    ]
}

Final Graphviz Input Dictionary
{
    "aws_cloudfront_distribution.s3_distribution": [],
    "aws_cloudfront_origin_access_identity.create-repo": [
        "aws_cloudfront_distribution.s3_distribution"
    ],
    "aws_s3_bucket.create-repo": [
        "aws_cloudfront_distribution.s3_distribution"
    ],
    "aws_s3_bucket_policy.allow_access_from_cloudfront": [
        "aws_s3_bucket.create-repo"
    ]
}

Output JSON Dictionary :
{
    "aws_cloudfront_distribution.s3_distribution": [],
    "aws_cloudfront_origin_access_identity.create-repo": [
        "aws_cloudfront_distribution.s3_distribution"
    ],
    "aws_s3_bucket.create-repo": [
        "aws_cloudfront_distribution.s3_distribution"
    ],
    "aws_s3_bucket_policy.allow_access_from_cloudfront": [
        "aws_s3_bucket.create-repo"
    ]
}

Exporting graph object into file architecture.json

Completed!
<!-- gh-comment-id:1546971697 --> @patrickchugh commented on GitHub (May 14, 2023): @Leylan Hi Sorry for the delayed response was implementing some other fixes. I have updated the code can you `git pull` the latest code and try again. Here is my output from your sample files: ``` $ terravision graphdata --source ~/git/no-module-test/ Preflight check.. dot command detected: /opt/local/bin/dot gvpr command detected: /opt/local/bin/gvpr git command detected: /usr/bin/git Added Source Location: /Users/parchugh/git/no-module-test/ Reading Terraforms.. Parsing /Users/parchugh/git/no-module-test/s3.tf Found 3 resource stanza(s) Found 1 data stanza(s) Parsing /Users/parchugh/git/no-module-test/cloudfront.tf Found 1 resource stanza(s) Parsing /Users/parchugh/git/no-module-test/variables.tf Found 1 variable stanza(s) Parsing /Users/parchugh/git/no-module-test/backend.tf Processing Variables.. aws_s3_bucket.create-repo aws_cloudfront_origin_access_identity.create-repo aws_s3_bucket_policy.allow_access_from_cloudfront aws_cloudfront_distribution.s3_distribution Locals list : Module list : Resource list : no-module-test/s3.tf: aws_s3_bucket.create-repo no-module-test/s3.tf: aws_cloudfront_origin_access_identity.create-repo no-module-test/s3.tf: aws_s3_bucket_policy.allow_access_from_cloudfront no-module-test/cloudfront.tf: aws_cloudfront_distribution.s3_distribution Data list : no-module-test/s3.tf: aws_iam_policy_document.allow_access_from_cloudfront Variable List: Module: main var.bucket_name = var.variable = [{'bucket_name': {'description': 'S3バケツ名'}}] Conditional Resource List: Computing Relations between 4 out of 4 resources... aws_s3_bucket_policy.allow_access_from_cloudfront --> aws_s3_bucket.create-repo aws_s3_bucket.create-repo --> aws_cloudfront_distribution.s3_distribution (Reversed) aws_cloudfront_origin_access_identity.create-repo --> aws_cloudfront_distribution.s3_distribution (Reversed) Unprocessed Graph Dictionary: { "aws_cloudfront_distribution.s3_distribution": [], "aws_cloudfront_origin_access_identity.create-repo": [ "aws_cloudfront_distribution.s3_distribution" ], "aws_s3_bucket.create-repo": [ "aws_cloudfront_distribution.s3_distribution" ], "aws_s3_bucket_policy.allow_access_from_cloudfront": [ "aws_s3_bucket.create-repo" ] } Final Graphviz Input Dictionary { "aws_cloudfront_distribution.s3_distribution": [], "aws_cloudfront_origin_access_identity.create-repo": [ "aws_cloudfront_distribution.s3_distribution" ], "aws_s3_bucket.create-repo": [ "aws_cloudfront_distribution.s3_distribution" ], "aws_s3_bucket_policy.allow_access_from_cloudfront": [ "aws_s3_bucket.create-repo" ] } Output JSON Dictionary : { "aws_cloudfront_distribution.s3_distribution": [], "aws_cloudfront_origin_access_identity.create-repo": [ "aws_cloudfront_distribution.s3_distribution" ], "aws_s3_bucket.create-repo": [ "aws_cloudfront_distribution.s3_distribution" ], "aws_s3_bucket_policy.allow_access_from_cloudfront": [ "aws_s3_bucket.create-repo" ] } Exporting graph object into file architecture.json Completed! ```
Author
Owner

@Leylan commented on GitHub (May 15, 2023):

Working flawlessly now
Thank you very much

<!-- gh-comment-id:1547053929 --> @Leylan commented on GitHub (May 15, 2023): Working flawlessly now Thank you very much
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/terravision#1
No description provided.