2026/06/11
Terraform + AWSでLaravel環境を構築する① - 初期設定・ネットワーク編
環境構築TerraformAWSLaravel
Terraformを初めて触るので、一からLaravelが動作するAWS環境を構築してみます。
公式ドキュメントを参考にしています。
https://registry.terraform.io/providers/hashicorp/aws/latest/docs
Terraformプロジェクトの準備
元となるファイルの作成
まずは元となるmain.tfファイルを作成します。
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~>6.0"
}
}
}
provider "aws" {
region = "ap-northeast-1"
}Terraformプロジェクトの初期化
main.tfのあるディレクトリで初期化コマンドを実行します。
terraform init
Initializing provider plugins found in the configuration...
- Finding hashicorp/aws versions matching "~> 6.0"...
- Installing hashicorp/aws v6.49.0...
- Installed hashicorp/aws v6.49.0 (signed by HashiCorp)
Initializing the backend...
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.初期化に成功すると.terraformファルダと.terraform.lock.hclが作成されます。
VPCを追加する
まずはVPCを追加してみます。
main.tfにvpcの設定を追加します。
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~>6.0"
}
}
}
provider "aws" {
region = "ap-northeast-1"
}
# VPCの作成
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "practice-vpc"
}
}
output "vpc_id" {
value = aws_vpc.example.id
}追加ができたら以下のコマンドでどのような変更になるか確認します。
# フォーマット
terraform fmt
# 実行計画の確認
terraform plan問題が無ければ実際に実行してみます。
terraform apply成功したら実際にAWSのダッシュボードから作成できているか確認してみましょう!
サブネットの追加
次にサブネットを追加します。
本来はプライベートサブネットも追加するべきですが、練習のためパブリックサブネットのみ追加します。
main.tfに追記してください。
resource "aws_subnet" "public_1a" {
vpc_id = aws_vpc.example.id
cidr_block = "10.0.1.0/24"
availability_zone = "ap-northeast-1a"
map_public_ip_on_launch = true
tags = {
Name = "practice-public-subnet-1a"
}
}インターネットゲートウェイの作成
VPCをインターネットに接続するためにインタネットゲートウェイを作成します。
# インターネットゲートウェイの作成
resource "aws_internet_gateway" "example" {
vpc_id = aws_vpc.example.id
tags = {
Name = "practice-igw"
}
}ルートテーブルの作成と関連付け
ルートテーブルを作成してインターネットゲートウェイのルートを追加します。
# ルートテーブルの作成
resource "aws_route_table" "public" {
vpc_id = aws_vpc.example.id
tags = {
Name = "practice-public-rt"
}
}
# ルートテーブルにインターネットゲートウェイへのルートを追加
resource "aws_route" "public_internet_access" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.example.id
}さらに、パブリックサブネットとルートテーブルを関連付けます。
# サブネットとルートテーブルの関連付け
resource "aws_route_table_association" "public_1a" {
subnet_id = aws_subnet.public_1a.id
route_table_id = aws_route_table.public.id
}ネットワーク設定の適用
ここまでできたら一度AWSに反映してみます。
terraform fmt
terraform plan
terraform applyAWSコンソールから作成したVPCへ移動して、リソースマップに追加されていたらOKです!
まとめ
ここまでTerraformプロジェクトの設定から・AWSのVPCとネットワークの設定までできました。
次回はセキュリティグループの作成を行います。