chrisluiz16

Untitled

May 5th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.00 KB | Source Code | 0 0
  1. # creating VPC
  2.  
  3. resource "aws_vpc" "main" {
  4.   cidr_block           = "192.168.0.0/16"
  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              = var.public1
  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              = var.public2
  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              = var.public3
  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              = var.private1
  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              = var.private2
  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              = var.private3
  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.  
  88.  
  89.  
  90.  
  91. # creating elastic IP
  92.  
  93. resource "aws_eip" "ip" {
  94.   vpc = true
  95.   tags = {
  96.     "Name" = "${var.project_name}-${var.project_env}"
  97.   }
  98. }
  99.  
  100. # creating NAT gateway
  101.  
  102. resource "aws_nat_gateway" "nat" {
  103.   allocation_id = aws_eip.ip.id
  104.   subnet_id     = aws_subnet.public1.id
  105.   tags = {
  106.     "Name" = "${var.project_name}-${var.project_env}"
  107.   }
  108.  
  109.   depends_on = [aws_internet_gateway.igw]
  110.  
  111. }
  112.  
  113.  
  114.  
  115. # Creating Route table
  116.  
  117. resource "aws_route_table" "public" {
  118.   vpc_id = aws_vpc.main.id
  119.  
  120.   route {
  121.     cidr_block = "0.0.0.0/0"
  122.     gateway_id = aws_internet_gateway.igw.id
  123.   }
  124.   tags = {
  125.     "Name" = "${var.project_name}-${var.project_env}-public"
  126.   }
  127. }
  128. resource "aws_route_table" "private" {
  129.   vpc_id = aws_vpc.main.id
  130.  
  131.   route {
  132.     cidr_block     = "0.0.0.0/0"
  133.     nat_gateway_id = aws_nat_gateway.nat.id
  134.   }
  135.   tags = {
  136.     "Name" = "${var.project_name}-${var.project_env}-private"
  137.   }
  138. }
  139.  
  140.  
  141. #Association with the route table
  142.  
  143. resource "aws_route_table_association" "public1" {
  144.   subnet_id      = aws_subnet.public1.id
  145.   route_table_id = aws_route_table.public.id
  146. }
  147.  
  148. resource "aws_route_table_association" "public2" {
  149.   subnet_id      = aws_subnet.public2.id
  150.   route_table_id = aws_route_table.public.id
  151. }
  152.  
  153. resource "aws_route_table_association" "public3" {
  154.   subnet_id      = aws_subnet.public3.id
  155.   route_table_id = aws_route_table.public.id
  156. }
  157. resource "aws_route_table_association" "private1" {
  158.   subnet_id      = aws_subnet.private1.id
  159.   route_table_id = aws_route_table.private.id
  160. }
  161. resource "aws_route_table_association" "private2" {
  162.   subnet_id      = aws_subnet.private2.id
  163.   route_table_id = aws_route_table.private.id
  164. }
  165. resource "aws_route_table_association" "private3" {
  166.   subnet_id      = aws_subnet.private3.id
  167.   route_table_id = aws_route_table.private.id
  168. }
  169. # Creating Security group
  170.  
  171. resource "aws_security_group" "bastion" {
  172.   name_prefix = "${var.project_name}-${var.project_env}-bastion-"
  173.   description = "Allow ssh from my IP only"
  174.   vpc_id      = aws_vpc.main.id
  175.  
  176.   ingress {
  177.     description      = "22 from my IP"
  178.     from_port        = 22
  179.     to_port          = 22
  180.     protocol         = "tcp"
  181.     cidr_blocks      = ["103.104.44.51/32"]
  182.     ipv6_cidr_blocks = ["::/0"]
  183.   }
  184.  
  185.   egress {
  186.     from_port        = 0
  187.     to_port          = 0
  188.     protocol         = "-1"
  189.     cidr_blocks      = ["0.0.0.0/0"]
  190.     ipv6_cidr_blocks = ["::/0"]
  191.   }
  192.  
  193.   tags = {
  194.     Name = "${var.project_name}-${var.project_env}-bastion"
  195.   }
  196. }
  197.  
  198. resource "aws_security_group" "frontend" {
  199.   name_prefix = "${var.project_name}-${var.project_env}-frontend-"
  200.   description = "Allow http,https and ssh"
  201.   vpc_id      = aws_vpc.main.id
  202.   ingress {
  203.     description      = "HTTP port"
  204.     from_port        = 80
  205.     to_port          = 80
  206.     protocol         = "tcp"
  207.     cidr_blocks      = ["0.0.0.0/0"]
  208.     ipv6_cidr_blocks = ["::/0"]
  209.   }
  210.   ingress {
  211.     description      = "HTTPS port"
  212.     from_port        = 443
  213.     to_port          = 443
  214.     protocol         = "tcp"
  215.     cidr_blocks      = ["0.0.0.0/0"]
  216.     ipv6_cidr_blocks = ["::/0"]
  217.   }
  218.  
  219.   ingress {
  220.     description      = "22 from my IP"
  221.     from_port        = 22
  222.     to_port          = 22
  223.     protocol         = "tcp"
  224.     security_groups  = [aws_security_group.bastion.id]
  225.     ipv6_cidr_blocks = ["::/0"]
  226.   }
  227.  
  228.   egress {
  229.     from_port        = 0
  230.     to_port          = 0
  231.     protocol         = "-1"
  232.     cidr_blocks      = ["0.0.0.0/0"]
  233.     ipv6_cidr_blocks = ["::/0"]
  234.   }
  235.  
  236.   tags = {
  237.     Name = "${var.project_name}-${var.project_env}-frontend"
  238.   }
  239. }
  240.  
  241. resource "aws_security_group" "backend" {
  242.   name_prefix = "${var.project_name}-${var.project_env}-backend-"
  243.   description = "Allow mysql and ssh port only"
  244.   vpc_id      = aws_vpc.main.id
  245.  
  246.   ingress {
  247.     description      = "22 from Bastion"
  248.     from_port        = 22
  249.     to_port          = 22
  250.     protocol         = "tcp"
  251.     security_groups  = [aws_security_group.bastion.id]
  252.     ipv6_cidr_blocks = ["::/0"]
  253.   }
  254.   ingress {
  255.     description      = "MYSQL IP"
  256.     from_port        = 3306
  257.     to_port          = 3306
  258.     protocol         = "tcp"
  259.     security_groups  = [aws_security_group.frontend.id]
  260.     ipv6_cidr_blocks = ["::/0"]
  261.   }
  262.  
  263.   egress {
  264.     from_port        = 0
  265.     to_port          = 0
  266.     protocol         = "-1"
  267.     cidr_blocks      = ["0.0.0.0/0"]
  268.     ipv6_cidr_blocks = ["::/0"]
  269.   }
  270.  
  271.   tags = {
  272.     Name = "${var.project_name}-${var.project_env}-backend"
  273.   }
  274. }
  275.  
  276.  
  277. # Creating a new key pair
  278.  
  279. resource "aws_key_pair" "zomato" {
  280.   key_name   = var.project_name
  281.   public_key = file("swiggy.pub")
  282. }
  283.  
  284. # Creating ec2-instance
  285.  
  286. resource "aws_instance" "bastion" {
  287.   ami                    = var.ami
  288.   subnet_id              = aws_subnet.public1.id
  289.   instance_type          = var.instance_type
  290.   key_name               = aws_key_pair.zomato.id
  291.   vpc_security_group_ids = [aws_security_group.bastion.id]
  292.   user_data              = file("bastion.sh")
  293.   tags = {
  294.     "Name" = "${var.project_name}-${var.project_env}-bastion"
  295.   }
  296.  
  297. }
  298.  
  299. resource "aws_instance" "frontend" {
  300.   ami                    = var.ami
  301.   subnet_id              = aws_subnet.public2.id
  302.   instance_type          = var.instance_type
  303.   key_name               = aws_key_pair.zomato.id
  304.   vpc_security_group_ids = [aws_security_group.frontend.id]
  305.   user_data              = file("frontend.sh")
  306.   tags = {
  307.     "Name" = "${var.project_name}-${var.project_env}-frontend"
  308.   }
  309. }
  310.  
  311. resource "aws_instance" "backend" {
  312.   ami                    = var.ami
  313.   subnet_id              = aws_subnet.private1.id
  314.   instance_type          = var.instance_type
  315.   key_name               = aws_key_pair.zomato.id
  316.   vpc_security_group_ids = [aws_security_group.backend.id]
  317.   user_data              = file("backend.sh")
  318.   tags = {
  319.     "Name" = "${var.project_name}-${var.project_env}-backend"
  320.    }
  321.  
  322.  lifecycle {
  323.     create_before_destroy = true
  324.   }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment