chrisluiz16

Untitled

May 6th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.06 KB | Source Code | 0 0
  1. # creating VPC
  2.  
  3. resource "aws_vpc" "main" {
  4.   cidr_block           = var.vpc_cidr
  5.   instance_tenancy     = "default"
  6.   enable_dns_support   = true
  7.   enable_dns_hostnames = true
  8.   tags = {
  9.     Name = "${var.project_name}-${var.project_env}"
  10.   }
  11. }
  12.  
  13. #Creating IGW
  14.  
  15. resource "aws_internet_gateway" "igw" {
  16.   vpc_id = aws_vpc.main.id
  17.   tags = {
  18.     Name = "${var.project_name}-${var.project_env}"
  19.   }
  20. }
  21. # Creating subnets
  22.  
  23. resource "aws_subnet" "public1" {
  24.   vpc_id                  = aws_vpc.main.id
  25.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, 0)
  26.   availability_zone       = "${var.region}a"
  27.   map_public_ip_on_launch = true
  28.   tags = {
  29.     "Name" = "${var.project_name}-${var.project_env}-public1"
  30.  
  31.   }
  32.  
  33. }
  34.  
  35. resource "aws_subnet" "public2" {
  36.   vpc_id                  = aws_vpc.main.id
  37.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, 1)
  38.   availability_zone       = "${var.region}b"
  39.   map_public_ip_on_launch = true
  40.   tags = {
  41.     "Name" = "${var.project_name}-${var.project_env}-public2"
  42.  
  43.   }
  44.  
  45. }
  46. resource "aws_subnet" "public3" {
  47.   vpc_id                  = aws_vpc.main.id
  48.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, 2)
  49.   availability_zone       = "${var.region}c"
  50.   map_public_ip_on_launch = true
  51.   tags = {
  52.     "Name" = "${var.project_name}-${var.project_env}-public3"
  53.  
  54.   }
  55. }
  56. resource "aws_subnet" "private1" {
  57.   vpc_id                  = aws_vpc.main.id
  58.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, 3)
  59.   availability_zone       = "${var.region}a"
  60.   map_public_ip_on_launch = false
  61.   tags = {
  62.     "Name" = "${var.project_name}-${var.project_env}-private1"
  63.  
  64.   }
  65. }
  66. resource "aws_subnet" "private2" {
  67.   vpc_id                  = aws_vpc.main.id
  68.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, 4)
  69.   availability_zone       = "${var.region}b"
  70.   map_public_ip_on_launch = false
  71.   tags = {
  72.     "Name" = "${var.project_name}-${var.project_env}-private2"
  73.  
  74.   }
  75. }
  76. resource "aws_subnet" "private3" {
  77.   vpc_id                  = aws_vpc.main.id
  78.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, 5)
  79.   availability_zone       = "${var.region}c"
  80.   map_public_ip_on_launch = false
  81.   tags = {
  82.     "Name" = "${var.project_name}-${var.project_env}-private3"
  83.  
  84.   }
  85. }
  86.  
  87. #Creating a data source to find the hosted zone
  88.  
  89. data "aws_route53_zone" "chris" {
  90.   name         = var.public_domain
  91.   private_zone = false
  92. }
  93.  
  94. # creating elastic IP for frontend instance
  95.  
  96. resource "aws_eip" "pub_ip" {
  97.   vpc      = true
  98.   instance = aws_instance.frontend.id
  99.   tags = {
  100.     "Name" = "${var.project_name}-${var.project_env}-frontend"
  101.   }
  102. }
  103.  
  104. # creating elastic IP for NAT G/W
  105.  
  106. resource "aws_eip" "ip" {
  107.   vpc = true
  108.   tags = {
  109.     "Name" = "${var.project_name}-${var.project_env}-"
  110.   }
  111. }
  112.  
  113.  
  114. # Creating chrisich.fun A record
  115.  
  116. resource "aws_route53_record" "a" {
  117.   zone_id = data.aws_route53_zone.chris.zone_id
  118.   name    = "${var.frontend_hostname}.${var.public_domain}"
  119.   type    = "A"
  120.   ttl     = 5
  121.   records = [aws_eip.pub_ip.public_ip]
  122. }
  123.  
  124. # Creating a Private hosted zone
  125.  
  126. resource "aws_route53_zone" "private" {
  127.   name = var.private_domain
  128.  
  129.   vpc {
  130.     vpc_id = aws_vpc.main.id
  131.   }
  132.   tags = {
  133.     "Name" = "${var.project_name}-${var.project_env}"
  134.   }
  135. }
  136.  
  137. # Creating Private hosted Zone records
  138.  
  139. resource "aws_route53_record" "frontend" {
  140.   zone_id = aws_route53_zone.private.zone_id
  141.   name    = "frontend.${var.private_domain}"
  142.   type    = "A"
  143.   ttl     = 5
  144.   records = [aws_instance.frontend.private_ip]
  145. }
  146.  
  147. resource "aws_route53_record" "backend" {
  148.   zone_id = aws_route53_zone.private.zone_id
  149.   name    = "backend.${var.private_domain}"
  150.   type    = "A"
  151.   ttl     = 5
  152.   records = [aws_instance.backend.private_ip]
  153. }
  154.  
  155. resource "aws_route53_record" "bastion" {
  156.   zone_id = aws_route53_zone.private.zone_id
  157.   name    = "bastion.${var.private_domain}"
  158.   type    = "A"
  159.   ttl     = 5
  160.   records = [aws_instance.backend.private_ip]
  161. }
  162.  
  163.  
  164.  
  165. # creating NAT gateway
  166.  
  167. resource "aws_nat_gateway" "nat" {
  168.   allocation_id = aws_eip.ip.id
  169.   subnet_id     = aws_subnet.public1.id
  170.   tags = {
  171.     "Name" = "${var.project_name}-${var.project_env}"
  172.   }
  173.  
  174.   depends_on = [aws_internet_gateway.igw]
  175.  
  176. }
  177.  
  178. # Creating Route table
  179.  
  180. resource "aws_route_table" "public" {
  181.   vpc_id = aws_vpc.main.id
  182.  
  183.   route {
  184.     cidr_block = "0.0.0.0/0"
  185.     gateway_id = aws_internet_gateway.igw.id
  186.   }
  187.   tags = {
  188.     "Name" = "${var.project_name}-${var.project_env}-public"
  189.   }
  190. }
  191. resource "aws_route_table" "private" {
  192.   vpc_id = aws_vpc.main.id
  193.  
  194.   route {
  195.     cidr_block     = "0.0.0.0/0"
  196.     nat_gateway_id = aws_nat_gateway.nat.id
  197.   }
  198.   tags = {
  199.     "Name" = "${var.project_name}-${var.project_env}-private"
  200.   }
  201. }
  202.  
  203.  
  204. #Association with the route table
  205.  
  206. resource "aws_route_table_association" "public1" {
  207.   subnet_id      = aws_subnet.public1.id
  208.   route_table_id = aws_route_table.public.id
  209. }
  210.  
  211. resource "aws_route_table_association" "public2" {
  212.   subnet_id      = aws_subnet.public2.id
  213.   route_table_id = aws_route_table.public.id
  214. }
  215.  
  216. resource "aws_route_table_association" "public3" {
  217.   subnet_id      = aws_subnet.public3.id
  218.   route_table_id = aws_route_table.public.id
  219. }
  220. resource "aws_route_table_association" "private1" {
  221.   subnet_id      = aws_subnet.private1.id
  222.   route_table_id = aws_route_table.private.id
  223. }
  224. resource "aws_route_table_association" "private2" {
  225.   subnet_id      = aws_subnet.private2.id
  226.   route_table_id = aws_route_table.private.id
  227. }
  228. resource "aws_route_table_association" "private3" {
  229.   subnet_id      = aws_subnet.private3.id
  230.   route_table_id = aws_route_table.private.id
  231.  
  232. }
  233.  
  234. # Creating Security group
  235.  
  236. resource "aws_security_group" "bastion" {
  237.   name_prefix = "${var.project_name}-${var.project_env}-bastion-"
  238.   description = "Allow ssh from my IP only"
  239.   vpc_id      = aws_vpc.main.id
  240.  
  241.   ingress {
  242.     description      = "22 from my IP"
  243.     from_port        = 22
  244.     to_port          = 22
  245.     protocol         = "tcp"
  246.     cidr_blocks      = ["103.104.44.48/32"]
  247.     ipv6_cidr_blocks = ["::/0"]
  248.   }
  249.  
  250.   egress {
  251.     from_port        = 0
  252.     to_port          = 0
  253.     protocol         = "-1"
  254.     cidr_blocks      = ["0.0.0.0/0"]
  255.     ipv6_cidr_blocks = ["::/0"]
  256.   }
  257.  
  258.   tags = {
  259.     Name = "${var.project_name}-${var.project_env}-bastion"
  260.   }
  261.  
  262.   lifecycle {
  263.     create_before_destroy = true
  264.   }
  265. }
  266.  
  267. resource "aws_security_group" "frontend" {
  268.   name_prefix = "${var.project_name}-${var.project_env}-frontend-"
  269.   description = "Allow http,https and ssh"
  270.   vpc_id      = aws_vpc.main.id
  271.   ingress {
  272.     description      = "HTTP port"
  273.     from_port        = 80
  274.     to_port          = 80
  275.     protocol         = "tcp"
  276.     cidr_blocks      = ["0.0.0.0/0"]
  277.     ipv6_cidr_blocks = ["::/0"]
  278.   }
  279.   ingress {
  280.     description      = "HTTPS port"
  281.     from_port        = 443
  282.     to_port          = 443
  283.     protocol         = "tcp"
  284.     cidr_blocks      = ["0.0.0.0/0"]
  285.     ipv6_cidr_blocks = ["::/0"]
  286.   }
  287.  
  288.   ingress {
  289.     description      = "22 from my IP"
  290.     from_port        = 22
  291.     to_port          = 22
  292.     protocol         = "tcp"
  293.     security_groups  = [aws_security_group.bastion.id]
  294.     ipv6_cidr_blocks = ["::/0"]
  295.   }
  296.  
  297.   egress {
  298.     from_port        = 0
  299.     to_port          = 0
  300.     protocol         = "-1"
  301.     cidr_blocks      = ["0.0.0.0/0"]
  302.     ipv6_cidr_blocks = ["::/0"]
  303.   }
  304.  
  305.   tags = {
  306.     Name = "${var.project_name}-${var.project_env}-frontend"
  307.   }
  308.  
  309.   lifecycle {
  310.     create_before_destroy = true
  311.   }
  312. }
  313.  
  314. resource "aws_security_group" "backend" {
  315.   name_prefix = "${var.project_name}-${var.project_env}-backend-"
  316.   description = "Allow mysql and ssh port only"
  317.   vpc_id      = aws_vpc.main.id
  318.  
  319.   ingress {
  320.     description      = "22 from Bastion"
  321.     from_port        = 22
  322.     to_port          = 22
  323.     protocol         = "tcp"
  324.     security_groups  = [aws_security_group.bastion.id]
  325.     ipv6_cidr_blocks = ["::/0"]
  326.   }
  327.   ingress {
  328.     description      = "MYSQL IP"
  329.     from_port        = 3306
  330.     to_port          = 3306
  331.     protocol         = "tcp"
  332.     security_groups  = [aws_security_group.frontend.id]
  333.     ipv6_cidr_blocks = ["::/0"]
  334.   }
  335.  
  336.   egress {
  337.     from_port        = 0
  338.     to_port          = 0
  339.     protocol         = "-1"
  340.     cidr_blocks      = ["0.0.0.0/0"]
  341.     ipv6_cidr_blocks = ["::/0"]
  342.   }
  343.  
  344.   tags = {
  345.     Name = "${var.project_name}-${var.project_env}-backend"
  346.   }
  347.  
  348.   lifecycle {
  349.     create_before_destroy = true
  350.   }
  351. }
  352.  
  353.  
  354. # Creating a new key pair with TLS provider
  355.  
  356. resource "tls_private_key" "rsa" {
  357.   algorithm = "RSA"
  358.   rsa_bits  = 4096
  359. }
  360.  
  361. resource "aws_key_pair" "zomato" {
  362.   key_name   = var.project_name
  363.   public_key = tls_private_key.rsa.public_key_openssh
  364. }
  365.  
  366. #Downloading in local machine
  367.  
  368. resource "local_file" "zomato" {
  369.   content  = tls_private_key.rsa.private_key_openssh
  370.   filename = "${var.project_name}.pem"
  371.   file_permission = "400"
  372. }
  373.  
  374. # Creating ec2-instance
  375.  
  376. resource "aws_instance" "bastion" {
  377.   ami                    = var.ami
  378.   subnet_id              = aws_subnet.public1.id
  379.   instance_type          = var.instance_type
  380.   key_name               = aws_key_pair.zomato.id
  381.   vpc_security_group_ids = [aws_security_group.bastion.id]
  382.   user_data              = file("bastion.sh")
  383.   tags = {
  384.     "Name" = "${var.project_name}-${var.project_env}-bastion"
  385.   }
  386.  
  387. }
  388.  
  389. resource "aws_instance" "frontend" {
  390.   ami                    = var.ami
  391.   subnet_id              = aws_subnet.public2.id
  392.   instance_type          = var.instance_type
  393.   key_name               = aws_key_pair.zomato.id
  394.   vpc_security_group_ids = [aws_security_group.frontend.id]
  395.   user_data              = file("frontend.sh")
  396.   tags = {
  397.     "Name" = "${var.project_name}-${var.project_env}-frontend"
  398.   }
  399. }
  400.  
  401. resource "aws_instance" "backend" {
  402.   ami                    = var.ami
  403.   subnet_id              = aws_subnet.private1.id
  404.   instance_type          = var.instance_type
  405.   key_name               = aws_key_pair.zomato.id
  406.   vpc_security_group_ids = [aws_security_group.backend.id]
  407.   user_data              = file("backend.sh")
  408.   tags = {
  409.     "Name" = "${var.project_name}-${var.project_env}-backend"
  410.   }
  411.  
  412. }
Advertisement
Add Comment
Please, Sign In to add comment