浏览代码

infra: add `VPC` module to setup networking.

- Create public and private subnets
- Add Internet Gateway and NAT Gateway
- Add Route tables
Aashish Goyal 1 年之前
父节点
当前提交
6e01da69ca
共有 3 个文件被更改,包括 146 次插入0 次删除
  1. 13 0
      modules/vpc/outputs.tf
  2. 92 0
      modules/vpc/terraform.tf
  3. 41 0
      modules/vpc/variables.tf

+ 13 - 0
modules/vpc/outputs.tf

@@ -0,0 +1,13 @@
+## Outputs
+
+output "vpc_id" {
+  value = aws_vpc.infra_vpc.id
+}
+
+output "public_subnet_id" {
+  value = aws_subnet.infra_public_subnet[*].id
+}
+
+output "private_subnet_id" {
+  value = aws_subnet.infra_private_subnet.id
+}

+ 92 - 0
modules/vpc/terraform.tf

@@ -0,0 +1,92 @@
+## VPC
+resource "aws_vpc" "infra_vpc" {
+  cidr_block           = var.cidr_block
+  enable_dns_support   = var.enable_dns_support
+  enable_dns_hostnames = var.enable_dns_hostnames
+
+  tags = var.tags
+}
+
+## Public Subnet for ALB
+resource "aws_subnet" "infra_public_subnet" {
+  count = length(var.public_subnet_az)
+
+  vpc_id            = aws_vpc.infra_vpc.id
+  availability_zone = var.public_subnet_az[count.index]
+  cidr_block        = var.public_subnet_cidr[count.index]
+
+  tags = var.tags
+}
+
+## Private Subnet for EC2
+resource "aws_subnet" "infra_private_subnet" {
+  vpc_id            = aws_vpc.infra_vpc.id
+  availability_zone = var.private_subnet_az
+  cidr_block        = var.private_subnet_cidr
+
+  tags = var.tags
+}
+
+## Internet Gateway
+resource "aws_internet_gateway" "infra_internet_gateway" {
+  vpc_id = aws_vpc.infra_vpc.id
+
+  tags = var.tags
+}
+
+## Route Table for Public Subnet and attach Internet Gateway to it
+resource "aws_route_table" "infra_public_rt" {
+  vpc_id = aws_vpc.infra_vpc.id
+
+  route {
+    cidr_block = "0.0.0.0/0"
+    gateway_id = aws_internet_gateway.infra_internet_gateway.id
+  }
+
+  tags = var.tags
+}
+
+## Route Table Public Subnet Association
+resource "aws_route_table_association" "infra_public_subnet_rt_association" {
+  count = length(var.public_subnet_az)
+
+  subnet_id      = aws_subnet.infra_public_subnet[count.index].id
+  route_table_id = aws_route_table.infra_public_rt.id
+}
+
+## Elastic IP for NAT Gateway
+resource "aws_eip" "infra_nat_gateway_eip" {
+  domain = "vpc"
+
+  tags = var.tags
+}
+
+## NAT Gateway
+resource "aws_nat_gateway" "infra_nat_gateway" {
+  allocation_id = aws_eip.infra_nat_gateway_eip.id
+  subnet_id     = aws_subnet.infra_public_subnet[0].id
+
+  tags = var.tags
+
+  depends_on = [
+    aws_internet_gateway.infra_internet_gateway
+  ]
+}
+
+## Route Table for Private Subnet and attach NAT Gateway to it
+resource "aws_route_table" "infra_private_rt" {
+  vpc_id = aws_vpc.infra_vpc.id
+
+  route {
+    cidr_block     = "0.0.0.0/0"
+    nat_gateway_id = aws_nat_gateway.infra_nat_gateway.id
+  }
+
+  tags = var.tags
+}
+
+## Route Table Private Subnet Association
+resource "aws_route_table_association" "infra_private_subnet_rt_association" {
+  subnet_id      = aws_subnet.infra_private_subnet.id
+  route_table_id = aws_route_table.infra_private_rt.id
+}

+ 41 - 0
modules/vpc/variables.tf

@@ -0,0 +1,41 @@
+## Variables
+
+variable "tags" {
+  type    = map(string)
+  default = {}
+}
+
+variable "cidr_block" {
+  type    = string
+  default = ""
+}
+
+variable "enable_dns_support" {
+  type    = bool
+  default = true
+}
+
+variable "enable_dns_hostnames" {
+  type    = bool
+  default = true
+}
+
+variable "public_subnet_az" {
+  type    = list(string)
+  default = [""]
+}
+
+variable "public_subnet_cidr" {
+  type    = list(string)
+  default = [""]
+}
+
+variable "private_subnet_az" {
+  type    = string
+  default = ""
+}
+
+variable "private_subnet_cidr" {
+  type    = string
+  default = ""
+}