0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Terraformの入力と出力の仕組み

Posted at

はじめに

「この値、どこから来てる?」
理解がすっと進んだ入出力の視点を、備忘も兼ねて共有します📝

全部同じパターン

プログラミングもTerraformも、本質は入力を受け取り、出力を返すだけです。

関数の場合

function calculate(x, y) {  // 引数 = 入力
  return x + y;             // 戻り値 = 出力
}

クラスの場合

class User {
  constructor(name, age) {  // コンストラクタ = 入力
    this.name = name;       // プロパティ = 出力
    this.age = age;
  }
}

Terraformの場合

module "vpc" {
  source = "./modules/vpc"

  # variables = 入力
  cidr_block = "10.0.0.0/16"
  name       = "my-vpc"
}

# output = 出力(子モジュールのoutputをリレー)
output "vpc_id" {
  value = module.vpc.vpc_id  # ← modules/vpc/outputs.tfのoutput "vpc_id"
}
ここで参照しているvpc_idの定義元

modules/vpc/outputs.tf

output "vpc_id" {
 value = aws_vpc.this.id
}

つまり、値のリレーをしているだけ

各レイヤーが入力を受け取り、処理し、次のレイヤーへ出力を渡します。

Module A (variables) → [処理] → (output)
                                   ↓
Module B (variables) ←─────────────┘
          ↓
       [処理]
          ↓
       (output) → トップレベルへ

見えるのはトップレベルだけ

Terraform CloudのUIで一覧できるのは、ルート(トップレベル)のoutputだけです。

中間モジュールのoutputは、明示的にトップまでリレーしない限り外からは見えません。

# これがないと外から見えない
output "database_endpoint" {
  value = module.database.endpoint  # 内部moduleのoutputをリレー
}

計算もできます

プログラミング同様、式や計算を書けます。

output "full_url" {
  value = "https://${var.domain}:${var.port}/api"
}

output "total_instances" {
  value = var.web_count + var.api_count
}

結論

Terraformは関数やクラスと同じ発想。
入力(variables)を受け取り、出力(output)を返す、"リレー"で構成されています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?