Terraform : Create a VPC, IG, SUBNETS , NAT, ROUTE TABLES
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Main.tf or vpc.tf
resource "aws_vpc" "my-vpc-superkite" {
cidr_block = "${var.cidr_block}"
# default uses the same hardware as other aws customer
instance_tenancy = "${var.instance_tenancy}"
/*
-- enable_dns_hostnames = true
When this setting is enabled, Amazon Route 53 will automatically
create DNS records for the instances in the VPC, allowing them to
be addressed by their hostname. This can be useful for services that
need to be accessed by their hostname instead of their IP address,
or for cases where the IP addresses of the instances may change freq uently. */
enable_dns_hostnames = true
/*
@color red
-- enable_dns_support = true @color
When this setting is enabled, Amazon Route 53 will provide DNS resolution
for the instances in the VPC, allowing them to be addressed by their hostname.
This can be useful for services that need to be accessed by their hostname
instead of their IP address, or for cases where the IP addresses of the
instances may change frequently. */
enable_dns_support = true
tags = {
Name = "my-vpc-superkite"
}
}
# Creating Gateway
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.my-vpc-superkite.id
tags = {
Name = "${var.environment}-igw"
Environment = "${var.environment}"
}
}
resource "aws_subnet" "sreejith_public_subnet" {
vpc_id = aws_vpc.my-vpc-superkite.id
cidr_block = "${var.public_subnet_cidr_block}"
tags = {
Name = "${var.environment}-public_subnet"
Environment = "${var.environment}"
}
}
resource "aws_subnet" "sreejith_private_subnet" {
vpc_id = aws_vpc.my-vpc-superkite.id
cidr_block = "${var.private_subnet_cidr_block}"
tags = {
Name = "${var.environment}-private_subnet"
Environment = "${var.environment}"
}
}
/* Elastic IP for NAT */
resource "aws_eip" "nat_eip" {
vpc = true
}
resource "aws_nat_gateway" "nat" {
allocation_id = aws_eip.nat_eip.id
subnet_id = aws_subnet.sreejith_private_subnet.id
depends_on = [aws_internet_gateway.gw]
tags = {
Name = "superkite-nat-gateway"
}
}
/* Routing table for private subnet */
resource "aws_route_table" "superkite-private" {
vpc_id = aws_vpc.my-vpc-superkite.id
tags = {
Name = "${var.environment}-private-route-table"
Environment = "${var.environment}"
}
}
/* Routing table for public subnet */
resource "aws_route_table" "superkite-public" {
vpc_id = aws_vpc.my-vpc-superkite.id
tags = {
Name = "${var.environment}-public-route-table"
Environment = "${var.environment}"
}
}
resource "aws_route" "public_internet_gateway" {
route_table_id = "${aws_route_table.superkite-private.id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
resource "aws_route" "private_internet_gateway" {
route_table_id = "${aws_route_table.superkite-public.id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
/* Route table associations */
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.sreejith_public_subnet.id
route_table_id = aws_route_table.superkite-public.id
}
/* Route table associations */
resource "aws_route_table_association" "private" {
subnet_id = aws_subnet.sreejith_private_subnet.id
route_table_id = aws_route_table.superkite-private.id
}
/*==== VPC's Default Security Group ======*/
resource "aws_security_group" "superkite_group" {
name_prefix = "superk_"
description = "Security group for web servers"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Environment = "asia-staging"
}
}
variables.tf
variable "cidr_block" {
description = "The CIDR block to use for the VPC"
type = string
default = "10.0.0.0/16"
}
variable "private_subnet_cidr_block" {
description = "The subset CIDR block to use for the VPC"
type = string
default = "10.0.1.0/26"
}
variable "public_subnet_cidr_block" {
description = "The subset CIDR block to use for the VPC"
type = string
default = "10.0.0.0/24"
}
variable "instance_tenancy" {
description = "instance tenancy"
type = string
default = "default"
}
variable "environment" {
description = "instance tenancy"
type = string
default = "asia-staging"
}
provider.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.59.0"
}
}
}
provider "aws" {
# Configuration options
}
Comments
Post a Comment