1
1

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のVPCを作成するシンプルな手順

1
Last updated at Posted at 2025-01-26

📌 概要

Terraformを使って AWSのVPC(仮想プライベートクラウド) を作成する方法を解説します ✨

VPCは AWS上のネットワークの土台 となる重要なリソースです 💡

本記事では Terraform を使ってVPCを サクッと作成する手順 を紹介します 🏃

📌 main.tf

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

📌 ディレクトリ構成

以下のようなディレクトリ構造でファイルを配置してください。

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

📌 コード

main.tf

AWS のプロバイダーを設定し、VPC とセキュリティグループのモジュールを読み込む

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

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

vpc/main.tf

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

# VPCの作成
resource "aws_vpc" "main" {
  cidr_block = "192.168.250.0/24"
  tags = {
    Name = "sandbox-terraform"
  }
}

# VPCを作成するだけなら不要です
# 関連記事で別のモジュールから本VPCのIDを参照する場合に追記してください
output "vpc_id" {
  value = aws_vpc.main.id
}

[参考リポジトリ]

📌 実行

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

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

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

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?