chrisluiz16

Task

May 9th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.07 KB | None | 0 0
  1. #mumbai provider
  2.  
  3. provider "aws" {
  4.   region     = "ap-south-1"
  5.   access_key = "AKIA5YKGNPEXMXI3AEUM"
  6.   secret_key = "kMNKrzW97p9gbqU180ruCM0PykHAmzjtXLo7wkro"
  7.   default_tags {
  8.     tags = {
  9.  
  10.       "Env"     = var.project_env,
  11.       "Project" = var.project_name
  12.     }
  13.   }
  14. alias = "mumbai"
  15. }
  16.  
  17. #ohio provider
  18.  
  19. provider "aws" {
  20.   region     = "us-east-2"
  21.   access_key = "AKIA5YKGNPEXMXI3AEUM"
  22.   secret_key = "kMNKrzW97p9gbqU180ruCM0PykHAmzjtXLo7wkro"
  23.   alias = "ohio"
  24.   default_tags {
  25.     tags = {
  26.  
  27.       "Env"     = var.project_env,
  28.       "Project" = var.project_name
  29.     }
  30.   }
  31. }
  32.  
  33.  
  34.  
  35.  
  36. # Should you choose to have a nat gateway to allow internet connectivity on private subnet
  37.  
  38. variable "nat" {
  39.   type    = bool
  40.   default = false
  41.  
  42. }
  43.  
  44.  
  45. # Fetaching the details of the availablity zones
  46.  
  47. data "aws_availability_zones" "az" {
  48.   state = "available"
  49. }
  50.  
  51. # creating VPC
  52.  
  53. resource "aws_vpc" "main" {
  54.   cidr_block           = var.vpc_cidr
  55.   instance_tenancy     = "default"
  56.   enable_dns_support   = true
  57.   enable_dns_hostnames = true
  58.   tags = {
  59.     Name = "${var.project_name}-${var.project_env}"
  60.   }
  61. }
  62.  
  63. #Creating IGW
  64.  
  65. resource "aws_internet_gateway" "igw" {
  66.   vpc_id = aws_vpc.main.id
  67.   tags = {
  68.     Name = "${var.project_name}-${var.project_env}"
  69.   }
  70. }
  71. # Creating subnets
  72.  
  73. resource "aws_subnet" "public" {
  74.   count                   = 3
  75.   vpc_id                  = aws_vpc.main.id
  76.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, count.index)
  77.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  78.   map_public_ip_on_launch = true
  79.   tags = {
  80.     "Name" = "${var.project_name}-${var.project_env}-public-${count.index + 1}"
  81.  
  82.   }
  83. }
  84.  
  85. resource "aws_subnet" "private" {
  86.   count                   = 3
  87.   vpc_id                  = aws_vpc.main.id
  88.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, count.index + 3)
  89.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  90.   map_public_ip_on_launch = false
  91.   tags = {
  92.     "Name" = "${var.project_name}-${var.project_env}-private-${count.index + 1}"
  93.  
  94.   }
  95. }
  96.  
  97. # creating elastic IP for NAT G/W if var =true
  98.  
  99. resource "aws_eip" "ip_true" {
  100.  
  101.   count = var.nat == true ? 1 : 0
  102.   vpc   = true
  103.   tags = {
  104.     "Name" = "${var.project_name}-${var.project_env}-Nat"
  105.   }
  106. }
  107.  
  108.  
  109.  
  110. resource "aws_nat_gateway" "nat_true" {
  111.   count         = var.nat == true ? 1 : 0
  112.   allocation_id = aws_eip.ip_true[0].id
  113.   subnet_id     = aws_subnet.public[0].id
  114.   tags = {
  115.     "Name" = "${var.project_name}-${var.project_env}"
  116.   }
  117.  
  118.   depends_on = [aws_internet_gateway.igw]
  119.  
  120. }
  121.  
  122.  
  123. # Creating Route table
  124.  
  125. resource "aws_route_table" "public" {
  126.   vpc_id = aws_vpc.main.id
  127.  
  128.   route {
  129.     cidr_block = "0.0.0.0/0"
  130.     gateway_id = aws_internet_gateway.igw.id
  131.   }
  132.   tags = {
  133.     "Name" = "${var.project_name}-${var.project_env}-public"
  134.   }
  135. }
  136.  
  137. # Creating route table if var = true
  138.  
  139. resource "aws_route_table" "private_true" {
  140.   count  = var.nat == true ? 1 : 0
  141.   vpc_id = aws_vpc.main.id
  142.  
  143.   route {
  144.     cidr_block     = "0.0.0.0/0"
  145.     nat_gateway_id = aws_nat_gateway.nat_true[0].id
  146.   }
  147.   tags = {
  148.     "Name" = "${var.project_name}-${var.project_env}-private"
  149.   }
  150. }
  151.  
  152.  
  153. # Creating route table if var = false
  154.  
  155. resource "aws_route_table" "private_false" {
  156.   count  = var.nat == false ? 1 : 0
  157.   vpc_id = aws_vpc.main.id
  158.   tags = {
  159.     "Name" = "${var.project_name}-${var.project_env}-private"
  160.   }
  161. }
  162.  
  163. #mumbai insatance
  164.  
  165. resource  "aws_instance"  "frontend-mumbai" {
  166.  
  167.    ami = "ami-0c768662cc797cd75"
  168.    instance_type = "t2.micro"
  169.    key_name = "mumbai-new"
  170.    vpc_security_group_ids = [ "sg-09a5a43f8400c81c4" ]
  171.    user_data = file("userdata.sh")
  172.    tags = {
  173.        "Name" = "webserver-mumbai"
  174.    }
  175. provider = aws.mumbai
  176. }
  177.  
  178. #ohio instance
  179.  
  180. resource  "aws_instance"  "frontend-ohio" {
  181.  
  182.    ami = "ami-08333bccc35d71140"
  183.    instance_type = "t2.micro"
  184.    key_name = "ohio-region"
  185.    vpc_security_group_ids = [ "sg-085700648c1f9680c" ]
  186.    user_data = file("userdata.sh")
  187.    tags = {
  188.        "Name" = "webserver-ohio"
  189.    }
  190. provider = aws.ohio
  191. }
Advertisement
Add Comment
Please, Sign In to add comment