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?

More than 1 year has passed since last update.

🌍 Terraform で AWS サブネットを作成するシンプルな手順

0
Last updated at Posted at 2025-02-09

📌 概要

Terraform を使って AWS のサブネットを作成する方法 を解説します💡

VPC のネットワークを分割し、特定の範囲でインスタンスを起動できるようにするため、サブネットの作成は AWS の基本的な設定の一つです🧞‍♂️

今回は以下のVPCを パブリックとプライベートの2つのサブネットに分割していきます 💔

種類 特徴
public インターネットと通信できるサブネット(例: Webサーバー・プロキシーサーバーを配置)
private インターネットからアクセスできない内部用のサブネット(例: DB サーバーを配置)

📌 事前に準備するもの

main.tf

main.tf の作成がまだの方は以下の記事を参考にファイルを作成してください。

VPC

VPCを未作成の場合は以下の記事を参考に作成します。

📌 ディレクトリ構成

terraform-project/
│── main.tf                # ルートディレクトリのメイン設定
│── modules/
│   ├── vpc/
│   │   ├── main.tf        # VPCを定義
│   ├── subnet/
│   │   ├── main.tf        # サブネットを定義

📌 コード

main.tf

このファイルでは、Terraform のプロバイダー設定とモジュールの読み込み を行います。

VPC や サブネット をモジュール化 することで、コードの再利用性を高め管理しやすくしています。

# AWS プロバイダーの設定
provider "aws" {
  region = "ap-northeast-1" # 東京リージョン
}

# VPC モジュールの読み込み
module "vpc" {
  source = "./vpc"
}

# サブネットモジュールの読み込み
module "subnet" {
  source = "./subnet"
  vpc_id = module.vpc.vpc_id
}

vpc/main.tf

VPC(Virtual Private Cloud)を作成し、VPC の ID を出力します。

# VPC(Virtual Private Cloud)の作成
resource "aws_vpc" "main" {
  cidr_block = "192.168.250.0/24" # VPC 内で使用する IP アドレスの範囲

  tags = {
    # VPC に "sandbox-terraform" という名前タグを付与
    Name = "sandbox-terraform"
  }
}

# 作成した VPC の ID を出力
output "vpc_id" {
  value = aws_vpc.main.id
}

subnet/main.tf

VPC 内に パブリックサブネット と プライベートサブネット を作成します。

# VPC の ID を受け取る変数を定義
variable "vpc_id" {
  description = "Internet Gateway を関連付ける VPC の ID"
  type        = string
}

# プライベートサブネットの作成
# CIDR ブロック: 192.168.250.0/25(最初の半分の IP 範囲)
# アベイラビリティゾーン: ap-northeast-1a(東京リージョンの特定のゾーン)
resource "aws_subnet" "private" {
  vpc_id                  = var.vpc_id
  cidr_block              = "192.168.250.0/25"
  availability_zone       = "ap-northeast-1a"

  tags = {
    # プライベートサブネットの識別用タグ
    Name = "private"
  }
}

# パブリックサブネットの作成
# CIDR ブロック: 192.168.250.128/25(残りの半分の IP 範囲)
# アベイラビリティゾーン: ap-northeast-1a(東京リージョンの特定のゾーン)
resource "aws_subnet" "public" {
  vpc_id                  = var.vpc_id
  cidr_block              = "192.168.250.128/25"
  availability_zone       = "ap-northeast-1a"

  tags = {
    # パブリックサブネットの識別用タグ
    Name = "public"
  }
}

# 作成したパブリックサブネットの ID を出力

output "subnet_public_id" {
  value = aws_subnet.public.id
}

# 作成したプライベートサブネットの ID を出力
output "subnet_private_id" {
  value = aws_subnet.private.id
}

[参考リポジトリ]

📌 実行

準備ができたらコマンド実行しましょう!

  1. terraform init
  2. terraform plan
  3. terraform apply

Terraform関連のコマンドについては以下の記事を参考にしてください ☺️

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?