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学習ログ Part2:Terraformブロックの構成とVPC作成の基礎を理解する

0
Last updated at Posted at 2025-11-03

はじめに

今日はTerraformの主要ブロック構成と、VPC関連リソースの作成方法を整理しました。

Terraformブロック構成の基本

Terraformのコードは複数の「ブロック」から構成されます。
主に以下のようなブロックがあります。

terraform ブロック

マルチクラウド対応が可能。
モジュールを切り分けることでプロバイダーを簡単に切り替えられる。

【バージョン固定方法】

Terraform本体:required_version
プロバイダー:required_providers

provider ブロック

クラウドプロバイダー(AWSなど)を指定します。
主な設定項目は次の2つです。

・profile
・region

例:

provider "aws" {
profile = "default"
region = "ap-northeast-1"
}

data ブロック

Terraform管理外の既存リソースを参照する際に使用します。
リソースの種類によって記載方法が異なります。

例:

data "aws_vpc" "default" {
  default = true
}

output ブロック

作成したリソースの情報を外部に出力するために使用します。

例:

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

リソース参照の書き方

Terraformでは、ドット(.)を使ってリソースを参照します。

記述形式:

<BLOCK_TYPE>.<LABEL_1>.<LABEL_2>

例:
aws_vpc.main.id

組み込み関数

Terraformには多くの組み込み関数が用意されています。

  • Numeric:数値操作
  • String:文字列操作
  • Collection:リスト・マップ操作
  • Encoding:Base64などのエンコード処理
  • Filesystem:ファイル操作
  • Date & Time:日付・時間処理
  • Hash & Crypt:ハッシュ生成
  • IP Network:IPアドレス処理
  • Type Conversion:型変換

組み込み関数のテスト方法

Terraformコンソールを利用します。

terraform console

変数を使用する場合は、変数定義ファイルが存在するディレクトリで実行します。

使用例:

> substr("hello world", 1, 4)
ello

ファイル構成と分割

Terraformは、カレントディレクトリの .tf ファイルのみを読み込みます。
サブディレクトリのファイルは自動的には読み込まれません。

最小構成例:
terraform.tfvars
main.tf

main.tf が大きくなった場合は、機能ごとにファイルを分割して整理します。

VPCの作成

VPCは resource ブロックを使って作成します。

例:

resource "aws_vpc" "main" {
  cidr_block                     = "10.0.0.0/16"
  assign_generated_ipv6_cidr_block = false
  instance_tenancy               = "default"
  enable_dns_support             = true
  enable_dns_hostnames           = true
  tags = {
    Name = "main-vpc"
  }
}

使用項目:

  • cidr_block(必須)
  • assign_generated_ipv6_cidr_block(IPv6を有効化するか)
  • instance_tenancy(専有インスタンス設定)
  • enable_dns_support(DNSサポートの有効化)
  • enable_dns_hostnames(ホスト名解決の有効化)

サブネットの作成

サブネットはVPCを参照するため、依存関係があります。

例:

resource "aws_subnet" "public" {
  vpc_id                  = aws_vpc.main.id
  cidr_block              = "10.0.1.0/24"
  availability_zone       = "ap-northeast-1a"
  map_public_ip_on_launch = true
  tags = {
    Name = "public-subnet"
  }
}

使用項目:

  • vpc_id(必須)
  • cidr_block(必須)
  • availability_zone(任意)
  • map_public_ip_on_launch(EC2作成時にPublic IPを自動付与するか)

ルートテーブル

ルートテーブルは2つのリソースで構成します。
1つ目はルートテーブル本体、2つ目はサブネットとの関連付けです。

resource "aws_route_table" "main" {
  vpc_id = aws_vpc.main.id
  tags = {
    Name = "main-route-table"
  }
}

resource "aws_route_table_association" "public_assoc" {
  subnet_id      = aws_subnet.public.id
  route_table_id = aws_route_table.main.id
}

インターネットゲートウェイ

VPCをインターネットに接続するために使用します。

resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.main.id
  tags = {
    Name = "main-igw"
  }
}

resource "aws_route" "internet_access" {
  route_table_id         = aws_route_table.main.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.gw.id
}

セキュリティグループ

セキュリティグループは2つのリソースで構成します。
1つ目はグループ本体、2つ目はルール定義です。

resource "aws_security_group" "web_sg" {
  name        = "web-sg"
  description = "Allow web traffic"
  vpc_id      = aws_vpc.main.id

  tags = {
    Name = "web-sg"
  }
}

resource "aws_security_group_rule" "http_in" {
  type              = "ingress"
  from_port         = 80
  to_port           = 80
  protocol          = "tcp"
  cidr_blocks       = ["0.0.0.0/0"]
  security_group_id = aws_security_group.web_sg.id
}

プレフィックスリスト

特定のIPアドレス範囲をまとめて扱うリソースです。

resource "aws_prefix_list" "example" {
  name           = "example-list"
  prefix_list_id = "pl-12345678"
}

まとめ

・Terraformの主要ブロックの役割を整理
・組み込み関数の使い方を確認
・VPC〜サブネット〜ルート〜IGW〜SGまでの構成を理解

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?