CLOVERšŸ€

That was when it all began.

Terragrunt悒ä½æć£ć¦ć€ē’°å¢ƒć”ćØ恮Terraformć®ę§‹ęˆćƒ•ć‚”ć‚¤ćƒ«ć‚’å‰Šęø›ć™ć‚‹

恓悌ćÆ态ćŖć«ć‚’ć—ćŸćć¦ę›øć„ćŸć‚‚ć®ļ¼Ÿ

Terragruntć‚’å­¦ć¶ć‚·ćƒŖćƒ¼ć‚ŗ怂

今回ćÆ态Terragrunt悒ä½æć£ć¦ē’°å¢ƒć”ćØć«ä½œć‚‹Terraformć®ę§‹ęˆćƒ•ć‚”ć‚¤ćƒ«ć‚’ęø›ć‚‰ć—恦ćæć¾ć™ć€‚

Terragrunt恧态ē’°å¢ƒć”ćØć®ę§‹ęˆćƒ•ć‚”ć‚¤ćƒ«ć‚’ęø›ć‚‰ć™

Terraform悒ä½æć£ć¦ć€č¤‡ę•°ć®ē’°å¢ƒć‚’꧋ēÆ‰ć€ē®”ē†ć™ć‚‹ć®ć«ćÆć„ćć¤ć‹ć®ę–¹ę³•ćŒć‚ć‚Šć¾ć™ć€‚

Terraformč‡Ŗčŗ«ćŒęä¾›ć™ć‚‹ć®ćÆ态Workspace恧恙恭怂

State: Workspaces - Terraform by HashiCorp

WorkspacećÆStateć‚’åˆ‡ć‚Šę›æ恈悋恓ćØćŒć§ćć‚‹ć‚‚ć®ć§ć€ć“ć”ć‚‰ćØVariablesćŖ恩恧ē’°å¢ƒå·®ē•°ć‚’č”Øē¾ć™ć‚‹ć“ćØ恫ćŖć‚Šć¾ć™ć€‚

他恫ćÆć€å‰²ć‚Šåˆ‡ć£ć¦ē’°å¢ƒć”ćØ恫Terraformę§‹ęˆćƒ•ć‚”ć‚¤ćƒ«ć‚’ē‹¬ē«‹ć—恦ē®”ē†ć™ć‚‹ę–¹ę³•ć‚‚ć‚ć‚Šć¾ć™ć€‚ć“ć®ę–¹ę³•ć ćØć€ćƒ•ć‚”ć‚¤ćƒ«ć®ć‚³ćƒ”ćƒ¼ćŒå¢—ćˆć‚‹ć“ćØ恫
ćŖć‚Šć¾ć™ć­ć€‚

ć”ć‚‡ć†ć©ć€ä»Šå›žä½æ恆Terragruntć®ćƒ‰ć‚­ćƒ„ćƒ”ćƒ³ćƒˆć«ć‚‚ä¾‹ćŒć‚ć‚Šć¾ć™ć€‚

Quick start / Promote immutable, versioned Terraform modules across environments

ć“ć†ć„ć†ę„Ÿć˜ć®ć‚‚ć®ć§ć™ć€‚ē’°å¢ƒć”ćØć«ćƒ•ć‚”ć‚¤ćƒ«ćŒåˆ†é›¢ć•ć‚Œć¦ć„ć¾ć™ć—ć€State悂恝悌恞悌ē‹¬ē«‹ć—恦ē®”ē†ć•ć‚Œć¾ć™ć€‚

ā”œā”€ā”€ prod
ā”‚   ā”œā”€ā”€ app
ā”‚   ā”‚   ā”œā”€ā”€ main.tf
ā”‚   ā”‚   ā””ā”€ā”€ outputs.tf
ā”‚   ā”œā”€ā”€ mysql
ā”‚   ā”‚   ā”œā”€ā”€ main.tf
ā”‚   ā”‚   ā””ā”€ā”€ outputs.tf
ā”‚   ā””ā”€ā”€ vpc
ā”‚       ā”œā”€ā”€ main.tf
ā”‚       ā””ā”€ā”€ outputs.tf
ā”œā”€ā”€ qa
ā”‚   ā”œā”€ā”€ app
ā”‚   ā”‚   ā”œā”€ā”€ main.tf
ā”‚   ā”‚   ā””ā”€ā”€ outputs.tf
ā”‚   ā”œā”€ā”€ mysql
ā”‚   ā”‚   ā”œā”€ā”€ main.tf
ā”‚   ā”‚   ā””ā”€ā”€ outputs.tf
ā”‚   ā””ā”€ā”€ vpc
ā”‚       ā”œā”€ā”€ main.tf
ā”‚       ā””ā”€ā”€ outputs.tf
ā””ā”€ā”€ stage
    ā”œā”€ā”€ app
    ā”‚   ā”œā”€ā”€ main.tf
    ā”‚   ā””ā”€ā”€ outputs.tf
    ā”œā”€ā”€ mysql
    ā”‚   ā”œā”€ā”€ main.tf
    ā”‚   ā””ā”€ā”€ outputs.tf
    ā””ā”€ā”€ vpc
        ā”œā”€ā”€ main.tf
        ā””ā”€ā”€ outputs.tf

Terragruntć®å “åˆć§ćÆ态ē’°å¢ƒć”ćØć«ćƒ•ć‚”ć‚¤ćƒ«ć‚’ē‹¬ē«‹ć•ć›ć‚‹č€ƒćˆć‚’å–ć‚Šć¤ć¤ć‚‚ć€ćƒŖć‚½ćƒ¼ć‚¹å®šē¾©ćÆćƒ¢ć‚øćƒ„ćƒ¼ćƒ«åŒ–ć—ć¦å‚ē…§ć™ć‚‹ę–¹ę³•ć‚’ꏐē¤ŗć—ć¦ć„ć¾ć™ć€‚
ć•ć‚‰ć«ć€ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć«åÆ¾ć™ć‚‹Variable恮čØ­å®šć‚’Terraformć®ę§‹ęˆćƒ•ć‚”ć‚¤ćƒ«ćŖć—ć§å®Ÿē¾ć§ćć¾ć™ć€‚

ćć®ć‚ćŸć‚ŠćŒę›øć‹ć‚Œć¦ć„ć‚‹ć®ćŒć€ć“ć”ć‚‰ć®ćƒ‰ć‚­ćƒ„ćƒ”ćƒ³ćƒˆć§ć™ć€‚

Quick start / Promote immutable, versioned Terraform modules across environments

Keep your Terraform code DRY

Terragrunt悒ä½æ恆ćØ态ćƒŖć‚½ćƒ¼ć‚¹å®šē¾©ć‚’ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć«ć¾ćØ悁恟äøŠć§ć€ć“ć†ć„ć£ćŸå½¢ć§.tfćƒ•ć‚”ć‚¤ćƒ«ćŖ恗恧č”Øē¾ć§ćć¾ć™ć€‚

ā”œā”€ā”€ prod
ā”‚   ā”œā”€ā”€ app
ā”‚   ā”‚   ā””ā”€ā”€ terragrunt.hcl
ā”‚   ā”œā”€ā”€ mysql
ā”‚   ā”‚   ā””ā”€ā”€ terragrunt.hcl
ā”‚   ā””ā”€ā”€ vpc
ā”‚       ā””ā”€ā”€ terragrunt.hcl
ā”œā”€ā”€ qa
ā”‚   ā”œā”€ā”€ app
ā”‚   ā”‚   ā””ā”€ā”€ terragrunt.hcl
ā”‚   ā”œā”€ā”€ mysql
ā”‚   ā”‚   ā””ā”€ā”€ terragrunt.hcl
ā”‚   ā””ā”€ā”€ vpc
ā”‚       ā””ā”€ā”€ terragrunt.hcl
ā””ā”€ā”€ stage
    ā”œā”€ā”€ app
    ā”‚   ā””ā”€ā”€ terragrunt.hcl
    ā”œā”€ā”€ mysql
    ā”‚   ā””ā”€ā”€ terragrunt.hcl
    ā””ā”€ā”€ vpc
        ā””ā”€ā”€ terragrunt.hcl

今回ćÆć€ć“ć”ć‚‰ć®ę©Ÿčƒ½ć‚’č¦‹ć¦ć„ć“ć†ćØę€ć„ć¾ć™ć€‚

ē’°å¢ƒ

ä»Šå›žć®ē’°å¢ƒćÆ态恓恔悉恧恙怂

$ terraform version
Terraform v0.14.7


$ terragrunt -v
terragrunt version v0.28.7

Terraform ProviderćÆ态MySQLē”Ø恮悂恮悒ä½æē”Øć—ć¾ć™ć€‚

Provider: MySQL - Terraform by HashiCorp

ä½æē”Ø恙悋MySQLćÆ8.0.23ćØ恗态172.17.0.2ćŠć‚ˆć³172.17.0.3ć§å‹•ä½œć—ć¦ć„ć‚‹ć‚‚ć®ćØć—ć¾ć™ć€‚

ć¾ćŸć€ä»Šå›žćÆGitćƒŖ惝ć‚ø惈ćƒŖ恌åæ…要恫ćŖć‚Šć¾ć™ć€‚GitćƒŖ惝ć‚ø惈ćƒŖć®ć‚½ćƒ•ćƒˆć‚¦ć‚§ć‚¢ćÆć‚ć¾ć‚Šé–¢äæ‚ć‚ć‚Šć¾ć›ć‚“ćŒć€GitLab 13.9悒ä½æć„ć¾ć™ć€‚
GitLabćÆ192.168.0.3ć§å‹•ä½œć—ć¦ć„ć‚‹ć‚‚ć®ćØć—ć¾ć™ć€‚

ćŠé”Œ

MySQL Provider悒ä½æć£ćŸć€ä»„äø‹ć®2ć¤ć®ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’å®šē¾©ć—ć¾ć™ć€‚

  • ćƒ‡ćƒ¼ć‚æćƒ™ćƒ¼ć‚¹
  • ćƒ¦ćƒ¼ć‚¶ćƒ¼ćŠć‚ˆć³ęة限

恓悌悉悒ē’°å¢ƒåˆ„ć®ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖć‹ć‚‰å‚ē…§ć—ć€ćć‚Œćžć‚Œä½æē”Øć™ć‚‹ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ćÆåŒć˜ć€ę§‹ēÆ‰ć™ć‚‹ęƒ…å ±ļ¼ˆļ¼Variableļ¼‰ćÆē•°ćŖ悋ēŠ¶ę…‹ć‚’実ē¾ć—恦ćæć¾ć™ć€‚

ē’°å¢ƒćÆdevelopmentćØproduction恮2恤ćØ恗态ćƒŖć‚½ćƒ¼ć‚¹ę§‹ēÆ‰å…ˆć®MySQLćÆdevelopmentē”Ø悒172.17.0.2态productionē”Ø悒172.17.0.3ćØć—ć¾ć™ć€‚

ć¾ćŸć€ä»Šå›žćÆTerragrunt悒ä½æ悏ćŖ恄惑ć‚æćƒ¼ćƒ³ćÆä½œć‚Šć¾ć›ć‚“ć€‚é•·ććŖ悋恮恧ā€¦ć€‚

恧ćÆć€å§‹ć‚ć¦ć„ćć¾ć—ć‚‡ć†ć€‚

Terraformćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ä½œęˆć™ć‚‹

今回ćÆ态Terraformćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ćŒćŖ恄ćØå§‹ć¾ć‚Šć¾ć›ć‚“ć€‚

GitLabäøŠć«ćƒŖ惝ć‚ø惈ćƒŖć‚’ä½œęˆć—ć¦ļ¼ˆåå‰ćÆmysql-terraform-moduleć«ć—ć¾ć—ćŸļ¼‰ć€ć“ć”ć‚‰ć®ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ē™»éŒ²ć—ć¾ć™ć€‚

git clone恗恦

$ git clone http://192.168.0.3/kazuhira/mysql-terraform-module.git
$ cd mysql-terraform-module

ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ē”Øć®ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖć‚’ä½œęˆć€‚

$ mkdir -p modules/{database,users}

仄äø‹ć®ć‚ˆć†ćŖę§‹ęˆć«ć—ć¾ć™ć€‚

$ tree
.
ā””ā”€ā”€ modules
    ā”œā”€ā”€ database
    ā”‚Ā Ā  ā”œā”€ā”€ main.tf
    ā”‚Ā Ā  ā”œā”€ā”€ outputs.tf
    ā”‚Ā Ā  ā”œā”€ā”€ variables.tf
    ā”‚Ā Ā  ā””ā”€ā”€ versions.tf
    ā””ā”€ā”€ users
        ā”œā”€ā”€ main.tf
        ā”œā”€ā”€ outputs.tf
        ā”œā”€ā”€ variables.tf
        ā””ā”€ā”€ versions.tf

3 directories, 8 files

ćƒ•ć‚”ć‚¤ćƒ«ćÆ态恝悌恞悌恓悓ćŖꄟ恘恧恙怂

ćƒ‡ćƒ¼ć‚æćƒ™ćƒ¼ć‚¹ē”Ø怂

modules/database/main.tf

resource "mysql_database" "this" {
  name                  = var.database_name
  default_character_set = var.default_character_set
  default_collation     = var.default_collation
}

åæ…é ˆćŖ恮ćÆć€ćƒ‡ćƒ¼ć‚æćƒ™ćƒ¼ć‚¹åć ć‘ć«ć—ć¾ć—ćŸć€‚

modules/database/variables.tf

variable "database_name" {
  type = string
}

variable "default_character_set" {
  type    = string
  default = "utf8mb4"
}

variable "default_collation" {
  type    = string
  default = "utf8mb4_ja_0900_as_cs_ks"
}

modules/database/outputs.tf

output "database_name" {
  value = mysql_database.this.name
}

modules/database/versions.tf

terraform {
  required_version = ">= 0.14.7"

  required_providers {
    mysql = {
      source  = "terraform-providers/mysql"
      version = ">= 1.9.0"
    }
  }
}

ćƒ¦ćƒ¼ć‚¶ćƒ¼ćŠć‚ˆć³ęة限ē”Ø怂

ē®”ē†ē”Øć®ćƒ¦ćƒ¼ć‚¶ćƒ¼ćØć€ć‚¢ćƒ—ćƒŖć‚±ćƒ¼ć‚·ćƒ§ćƒ³ćƒ¦ćƒ¼ć‚¶ćƒ¼ć®2ēØ®é”žć‚’ä½œęˆć—ć¾ć™ć€‚

modules/users/main.tf

resource "mysql_user" "administrator" {
  user               = var.administrator_username
  plaintext_password = var.administrator_password
  host               = var.administrator_allow_host
}

resource "mysql_grant" "administrator" {
  user       = mysql_user.administrator.user
  host       = mysql_user.administrator.host
  database   = var.database_name
  privileges = ["ALL"]
}

resource "mysql_user" "application_user" {
  user               = var.application_user_username
  plaintext_password = var.application_user_password
  host               = var.application_user_allow_host
}

resource "mysql_grant" "application_user" {
  user       = mysql_user.application_user.user
  host       = mysql_user.application_user.host
  database   = var.database_name
  privileges = ["SELECT", "INSERT", "UPDATE", "DELETE"]
}

åæ…é ˆć®ć‚‚ć®ćÆć€ćƒ‡ćƒ¼ć‚æćƒ™ćƒ¼ć‚¹åćØć€å„ćƒ¦ćƒ¼ć‚¶ćƒ¼ć®ćƒ‘ć‚¹ćƒÆćƒ¼ćƒ‰ć«ć—ć¾ć—ćŸć€‚

modules/users/variables.tf

variable "database_name" {
  type = string
}

variable "administrator_username" {
  type    = string
  default = "admin"
}

variable "administrator_password" {
  type = string
}

variable "administrator_allow_host" {
  type    = string
  default = "%"
}

variable "application_user_username" {
  type    = string
  default = "appuser"
}

variable "application_user_password" {
  type = string
}

variable "application_user_allow_host" {
  type    = string
  default = "%"
}

modules/users/outputs.tf

output "administrator_username" {
  value = mysql_user.administrator.user
}

output "administrator_password" {
  value     = mysql_user.administrator.plaintext_password
  sensitive = true
}

output "administrator_allow_host" {
  value = mysql_user.administrator.host
}

output "application_user_username" {
  value = mysql_user.application_user.user
}

output "application_user_password" {
  value     = mysql_user.application_user.plaintext_password
  sensitive = true
}

output "application_user_allow_host" {
  value = mysql_user.application_user.host
}

modules/users/versions.tf

terraform {
  required_version = ">= 0.14.7"

  required_providers {
    mysql = {
      source  = "terraform-providers/mysql"
      version = ">= 1.9.0"
    }
  }
}

ä½œęˆć—ćŸćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ć€GitćƒŖ惝ć‚ø惈ćƒŖ恫ē™»éŒ²ć—ć¾ć™ć€‚

$ git add modules
$ git commit -m 'add, modules'
$ git push origin master

ć‚æć‚°ć‚‚ć¤ć‘ć¦ćŠćć¾ć—ć‚‡ć†ć€‚

$ git tag v0.0.1
$ git push origin v0.0.1

ć“ć“ć¾ć§ćÆć€å˜ć«Terraformćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ä½œęˆć—ćŸć ć‘ć§ć™ć­ć€‚

ē’°å¢ƒåˆ„ć«ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’åˆ©ē”Ø恗态恋恤Terraformę§‹ęˆćƒ•ć‚”ć‚¤ćƒ«ćÆä½œęˆć—ćŖ恄

恓恓恋悉态Terragrunt恌å‡ŗć¦ćć¾ć™ć€‚

ć¾ćšćÆē’°å¢ƒē”Øć®ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖćØć€å„ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ä½æć†ćŸć‚ć®ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖć‚’ä½œęˆć—ć¾ć—ć‚‡ć†ć€‚

$ mkdir -p environments/{development,production}
$ mkdir -p environments/development/{database,users}
$ mkdir -p environments/production/{database,users}

꜀ēµ‚ēš„ć«ć€ć§ćć‚ćŒć£ćŸćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖćŠć‚ˆć³ćƒ•ć‚”ć‚¤ćƒ«ćÆ态恓悓ćŖę„Ÿć˜ć§ć™ć€‚ć„ćšć‚Œć®ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖć«ć‚‚ćƒ•ć‚”ć‚¤ćƒ«ćÆterragrunt.hcl恗恋
存åœØć—ć¾ć›ć‚“ć€‚

$ tree environments
environments
ā”œā”€ā”€ development
ā”‚Ā Ā  ā”œā”€ā”€ database
ā”‚Ā Ā  ā”‚Ā Ā  ā””ā”€ā”€ terragrunt.hcl
ā”‚Ā Ā  ā”œā”€ā”€ terragrunt.hcl
ā”‚Ā Ā  ā””ā”€ā”€ users
ā”‚Ā Ā      ā””ā”€ā”€ terragrunt.hcl
ā””ā”€ā”€ production
    ā”œā”€ā”€ database
    ā”‚Ā Ā  ā””ā”€ā”€ terragrunt.hcl
    ā”œā”€ā”€ terragrunt.hcl
    ā””ā”€ā”€ users
        ā””ā”€ā”€ terragrunt.hcl

6 directories, 6 files

developmentćØåä»˜ć‘ćŸę–¹ć®ē’°å¢ƒć‹ć‚‰č¦‹ć¦ć„ćć¾ć—ć‚‡ć†ć€‚

$ cd environments/development

ć“ć®ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖ恫恂悋terragrunt.hclćƒ•ć‚”ć‚¤ćƒ«ć®äø­čŗ«ćÆ态恓悓ćŖꄟ恘恧恙怂

terragrunt.hcl

generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
provider "mysql" {
  endpoint = "172.17.0.2:3306"
  username = "root"
  password = "password"
}
EOF
}

Providerć®å®šē¾©ć‚’ć¾ćØć‚ć‚‹ę©Ÿčƒ½ć‚’ä½æć£ć¦ć„ć¾ć™ć€‚ćƒ‡ćƒ¼ć‚æćƒ™ćƒ¼ć‚¹ē”Øć€ćƒ¦ćƒ¼ć‚¶ćƒ¼ćŠć‚ˆć³ęة限ē”Øć®ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ćŒć‚ć‚Šć¾ć™ćŒć€ęŽ„ē¶šå…ˆć®MySQLć‚µćƒ¼ćƒćƒ¼ćÆ
ć©ć”ć‚‰ć‚‚åŒć˜ćŖ恮恧怂

Terragruntを使って、TerraformのProvider定義をまとめてみる - CLOVER🍀

ćƒ‡ćƒ¼ć‚æćƒ™ćƒ¼ć‚¹å“ć«ē§»å‹•ć—ć¾ć™ć€‚

$ cd database

ć“ć”ć‚‰ć«é…ē½®ć—恦恄悋terragrunt.hcl恮äø­čŗ«ć§ć™ć€‚

terragrunt.hcl

include {
  path = find_in_parent_folders()
}

terraform {
  source = "git::http://192.168.0.3/kazuhira/mysql-terraform-module.git//modules/database?ref=v0.0.1"
}

inputs = {
  database_name = "development_database"
}

Providerć®å®šē¾©ć‚’参ē…§ć™ć‚‹ćŸć‚ć«ć€äøŠä½ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖ恫恂悋terragrunt.hclćƒ•ć‚”ć‚¤ćƒ«ć‚’å‚ē…§ć™ć‚‹ć‚ˆć†ć«ć—ć¦ć„ć¾ć™ć€‚

include {
  path = find_in_parent_folders()
}

terraform惖惭惃ć‚Æ恮sourceå±žę€§ćÆć€ć“ć®ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖ恧ä½æć†ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ęŒ‡å®šć—ć¾ć™ć€‚

terraform {
  source = "git::http://192.168.0.3/kazuhira/mysql-terraform-module.git//modules/database?ref=v0.0.1"
}

Configuration Blocks and Attributes / terraform

čؘčæ°ę–¹ę³•č‡Ŗ体ćÆ态通åøø恮TerraformćØåŒć˜ć§ć™ć­ć€‚//仄äø‹ć§ć€GitćƒŖ惝ć‚ø惈ćƒŖå†…ć®ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ęŒ‡å®šć—ć¦ć„ć¾ć™ć€‚ć‚æ悰ćÆrefć§ęŒ‡å®šć—ć¾ć™ć€‚

Module Sources - Terraform by HashiCorp

ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć«äøŽćˆć‚‹VariablećÆ态inputsć§ęŒ‡å®šć—ć¾ć™ć€‚

inputs = {
  database_name = "development_database"
}

Configuration Blocks and Attributes / inputs

ć“ć“ć¾ć§ć§ć€ć“ć®ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖ恫恤恄恦ćÆęŗ–å‚™ćŒć§ććŸć®ć§ć€apply恗恦ćæć¾ć—ć‚‡ć†ć€‚

$ terragrunt apply

ćƒŖć‚½ćƒ¼ć‚¹ćŒć§ćć¾ć—ćŸć€‚

mysql_database.this: Creating...
mysql_database.this: Creation complete after 0s [id=development_database]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

database_name = "development_database"

恔ćŖćæć«ć€ć“ć®ę™‚ć®ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖå†…ć®ćƒ•ć‚”ć‚¤ćƒ«ć‚„ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖćÆć€ć“ć®ć‚ˆć†ć«ćŖć£ć¦ć„ć¾ć™ć€‚

$ ll
合č؈ 20
drwxrwxr-x 3 xxxxx xxxxx 4096  2꜈ 24 00:05 ./
drwxrwxr-x 4 xxxxx xxxxx 4096  2꜈ 23 22:05 ../
-rw-r--r-- 1 xxxxx xxxxx 1194  2꜈ 24 00:05 .terraform.lock.hcl
drwxrwxr-x 3 xxxxx xxxxx 4096  2꜈ 24 00:05 .terragrunt-cache/
-rw-rw-r-- 1 xxxxx xxxxx  218  2꜈ 23 23:15 terragrunt.hcl

ē¶šć„ć¦ć€ćƒ¦ćƒ¼ć‚¶ćƒ¼ćŠć‚ˆć³ęة限偓ćø怂

$ cd ../users

terragrunt.hclćÆ态恓恔悉怂ä½æć„ę–¹č‡Ŗ体ćÆć€ćƒ‡ćƒ¼ć‚æćƒ™ćƒ¼ć‚¹å“ćØåŒć˜ć§ć™ć€‚

terragrunt.hcl

include {
  path = find_in_parent_folders()
}

terraform {
  source = "git::http://192.168.0.3/kazuhira/mysql-terraform-module.git//modules/users?ref=v0.0.1"
}

inputs = {
  database_name = "development_database"

  administrator_password    = "admin_password"
  application_user_password = "appuser_password"
}

applyć—ć¦ćŠćć¾ć—ć‚‡ć†ć€‚

$ terragrunt apply

ćƒŖć‚½ćƒ¼ć‚¹ćŒć§ćć¾ć—ćŸć€‚

mysql_user.administrator: Creating...
mysql_user.application_user: Creating...
mysql_user.application_user: Creation complete after 0s [id=appuser@%]
mysql_grant.application_user: Creating...
mysql_user.administrator: Creation complete after 0s [id=admin@%]
mysql_grant.administrator: Creating...
mysql_grant.application_user: Creation complete after 0s [id=appuser@%:`development_database`]
mysql_grant.administrator: Creation complete after 0s [id=admin@%:`development_database`]

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

Outputs:

administrator_allow_host = "%"
administrator_password = <sensitive>
administrator_username = "admin"
application_user_allow_host = "%"
application_user_password = <sensitive>
application_user_username = "appuser"

productionå“ć®å†…å®¹ć‚‚č¦‹ć¦ćæć¾ć—ć‚‡ć†ć€‚ćć©ććŖ悋恮恧态applyć®å®Ÿč”Œć®ę§˜å­ćÆč¼‰ć›ć¾ć›ć‚“ć€‚

$ cd ../../production

Providerć®å®šē¾©ć‚’恗恦恄悋态terragrunt.hcl怂development恮ꙂćØćÆ态ꎄē¶šå…ˆćŒē•°ćŖć‚Šć¾ć™ć€‚

terragrunt.hcl

generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
provider "mysql" {
  endpoint = "172.17.0.3:3306"
  username = "root"
  password = "password"
}
EOF
}

ćƒ‡ćƒ¼ć‚æćƒ™ćƒ¼ć‚¹å“ć€‚

database/terragrunt.hcl

include {
  path = find_in_parent_folders()
}

terraform {
  source = "git::http://192.168.0.3/kazuhira/mysql-terraform-module.git//modules/database?ref=v0.0.1"
}

inputs = {
  database_name = "production_database"
}

ćƒ¦ćƒ¼ć‚¶ćƒ¼ćŠć‚ˆć³ęØ©é™å“ć€‚

users/terragrunt.hcl

include {
  path = find_in_parent_folders()
}

terraform {
  source = "git::http://192.168.0.3/kazuhira/mysql-terraform-module.git//modules/users?ref=v0.0.1"
}

inputs = {
  database_name = "production_database"

  administrator_password    = "rZRuLj0xx2Z1M"
  application_user_password = "gSudLAJt4icZL"
}

ć“ć‚Œć§ć€åŒć˜ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ä½æć£ć¦ć€ćƒŖć‚½ćƒ¼ć‚¹ć‚’ę§‹ēÆ‰ć™ć‚‹ćŸć‚ć®čØ­å®šćŒē•°ćŖ悋ćØ恄恆ēŠ¶ę…‹ć‚’态Terraformę§‹ęˆćƒ•ć‚”ć‚¤ćƒ«ćŖć—ć§å®Ÿē¾ć§ćć¾ć—ćŸć€‚

State悄provider.tfćÆļ¼Ÿ

developmentć®ćƒ‡ćƒ¼ć‚æćƒ™ćƒ¼ć‚¹å“ć®å®šē¾©ć«ęˆ»ć£ć¦ćæć¾ć™ć€‚

$ cd ../../development/database

å…ˆć»ć©Terragrunt悒ä½æć£ć¦applyć—ćŸę™‚ć«ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖ悒态provider.tfćŒć‚ć‚Šć¾ć›ć‚“ć§ć—ćŸć€‚ä»Šå›žćÆTerraform Backend悒ē‰¹ć«čØ­å®šć—ć¦
恄ćŖ恄恮恧态StatećŒćƒ•ć‚”ć‚¤ćƒ«ć§ē®”ē†ć•ć‚Œć¦ć„ć‚‹ćÆ恚ćŖć®ć§ć™ćŒć€ćć‚Œć‚‚ć‚ć‚Šć¾ć›ć‚“ć€‚

$ ll
合č؈ 20
drwxrwxr-x 3 xxxxx xxxxx 4096  2꜈ 24 00:05 ./
drwxrwxr-x 4 xxxxx xxxxx 4096  2꜈ 23 22:05 ../
-rw-r--r-- 1 xxxxx xxxxx 1194  2꜈ 24 00:05 .terraform.lock.hcl
drwxrwxr-x 3 xxxxx xxxxx 4096  2꜈ 24 00:05 .terragrunt-cache/
-rw-rw-r-- 1 xxxxx xxxxx  218  2꜈ 23 23:15 terragrunt.hcl

provider.tf恫恤恄恦ćÆ态č¦Ŗćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖ恫恂悋terragrunt.tfć®å†…å®¹ć‹ć‚‰ć—ć¦ć€ć“ć”ć‚‰ć®ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖć«ä½œęˆć•ć‚Œć¦ć„ć‚‹ć®ć§ćÆļ¼ŸćØę€ć†ć®ć§ć™ćŒć€‚

実ćÆ态恓悓ćŖćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖć«ć§ćć¦ć„ćŸć‚Šć—ć¾ć™ć€‚

.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/provider.tf

# Generated by Terragrunt. Sig: nIlQXj57tbuaRZEa
provider "mysql" {
  endpoint = "172.17.0.2:3306"
  username = "root"
  password = "password"
}

恓恆恄恆ēŠ¶ę…‹ć§ć™ć€‚

$ tree .terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4
ā””ā”€ā”€ modules
    ā”œā”€ā”€ database
    ā”‚Ā Ā  ā”œā”€ā”€ main.tf
    ā”‚Ā Ā  ā”œā”€ā”€ outputs.tf
    ā”‚Ā Ā  ā”œā”€ā”€ provider.tf
    ā”‚Ā Ā  ā”œā”€ā”€ terraform.tfstate
    ā”‚Ā Ā  ā”œā”€ā”€ terragrunt.hcl
    ā”‚Ā Ā  ā”œā”€ā”€ variables.tf
    ā”‚Ā Ā  ā””ā”€ā”€ versions.tf
    ā””ā”€ā”€ users
        ā”œā”€ā”€ main.tf
        ā”œā”€ā”€ outputs.tf
        ā”œā”€ā”€ variables.tf
        ā””ā”€ā”€ versions.tf

3 directories, 11 files

Stateć¾ć§ć€ć“ć”ć‚‰ć«å…„ć£ć¦ć„ć‚‹ć‚“ć§ć™ć­ć€‚

find恙悋ćØ态恓悓ćŖę„Ÿć˜ć«ćŖć£ć¦ć„ć¾ć™ć€‚
ā€» .gitćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖ除恏

.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/.terragrunt-source-version
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/users/outputs.tf
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/users/versions.tf
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/users/main.tf
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/users/variables.tf
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/outputs.tf
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/provider.tf
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/terraform.tfstate
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/versions.tf
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/main.tf
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/terragrunt.hcl
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/.terraform.lock.hcl
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/.terragrunt-module-manifest
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/variables.tf
.terragrunt-cache/ro7akS-zy30K33w0IWWX0m8k7Jk/yKALuQR6CZfAIYeyffGdzMPkAo4/modules/database/.terraform/providers/registry.terraform.io/terraform-providers/mysql/1.9.0/linux_amd64/terraform-provider-mysql_v1.9.0_x4

実ćÆć€ć“ć®ę§‹ęˆć ćØterragrunt applyćŖć©ć‚’å®Ÿč”Œć™ć‚‹ę™‚ć«.terragrunt-cache配äø‹ć®äø€ę™‚ćƒ‡ć‚£ćƒ¬ć‚Æ惈ćƒŖ恫ē§»å‹•ć—ć¦ć„ć‚‹ć‚ˆć†ćŖ恮恧恙怂

Keep your Terraform code DRY / Important gotcha: working with relative file paths

ć“ć®ćŸć‚ć€-var-file悄terragrunt.hclå†…ć§ę›øććƒ‘ć‚¹ćÆ态ēµ¶åÆ¾ćƒ‘ć‚¹ć«ć—ć¦ćŠć‹ćŖ恄ćØć†ć¾ćć„ć‹ćŖ恄悈恆恧恙怂

ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ćŒę›“ę–°ć•ć‚ŒćŸå “åˆ

ć“ć”ć‚‰ć®å†…å®¹ć§ć™ć€‚

Keep your Terraform code DRY / Important gotcha: Terragrunt caching

sourceć®ęŒ‡å®šć‚’ćƒŖćƒ¢ćƒ¼ćƒˆć«ć—ćŸå “åˆć€TerragruntćÆćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć®ćƒ€ć‚¦ćƒ³ćƒ­ćƒ¼ćƒ‰ć‚’1åŗ¦ć—ć‹č”Œć„ć¾ć›ć‚“ć€‚

terraform init -upgrade态terragrunt init -upgradećŖć©ć—ć¦ć‚‚ć€ę„å‘³ćŒć‚ć‚Šć¾ć›ć‚“ć€‚

ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’å†åŗ¦ćƒ€ć‚¦ćƒ³ćƒ­ćƒ¼ćƒ‰ć™ć‚‹ć«ćÆ态--terragrunt-source-updateć‚Ŗćƒ—ć‚·ćƒ§ćƒ³ć‚’ä½æē”Øć—ć¾ć™ć€‚

$ terragrunt apply --terragrunt-source-update

ćƒ­ćƒ¼ć‚«ćƒ«ć®ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ä½æć†å “åˆ

--terragrunt-sourceć‚Ŗćƒ—ć‚·ćƒ§ćƒ³ć‚’ä½æē”Ø恙悋恓ćØ恧态äø€ę™‚ēš„恫sourceć§ęŒ‡å®šć—ć¦ć„ć‚‹ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć®å‚ē…§å…ˆć‚’å¤‰ę›“恧恍悋悈恆恧恙怂

Keep your Terraform code DRY / Working locally

ć“ć‚Œć§ć€ćƒ­ćƒ¼ć‚«ćƒ«ć®ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’åˆ©ē”Øć—ć¦ä½œę„­ćŒć§ćć¾ć™ć€ćØ怂

ć¾ćØ悁

Terragrunt悒ä½æć£ćŸć€ē’°å¢ƒć”ćØ恮ćƒŖć‚½ćƒ¼ć‚¹å®šē¾©ę–¹ę³•ļ¼ˆļ¼Ÿļ¼‰ć‚’見恦ćæć¾ć—ćŸć€‚

恩恆ćŖ悓恧恗悇恆恭态ē¢ŗć‹ć«ć™ć”ćć™ć£ćć‚Šć—ćŸę§‹ęˆć«ćŖć‚Šć¾ć™ć—ć€ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ćƒŖ惝ć‚ø惈ćƒŖē®”ē†ć—ć¦å‚ē…§ć™ć‚‹ć®ć‚‚č‰Æ恄ćØę€ć†ć®ć§ć™ćŒć€‚

terragrunt.hcl悒ä½æć£ć¦å¤‰ę›“ć§ćć‚‹ēÆ„å›²ćŒć€ć”ć‚‡ć£ćØ少ćŖ恄悈恆ćŖā€¦ć€‚

ē’°å¢ƒå·®ē•°ćŒVariables恧åøåŽć§ćć‚‹ēƄ囲ćŖć‚‰ć€ęœ‰åŠ¹ćŖ恮恧恗悇恆恋ļ¼Ÿ

Workspace悒ä½æ悏ćŖć„ć€ć“ć®č€ƒćˆę–¹č‡Ŗ体ćÆęŠ¼ć•ćˆć¦ćŠć„ćŸę–¹ćŒć‚ˆć„ć‹ćŖćØę€ć„ć¾ć™ć€‚