search
LoginSignup
0

posted at

updated at

Organization

TerraformでVPC・サブネット・インターネットGWを作成する

TerraformでALBとFargateを作成したので、工程を数回に分けて記載します。
Terraformコマンド・AWS CLIが使用可能である、export AWS_DEFAULT_PROFILEでインフラを構築したいプロファイルになっている事が前提です。

環境

Terraform v1.1.6

完成図

AWS_Fargate資料.png

まずVPCを作成したのでメモとして記載します。

今回作成するVPC構成図

Cursor_と_AWS_Fargate資料.png

Terraformディレクトリ構成

.
├── environments
│   └── staging
│   │   ├── backend.tf
│   │   ├── main.tf
│   │   ├── terraform.tfvars
│   │   └── variables.tf
│   └── production
│   │   ├── backend.tf
│   │   ├── main.tf
│   │   ├── terraform.tfvars
│   │   └── variables.tf
|   └── script
|       └── install.sh
|
└──  module_aws
    └── vpc
    |    ├── main.tf
    |    ├── output.tf
    |    └── variables.tf 
    └── ec2
    |    ├── main.tf
    |    ├── output.tf
    |    └── variables.tf 
    └── alb
         ├── main.tf
         └── variables.tf 

作成したコード

/environments/staging/backend.tf
terraform {
  backend "s3" {
    bucket = "S3のバケット名"
    key    = "terraform/XXX.tfstate"
    region = "ap-northeast-1"
    profile = "XXX"
  }
}
/environments/staging/main.tf
provider "aws" {
  region = "ap-northeast-1"
}

# vpcモジュールを呼び出す
module "vpc" {
  source = "../../module_aws/vpc"
  customer_no = var.customer_no
  system = var.system
  env = var.env
  cidr_vpc = var.vpc_cidr
  cidr_public_a = var.cidr_public_a
  cidr_public_c = var.cidr_public_c
  cidr_private_a = var.cidr_private_a
  cidr_private_c = var.cidr_private_c
}
/environments/staging/terraform.tfvars
customer_no = "001" # 顧客No
system       = "WEB" # タグに使用するこの基盤のシステム名称(任意)
env          = "STG" # タグに使用する環境の名称(dev|stg|prd)
vpc_cidr     = "10.0.0.0/16" # VPCのサイダーブロック
cidr_public_a  = "10.0.1.0/24" # サブネットのCIDRブロック
cidr_public_c  = "10.0.2.0/24" # サブネットのCIDRブロック
cidr_private_a  = "10.0.3.0/24" # サブネットのCIDRブロック
cidr_private_c  = "10.0.4.0/24" # サブネットのCIDRブロック
/environments/staging/variables.tf
# General
variable "customer_no" {}
variable "system" {}
variable "env" {}

# VPC
variable "vpc_cidr" {}
variable "cidr_public_a" {}
variable "cidr_public_c" {}
variable "cidr_private_a" {}
variable "cidr_private_c" {}
/module_aws/vpc/main.tf
data "aws_availability_zones" "available" {
  state = "available"
}

# VPC作成
resource "aws_vpc" "vpc" {
  cidr_block           = var.cidr_vpc
  instance_tenancy     = "default"
  enable_dns_hostnames = true
  tags = {
    Name = "${var.customer_no}-${var.env}-${var.system}"
    "terraform_${var.customer_no}-${var.env}-${var.system}" = "yes"
  }
}

# IG作成
resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.vpc.id
  tags = {
    Name = "${var.customer_no}-${var.env}-${var.system}-igw"
    "terraform_${var.customer_no}-${var.env}-${var.system}" = "yes"
  }
}

# サブネット作成
## public_ap_northeast_1a
resource "aws_subnet" "public_ap_northeast_1a" {
  vpc_id = aws_vpc.vpc.id
  availability_zone = "ap-northeast-1a"
  cidr_block = var.cidr_public_a
  tags = {
    Name = "${var.customer_no}-${var.env}-${var.system}-PUB-A"
    "terraform_${var.customer_no}-${var.env}-${var.system}" = "yes"
  }
}

## public_ap_northeast_1c
resource "aws_subnet" "public_ap_northeast_1c" {
  vpc_id = aws_vpc.vpc.id
  availability_zone = "ap-northeast-1c"
  cidr_block = var.cidr_public_c
  tags = {
    Name = "${var.customer_no}-${var.env}-${var.system}-PUB-C"
    "terraform_${var.customer_no}-${var.env}-${var.system}" = "yes"
  }
}

## private_ap_northeast_1a
resource "aws_subnet" "private_ap_northeast_1a" {
  vpc_id = aws_vpc.vpc.id
  availability_zone = "ap-northeast-1a"
  cidr_block = var.cidr_private_a
  tags = {
    Name = "${var.customer_no}-${var.env}-${var.system}-PRI-A"
    "terraform_${var.customer_no}-${var.env}-${var.system}" = "yes"
  }
}

## private_ap_northeast_1c
resource "aws_subnet" "private_ap_northeast_1c" {
  vpc_id = aws_vpc.vpc.id
  availability_zone = "ap-northeast-1c"
  cidr_block = var.cidr_private_c
  tags = {
    Name = "${var.customer_no}-${var.env}-${var.system}-PRI-C"
    "terraform_${var.customer_no}-${var.env}-${var.system}" = "yes"
  }
}

# ルートテーブル作成
## public
resource "aws_default_route_table" "public_route" {
  default_route_table_id = aws_vpc.vpc.default_route_table_id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }
  tags = {
    Name = "${var.customer_no}-${var.env}-${var.system}-PUB"
    "terraform_${var.customer_no}-${var.env}-${var.system}" = "yes"
  }
}  
/module_aws/vpc/variables.tf
variable "customer_no" {}
variable "system" {}
variable "env" {}
variable "cidr_vpc" {}
variable "cidr_public_a" {}
variable "cidr_public_c" {}
variable "cidr_private_a" {}
variable "cidr_private_c" {}

実行手順

  • ワークスペースを初期化
cd /environments/staging
terraform init
  • 実行計画確認
terraform plan
  • インフラ構築を実行
terraform apply
  • 作成したインフラ削除
terraform destroy

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
What you can do with signing up
0