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 で AWS VPC を作成する

0
Posted at

はじめに

Terraform を使って AWS VPC と Subnet を作成する方法を、備忘録的に記載します。

前提環境

  • Windows 11
  • PowerShell
  • VSCode
    • Terraform の拡張機能はインストールした方が良い(HashiCorp Terraform

準備

  • Terraform のインストール
  • AWS CLI のインストール
    • アクセスキー・シークレットアクセスキーを環境変数に設定
//一時的に設定する場合
$Env:AWS_ACCESS_KEY_ID="アクセスキー"
$Env:AWS_SECRET_ACCESS_KEY="シークレットアクセスキー"

//恒久的に設定する場合
[Environment]::SetEnvironmentVariable("AWS_ACCESS_KEY_ID", "アクセスキー", "User")
[Environment]::SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", "シークレットアクセスキー", "User")

VPC 作成手順

フォルダ作成

  • 適当な場所にフォルダを作成(vpc-subnet)
    • 配下に main.tf を作成

main.tf 記述


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

provider とは?
特定のサービスや外部システムと Terraorm を連携させるための「接続ポイント」

terraform_provider.png

terraform init 実行

terraform init とは?
Terraform の初期設定を行うコマンド
→AWSプロバイダのダウンロード、バックエンドの設定、モジュールの取得など

新しいフォルダ・ファイルが作成される。

① .terraform 配下の Provider ファイル
AWS プロバイダーのバイナリ実行ファイル
例: terraform-provider-aws_v6.32.0_x5.exe

② terraform.lock.hcl
複数の環境(複数人)で Terraform を使う際にプロバイダのバージョンを揃えるためのファイル
①のハッシュ値が記載されている
例: aws プロバイダバージョン: 6.32.0

terraform_lock.png

main.tf に VPC と Subnet を定義

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

resource "aws_vpc" "terra_vpc" {
  
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "aws_vpc_name"
  }

}

resource "aws_subnet" "terra_subnet" {
  
  vpc_id = aws_vpc.terra_vpc.id

  cidr_block = "10.0.0.0/24"
  tags = {
    Name = "aws_subnet_name"
  }

}

terraform plan と apply を実行

terraform plan を実行

terraform_plan.png

terraform apply を実行

terraform_apply.png

VPC が作成されていることを確認

vpc.png

subnet.png

新しいファイルが作成されていることを確認する

  • terraform.tfstate
    • Terraform が管理している AWS リソースの情報を記載するファイル
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?