2
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を作成する

Posted at

EVENT

TerraformでAWS開発
Module単位で以下を開発する

  • VPC
  • サブネット
  • IGW
  • ルートテーブル

SOLUTION

イメージ図

image.png

  • internet_gateway -> aws_routeを使用して、publicからインターネットへ出る
  • route_table -> subnetの通信地図(ルートテーブル)
  • route_table_association -> subnetとroute_tableの紐づけ

フォルダ構成

image.png

VPC

1 . modules/vpc/main.tf

/**-------------
- VPC
- Subnet
- IGW
- Route Table
-------------*/

resource "aws_vpc" "main" {
  cidr_block = var.vpc_cidr
  enable_dns_support = true
  enable_dns_hostnames = true

  tags = {
    name = "${var.project_name}-${var.environment}-vpc"
    environment = var.environment
  }
}

resource "aws_internet_gateway" "main" {
  vpc_id = aws_vpc.main.id

  tags = {
    name = "${var.project_name}-${var.environment}-igw"
    environment = var.environment
  }
}

resource "aws_subnet" "public" {
  count = length(var.public_subnets_cidr)
  vpc_id = aws_vpc.main.id
  cidr_block = var.public_subnets_cidr[count.index]
  availability_zone = var.availability_zones[count.index]
  map_public_ip_on_launch = true

  tags = {
    name = "${var.project_name}-${var.environment}-public-subnet-${count.index + 1}"
    environment = var.environment
  }
}

resource "aws_subnet" "private" {
  count = length(var.private_subnets_cidr)
  vpc_id = aws_vpc.main.id
  cidr_block = var.private_subnets_cidr[count.index]
  availability_zone = var.availability_zones[count.index]

  tags = {
    name = "${var.project_name}-${var.environment}-private-subnet-${count.index + 1}"
    environment = var.environment
  }
}

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.main.id

  tags = {
    name = "${var.project_name}-${var.environment}-public-rt"
    environment = var.environment
  }
}

# Public Route (IGWへのデフォルトルート)
resource "aws_route" "public_internet_gateway" {
  route_table_id = aws_route_table.public.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id = aws_internet_gateway.main.id
}

# Public SubnetとPublic Route Tableへの紐づけ
resource "aws_route_table_association" "public" {
  count = length(aws_subnet.public)
  subnet_id = aws_subnet.public[count.index].id
  route_table_id = aws_route_table.public.id
}

resource "aws_route_table" "private" {
  # AZ事に1つずつ(削除予定?)
  count = length(var.private_subnets_cidr)
  vpc_id = aws_vpc.main.id

  tags = {
    name = "${var.project_name}-${var.environment}-private-rt-${count.index + 1}"
    environment = var.environment
  }
}

resource "aws_route_table_association" "private" {
  count          = length(aws_subnet.private)
  subnet_id      = aws_subnet.private[count.index].id
  route_table_id = aws_route_table.private[count.index].id
}

2 . modules/vpc/variables.tf

variable "project_name" {
  description = "Project Name tag,"
  type = string
}

variable "environment" {
  description = "Deployment Environment."
  type = string
}

variable "vpc_cidr" {
 description = "CIDR Block for the VPC." 
 type = string
}

variable "public_subnets_cidr" {
  description = "List of CIDR blocks for public subnets."
  type        = list(string)
}

variable "private_subnets_cidr" {
  description = "List of CIDR blocks for private subnets."
  type        = list(string)
}

variable "availability_zones" {
  description = "List of Availability Zones to use."
  type        = list(string)
}

3 . modules/vpc/outputs.tf

output "vpc_id" {
  description = "The ID of the VPC."
  value       = aws_vpc.main.id
}

output "public_subnet_ids" {
  description = "List of IDs of the public subnets."
  value       = [for s in aws_subnet.public : s.id]
}

output "private_subnet_ids" {
  description = "List of IDs of the private subnets."
  value       = [for s in aws_subnet.private : s.id]
}

output "public_route_table_id" {
  description = "The ID of the public route table."
  value       = aws_route_table.public.id
}

output "private_route_table_ids" {
  description = "List of IDs of the private route tables."
  value       = [for rt in aws_route_table.private : rt.id]
}

4 . dev環境にVPCを定義する

  • environments/dev/main.tf
output "vpc_id" {
  description = "The ID of the VPC."
  value       = aws_vpc.main.id
}

output "public_subnet_ids" {
  description = "List of IDs of the public subnets."
  value       = [for s in aws_subnet.public : s.id]
}

output "private_subnet_ids" {
  description = "List of IDs of the private subnets."
  value       = [for s in aws_subnet.private : s.id]
}

output "public_route_table_id" {
  description = "The ID of the public route table."
  value       = aws_route_table.public.id
}

output "private_route_table_ids" {
  description = "List of IDs of the private route tables."
  value       = [for rt in aws_route_table.private : rt.id]
}

実行コンソール確認

  1. terraform apply
    image.png

  2. コンソールで確認

  • VPC
    image.png

  • IGW
    image.png

  • SUBNET
    image.png

  • ルートテーブル
    image.png

リソース削除

image.png
画面移動しなくてもTerraformコマンド1回で全て反映できるの助かります。

2
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
2
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?