🛠️

terragrunt でも値の中身を確認したい! (terragrunt render)

に公開

TL;DR

  • 「terragrunt.hcl ファイルの中で console.log したい!」あると思います (JS脳)
  • そんな時に使えるのが terragrunt render コマンド
  • terragrunt render は「実行されるテンプレートの最終的な HCL を確認するためのデバッグ専用コマンド」である

Usage example:

# terragrunt.hcl のある dir を指定するか、その dir に cd して実行する
terragrunt render --working-dir live/production/ap-northeast-1/network/

Source file:

live/production/ap-northeast-1/network/terragrunt.hcl
include "root" {
  path   = find_in_parent_folders("root.hcl")
  expose = true
}

terraform {
  source = "${include.root.locals.modules_path}/network"
}

locals {
  # この中身を知りたい!
  locals_vars = include.root.locals
}

inputs = {
  vpc_name = "prod-main-vpc"
}

Results:

locals {
  locals_vars = {
    cloud = "aws"
    cloud_vars = {
      dependencies                 = null
      dependency                   = {}
      download_dir                 = ""
      feature                      = {}
      generate                     = {}
      iam_assume_role_duration     = null
      iam_assume_role_session_name = ""
      iam_role                     = ""
      iam_web_identity_token       = ""
      inputs                       = null
      locals = {
        cloud = "aws"
      }
      terraform_binary              = ""
      terraform_version_constraint  = ""
      terragrunt_version_constraint = ""
    }
    common_tags = {
      Environment = "production"
      ManagedBy   = "Terragrunt"
    }
    env = "production"
    env_vars = {
      dependencies                 = null
      dependency                   = {}
      download_dir                 = ""
      feature                      = {}
      generate                     = {}
      iam_assume_role_duration     = null
      iam_assume_role_session_name = ""
      iam_role                     = ""
      iam_web_identity_token       = ""
      inputs                       = null
      locals = {
        account_id = "143254780325"
        env        = "production"
        env_short  = "prod"
        profile    = "app_production"
      }
      terraform_binary              = ""
      terraform_version_constraint  = ""
      terragrunt_version_constraint = ""
    }
    modules_path = "/Users/biz/app/infrastructure/modules"
    profile      = "app_production"
    region       = "ap-northeast-1"
    region_vars = {
      dependencies                 = null
      dependency                   = {}
      download_dir                 = ""
      feature                      = {}
      generate                     = {}
      iam_assume_role_duration     = null
      iam_assume_role_session_name = ""
      iam_role                     = ""
      iam_web_identity_token       = ""
      inputs                       = null
      locals = {
        availability_zones = ["ap-northeast-1a", "ap-northeast-1c"]
        region             = "ap-northeast-1"
      }
      terraform_binary              = ""
      terraform_version_constraint  = ""
      terragrunt_version_constraint = ""
    }
  }
}
terraform {
  source = "/Users/biz/app/infrastructure/modules/network"
  extra_arguments "disable_input" {
    commands  = ["apply", "import", "init", "plan", "refresh"]
    arguments = ["-input=false"]
  }
}
remote_state {
  backend = "s3"
  config = {
    bucket  = "production-app-terraform-state"
    encrypt = true
    key     = "live/production/ap-northeast-1/network/terraform.tfstate"
    profile = "app_production"
    region  = "ap-northeast-1"
    s3_bucket_tags = {
      Environment = "production"
      ManagedBy   = "Terragrunt"
      Name        = "production-app-terraform-state"
      Purpose     = "Terraform remote state storage"
    }
    use_lockfile = true
  }
}
generate "provider" {
  path        = "provider.tf"
  if_exists   = "overwrite_terragrunt"
  if_disabled = "skip"
  contents    = "terraform {\n  # https://releases.hashicorp.com/terraform/\n  required_version = \"1.13.5\"\n\n  required_providers {\n    aws = {\n      # https://registry.terraform.io/providers/hashicorp/aws/latest\n      source  = \"hashicorp/aws\"\n      version = \"6.21.0\"\n    }\n  }\n}\n\nprovider \"aws\" {\n  region  = \"ap-northeast-1\"\n  profile = \"app_production\"\n\n  default_tags {\n    tags = {\"Environment\":\"production\",\"ManagedBy\":\"Terragrunt\"}\n  }\n}\n"
}
inputs = {
  account_id         = "143254780325"
  availability_zones = ["ap-northeast-1a", "ap-northeast-1c"]
  cloud              = "aws"
  env                = "production"
  env_short          = "prod"
  profile            = "app_production"
  region             = "ap-northeast-1"
  vpc_name           = "prod-main-vpc"
}

無事 locals_vars の中身がわかりましたね。めでたしめでたし。完。

余談

  • v0.77.22 以前の古いコマンドでは terragrunt render-json だったが、CLI Redesign に伴い現在は terragrunt render になった。
  • --json option で json 出力できるので、何か他の処理に繋げたいならそれを jq と組み合わせるなどすると良いだろう。

参考文献

Discussion