Skip to content

Instantly share code, notes, and snippets.

@rquadling
Last active December 6, 2022 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rquadling/ce42fbdf8b7b336512f80e2820c4cfab to your computer and use it in GitHub Desktop.
Save rquadling/ce42fbdf8b7b336512f80e2820c4cfab to your computer and use it in GitHub Desktop.
Display the diagnostics from Terraform in a tabular form

Terraform Diagnostics Display

As part of the upgrading from v3 to v4 of the Terraform AWS Provider, I needed to see all the places where I needed to do some work. The standard ouput from Terraform doesn't show all the places where work is required. But, luckily, using the -json option, you get access to all of the information.

The following shell script (written and tested in bash, but should be OK as really, it's only terraform, jq, column, and | that's being used) will output a nicely formatted table showing you the

terraform validate -json | jq -r '
    # Create an array of objects from the following logic
    [
      # Delete any diagnostics whose detail starts with "Experimental features"
      del(.diagnostics[] | select(.detail | startswith("Experimental features")))
      |
      # Extract just the diagnostics that remain
      .diagnostics[]
      |
      # Extract the required elements from each diagnostic into an object
      {Detail:.detail, Address:.address, Filename:.range.filename, Line:.range.start.line}
    ]
    |
    if (.[0] | length) == 0 then
      "No validation issues"
    else
      # Generate the column headings for the resultant table.
      (
        # Use the first diagnostic
        .[0]
        |
        # Get the keys in their defined order
        keys_unsorted
        |
        # For each key, create an underline and have these underlines in a new array (so, keys, then underlines).
        (
          .,
          map(length*"-")
        )
      ),
      # Join on the diagnostics to the set of column headings.
      .[]
      |
      # Get just the values.
      map(.)
      |
      # Output everything using tab separation which is the picked up by the column command that follows to make a nice
      # tabular output.
      @tsv
    end
  ' | column -ts $'\t' 

This script will output something resembling this.

Detail                                                                       Address                                                 Filename                       Line
------                                                                       -------                                                 --------                       ----
Use the aws_s3_bucket_website_configuration resource instead                 module.api_doc_website.aws_s3_bucket.cf_s3_bucket       modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_cors_configuration resource instead                    module.api_doc_website.aws_s3_bucket.cf_s3_bucket       modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_server_side_encryption_configuration resource instead  module.api_doc_website.aws_s3_bucket.cf_s3_bucket       modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_server_side_encryption_configuration resource instead  module.beta_epos_website.aws_s3_bucket.cf_s3_bucket     modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_cors_configuration resource instead                    module.beta_epos_website.aws_s3_bucket.cf_s3_bucket     modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_website_configuration resource instead                 module.beta_epos_website.aws_s3_bucket.cf_s3_bucket     modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_server_side_encryption_configuration resource instead  aws_s3_bucket.userfiles_bucket                          s3.tf                          1
Use the aws_s3_bucket_acl resource instead                                   aws_s3_bucket.userfiles_bucket                          s3.tf                          3
Use the aws_s3_bucket_server_side_encryption_configuration resource instead  module.epos_website.aws_s3_bucket.cf_s3_bucket          modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_cors_configuration resource instead                    module.epos_website.aws_s3_bucket.cf_s3_bucket          modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_website_configuration resource instead                 module.epos_website.aws_s3_bucket.cf_s3_bucket          modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_cors_configuration resource instead                    module.queue_assets_website.aws_s3_bucket.cf_s3_bucket  modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_website_configuration resource instead                 module.queue_assets_website.aws_s3_bucket.cf_s3_bucket  modules/cf_s3_website/main.tf  45
Use the aws_s3_bucket_server_side_encryption_configuration resource instead  module.queue_assets_website.aws_s3_bucket.cf_s3_bucket  modules/cf_s3_website/main.tf  45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment