chrisluiz16

Untitled

May 8th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.51 KB | None | 0 0
  1. # Fetaching the details of the availablity zones
  2.  
  3. data "aws_availability_zones" "az" {
  4.   state = "available"
  5. }
  6.  
  7. # creating VPC
  8.  
  9. resource "aws_vpc" "main" {
  10.   cidr_block           = var.vpc_cidr
  11.   instance_tenancy     = "default"
  12.   enable_dns_support   = true
  13.   enable_dns_hostnames = true
  14.   tags = {
  15.     Name = "${var.project_name}-${var.project_env}"
  16.   }
  17. }
  18.  
  19. #Creating IGW
  20.  
  21. resource "aws_internet_gateway" "igw" {
  22.   vpc_id = aws_vpc.main.id
  23.   tags = {
  24.     Name = "${var.project_name}-${var.project_env}"
  25.   }
  26. }
  27. # Creating subnets
  28.  
  29. resource "aws_subnet" "public" {
  30.   count                   = 3
  31.   vpc_id                  = aws_vpc.main.id
  32.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, count.index)
  33.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  34.   map_public_ip_on_launch = true
  35.   tags = {
  36.     "Name" = "${var.project_name}-${var.project_env}-public-${count.index + 1}"
  37.  
  38.   }
  39. }
  40.  
  41. resource "aws_subnet" "private" {
  42.   count                   = 3
  43.   vpc_id                  = aws_vpc.main.id
  44.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, count.index + 3)
  45.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  46.   map_public_ip_on_launch = false
  47.   tags = {
  48.     "Name" = "${var.project_name}-${var.project_env}-private-${count.index + 1}"
  49.  
  50.   }
  51. }
  52.  
  53. # creating elastic IP for NAT G/W
  54.  
  55. resource "aws_eip" "ip" {
  56.   vpc = true
  57.   tags = {
  58.     "Name" = "${var.project_name}-${var.project_env}-"
  59.   }
  60. }
  61.  
  62.  
  63. # creating NAT gateway
  64.  
  65. resource "aws_nat_gateway" "nat" {
  66.   allocation_id = aws_eip.ip.id
  67.   subnet_id     = aws_subnet.public[0].id
  68.   tags = {
  69.     "Name" = "${var.project_name}-${var.project_env}"
  70.   }
  71.  
  72.   depends_on = [aws_internet_gateway.igw]
  73.  
  74. }
  75.  
  76. # Creating Route table
  77.  
  78. resource "aws_route_table" "public" {
  79.   vpc_id = aws_vpc.main.id
  80.  
  81.   route {
  82.     cidr_block = "0.0.0.0/0"
  83.     gateway_id = aws_internet_gateway.igw.id
  84.   }
  85.   tags = {
  86.     "Name" = "${var.project_name}-${var.project_env}-public"
  87.   }
  88. }
  89. resource "aws_route_table" "private" {
  90.   vpc_id = aws_vpc.main.id
  91.  
  92.   route {
  93.     cidr_block     = "0.0.0.0/0"
  94.     nat_gateway_id = aws_nat_gateway.nat.id
  95.   }
  96.   tags = {
  97.     "Name" = "${var.project_name}-${var.project_env}-private"
  98.   }
  99. }
  100.  
  101.  
  102. #Association with the route table
  103.  
  104. resource "aws_route_table_association" "public" {
  105.   count          = 3
  106.   subnet_id      = aws_subnet.public["${count.index}"].id
  107.   route_table_id = aws_route_table.public.id
  108. }
  109.  
  110. resource "aws_route_table_association" "private" {
  111.   count          = 3
  112.   subnet_id      = aws_subnet.private["${count.index}"].id
  113.   route_table_id = aws_route_table.private.id
  114. }
  115.  
  116. # Creating Security group
  117.  
  118. resource "aws_security_group" "bastion" {
  119.   name_prefix = "${var.project_name}-${var.project_env}-bastion-"
  120.   description = "Allow ssh from my IP only"
  121.   vpc_id      = aws_vpc.main.id
  122.  
  123.   ingress {
  124.     description      = "22 from my IP"
  125.     from_port        = 22
  126.     to_port          = 22
  127.     protocol         = "tcp"
  128.     cidr_blocks      = ["0.0.0.0/0"]
  129.     ipv6_cidr_blocks = ["::/0"]
  130.   }
  131.  
  132.   egress {
  133.     from_port        = 0
  134.     to_port          = 0
  135.     protocol         = "-1"
  136.     cidr_blocks      = ["0.0.0.0/0"]
  137.     ipv6_cidr_blocks = ["::/0"]
  138.   }
  139.  
  140.   tags = {
  141.     Name = "${var.project_name}-${var.project_env}-bastion"
  142.   }
  143. }
  144.  
  145. resource "aws_security_group" "frontend" {
  146.   name_prefix = "${var.project_name}-${var.project_env}-frontend-"
  147.   description = "Allow http,https and ssh"
  148.   vpc_id      = aws_vpc.main.id
  149.   ingress {
  150.     description      = "HTTP port"
  151.     from_port        = 80
  152.     to_port          = 80
  153.     protocol         = "tcp"
  154.     cidr_blocks      = ["0.0.0.0/0"]
  155.     ipv6_cidr_blocks = ["::/0"]
  156.   }
  157.   ingress {
  158.     description      = "HTTPS port"
  159.     from_port        = 443
  160.     to_port          = 443
  161.     protocol         = "tcp"
  162.     cidr_blocks      = ["0.0.0.0/0"]
  163.     ipv6_cidr_blocks = ["::/0"]
  164.   }
  165.  
  166.   ingress {
  167.     description      = "22 from my IP"
  168.     from_port        = 22
  169.     to_port          = 22
  170.     protocol         = "tcp"
  171.     security_groups  = [aws_security_group.bastion.id]
  172.     ipv6_cidr_blocks = ["::/0"]
  173.   }
  174.  
  175.   egress {
  176.     from_port        = 0
  177.     to_port          = 0
  178.     protocol         = "-1"
  179.     cidr_blocks      = ["0.0.0.0/0"]
  180.     ipv6_cidr_blocks = ["::/0"]
  181.   }
  182.  
  183.   tags = {
  184.     Name = "${var.project_name}-${var.project_env}-frontend"
  185.   }
  186. }
  187.  
  188. resource "aws_security_group" "backend" {
  189.   name_prefix = "${var.project_name}-${var.project_env}-backend-"
  190.   description = "Allow mysql and ssh port only"
  191.   vpc_id      = aws_vpc.main.id
  192.  
  193.   ingress {
  194.     description      = "22 from Bastion"
  195.     from_port        = 22
  196.     to_port          = 22
  197.     protocol         = "tcp"
  198.     security_groups  = [aws_security_group.bastion.id]
  199.     ipv6_cidr_blocks = ["::/0"]
  200.   }
  201.   ingress {
  202.     description      = "MYSQL IP"
  203.     from_port        = 3306
  204.     to_port          = 3306
  205.     protocol         = "tcp"
  206.     security_groups  = [aws_security_group.frontend.id]
  207.     ipv6_cidr_blocks = ["::/0"]
  208.   }
  209.  
  210.   egress {
  211.     from_port        = 0
  212.     to_port          = 0
  213.     protocol         = "-1"
  214.     cidr_blocks      = ["0.0.0.0/0"]
  215.     ipv6_cidr_blocks = ["::/0"]
  216.   }
  217.  
  218.   tags = {
  219.     Name = "${var.project_name}-${var.project_env}-backend"
  220.   }
  221. }
  222.  
  223. # Creating ec2-instance
  224.  
  225. resource "aws_instance" "bastion" {
  226.   ami                    = var.ami
  227.   subnet_id              = aws_subnet.public[0].id
  228.   instance_type          = var.instance_type
  229.   key_name               = "mumbai-new"
  230.   vpc_security_group_ids = [aws_security_group.bastion.id]
  231.   user_data              = file("bastion.sh")
  232.   tags = {
  233.     "Name" = "${var.project_name}-${var.project_env}-bastion"
  234.   }
  235.  
  236. }
  237.  
  238. resource "aws_instance" "frontend" {
  239.   ami                    = var.ami
  240.   subnet_id              = aws_subnet.public[0].id
  241.   instance_type          = var.instance_type
  242.   key_name               = "mumbai-new"
  243.   vpc_security_group_ids = [aws_security_group.frontend.id]
  244.   user_data              = file("frontend.sh")
  245.   tags = {
  246.     "Name" = "${var.project_name}-${var.project_env}-frontend"
  247.   }
  248. }
  249.  
  250. resource "aws_instance" "backend" {
  251.   ami                    = var.ami
  252.   subnet_id              = aws_subnet.private[0].id
  253.   instance_type          = var.instance_type
  254.   key_name               = "mumbai-new"
  255.   vpc_security_group_ids = [aws_security_group.backend.id]
  256.   user_data              = file("backend.sh")
  257.   tags = {
  258.     "Name" = "${var.project_name}-${var.project_env}-backend"
  259.   }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment