Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Terragruntで快適なIaCを!

KOHEKOHE
January 19, 2025

 Terragruntで快適なIaCを!

SRE Kaigi 2025のLTのために作成した資料

KOHEKOHE

January 19, 2025
Tweet

More Decks by KOHEKOHE

Other Decks in Programming

Transcript

  1. locals { environment = "${replace(replace(replace(get_path_from_repo_root(), "environments/", ""), path_relative_to_include(), ""), "/",

    "")}" variables = read_terragrunt_config(find_in_parent_folders("envs/env_${local.environment}.hcl")) module_name = basename(path_relative_to_include()) } remote_state { backend = "s3" generate = { path = "backend.tf" if_exists = "overwrite_terragrunt" } config = { region = "ap-northeast-1" bucket = "terragrunt-state-sugar-sample-${local.environment}" key = "${path_relative_to_include()}/terraform.tfstate" encrypt = true } } (右に続く...) Terraformの難点:複数環境の設定ファイルの管理が煩雑 9 (....左の続き) generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" contents = <<EOF terraform { required_version = "= 1.9.8" required_providers { aws = { source = "hashicorp/aws" version = "5.59.0" } } } EOF } inputs = { variables = local.variables.locals } develop環境⽤の親hclファイルの例
  2. locals { environment = "${replace(replace(replace(get_path_from_repo_root(), "environments/", ""), path_relative_to_include(), ""), "/",

    "")}" variables = read_terragrunt_config(find_in_parent_folders("envs/env_${local.environment}.hcl")) module_name = basename(path_relative_to_include()) } remote_state { backend = "s3" generate = { path = "backend.tf" if_exists = "overwrite_terragrunt" } config = { region = "ap-northeast-1" bucket = "terragrunt-state-sugar-sample-${local.environment}" key = "${path_relative_to_include()}/terraform.tfstate" encrypt = true } } (右に続く...) Terraformの難点:複数環境の設定ファイルの管理が煩雑 10 (....左の続き) generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" contents = <<EOF terraform { required_version = "= 1.9.8" required_providers { aws = { source = "hashicorp/aws" version = "5.59.0" } } } EOF } inputs = { variables = local.variables.locals } develop環境⽤の親hclファイルの例 環境名をディレクトリから引っ張ってくる パスからモジュール名を取得 環境変数を格納している hclファイルを指定 inputsが子に引き継がれる tfファイルを生成する
  3. include { path = find_in_parent_folders() } terraform { source =

    "../../../modules/${path_relative_to_include()}" } Terraformの難点:複数環境の設定ファイルの管理が煩雑 11 S3⽤の⼦hclファイルの例 • inputsが親から引き継がれるので、⼦hcl⽤にわざわざ記述 しなくてよい • これを各モジュールの配下にコピペしていく