chrisluiz16

Untitled

May 11th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.06 KB | None | 0 0
  1. module "vpc" {
  2.   source              = "/home/ec2-user/count/vpc-module"
  3.   project_enviornment = var.project_env
  4.   project_name        = var.myproject_name
  5.   vpc_cidr            = var.myvpc_cidr
  6.   natgw               = true
  7. }
  8.  
  9. # Creating Security group
  10.  
  11. resource "aws_security_group" "bastion" {
  12.   name_prefix = "${var.myproject_name}-${var.project_env}-bastion-"
  13.   description = "Allow ssh from my IP only"
  14.   vpc_id      = module.vpc.vpc_id
  15.  
  16.   ingress {
  17.     description      = "22 from my IP"
  18.     from_port        = 22
  19.     to_port          = 22
  20.     protocol         = "tcp"
  21.     cidr_blocks      = ["0.0.0.0/0"]
  22.     ipv6_cidr_blocks = ["::/0"]
  23.   }
  24.  
  25.   egress {
  26.     from_port        = 0
  27.     to_port          = 0
  28.     protocol         = "-1"
  29.     cidr_blocks      = ["0.0.0.0/0"]
  30.     ipv6_cidr_blocks = ["::/0"]
  31.   }
  32.  
  33.   tags = {
  34.     Name = "${var.myproject_name}-${var.project_env}-bastion"
  35.   }
  36.   lifecycle {
  37.     create_before_destroy = true
  38.   }
  39. }
  40.  
  41. resource "aws_security_group" "frontend" {
  42.   name_prefix = "${var.myproject_name}-${var.project_env}-frontend-"
  43.   description = "Allow http,https and ssh"
  44.   vpc_id      = module.vpc.vpc_id
  45.   ingress {
  46.     description      = "HTTP port"
  47.     from_port        = 80
  48.     to_port          = 80
  49.     protocol         = "tcp"
  50.     cidr_blocks      = ["0.0.0.0/0"]
  51.     ipv6_cidr_blocks = ["::/0"]
  52.   }
  53.   ingress {
  54.     description      = "HTTPS port"
  55.     from_port        = 443
  56.     to_port          = 443
  57.     protocol         = "tcp"
  58.     cidr_blocks      = ["0.0.0.0/0"]
  59.     ipv6_cidr_blocks = ["::/0"]
  60.   }
  61.  
  62.   ingress {
  63.     description      = "22 from my IP"
  64.     from_port        = 22
  65.     to_port          = 22
  66.     protocol         = "tcp"
  67.     security_groups  = [aws_security_group.bastion.id]
  68.     ipv6_cidr_blocks = ["::/0"]
  69.   }
  70.  
  71.   egress {
  72.     from_port        = 0
  73.     to_port          = 0
  74.     protocol         = "-1"
  75.     cidr_blocks      = ["0.0.0.0/0"]
  76.     ipv6_cidr_blocks = ["::/0"]
  77.   }
  78.  
  79.   tags = {
  80.     Name = "${var.myproject_name}-${var.project_env}-frontend"
  81.   }
  82.   lifecycle {
  83.     create_before_destroy = true
  84.   }
  85. }
  86.  
  87. resource "aws_security_group" "backend" {
  88.   name_prefix = "${var.myproject_name}-${var.project_env}-backend-"
  89.   description = "Allow mysql and ssh port only"
  90.   vpc_id      = module.vpc.vpc_id
  91.  
  92.   ingress {
  93.     description      = "22 from Bastion"
  94.     from_port        = 22
  95.     to_port          = 22
  96.     protocol         = "tcp"
  97.     security_groups  = [aws_security_group.bastion.id]
  98.     ipv6_cidr_blocks = ["::/0"]
  99.   }
  100.   ingress {
  101.     description      = "MYSQL IP"
  102.     from_port        = 3306
  103.     to_port          = 3306
  104.     protocol         = "tcp"
  105.     security_groups  = [aws_security_group.frontend.id]
  106.     ipv6_cidr_blocks = ["::/0"]
  107.   }
  108.  
  109.   egress {
  110.     from_port        = 0
  111.     to_port          = 0
  112.     protocol         = "-1"
  113.     cidr_blocks      = ["0.0.0.0/0"]
  114.     ipv6_cidr_blocks = ["::/0"]
  115.   }
  116.  
  117.   tags = {
  118.     Name = "${var.myproject_name}-${var.project_env}-backend"
  119.   }
  120.   lifecycle {
  121.     create_before_destroy = true
  122.   }
  123. }
  124.  
  125. # Creating ec2-instance
  126.  
  127. resource "aws_instance" "bastion" {
  128.   ami                    = var.ami
  129.   subnet_id              = module.vpc.public_subnet1
  130.   instance_type          = var.instance_type
  131.   key_name               = var.key_name
  132.   vpc_security_group_ids = [aws_security_group.bastion.id]
  133.   user_data              = file("bastion.sh")
  134.   tags = {
  135.     "Name" = "${var.myproject_name}-${var.project_env}-bastion"
  136.   }
  137.  
  138. }
  139.  
  140. resource "aws_instance" "frontend" {
  141.   ami                    = var.ami
  142.   subnet_id              = module.vpc.public_subnet2
  143.   instance_type          = var.instance_type
  144.   key_name               = var.key_name
  145.   vpc_security_group_ids = [aws_security_group.frontend.id]
  146.   user_data              = file("frontend.sh")
  147.   tags = {
  148.     "Name" = "${var.myproject_name}-${var.project_env}-frontend"
  149.   }
  150. }
  151.  
  152. resource "aws_instance" "backend" {
  153.   ami                    = var.ami
  154.   subnet_id              = module.vpc.private_subnet1
  155.   instance_type          = var.instance_type
  156.   key_name               = var.key_name
  157.   vpc_security_group_ids = [aws_security_group.backend.id]
  158.   user_data              = file("backend.sh")
  159.   tags = {
  160.     "Name" = "${var.myproject_name}-${var.project_env}-backend"
  161.   }
  162.  
  163. }
  164.  
  165.  
  166. # Creating a private hosted zones
  167.  
  168. resource "aws_route53_zone" "uber" {
  169.   name = "${var.myproject_name}-${var.project_env}.local"
  170.  
  171.   vpc {
  172.     vpc_id = module.vpc.vpc_id
  173.   }
  174. }
  175.  
  176.  
  177. # Creatinng 3 records
  178.  
  179. resource "aws_route53_record" "frontend" {
  180.   zone_id = aws_route53_zone.uber.zone_id
  181.   name    = "frontend"
  182.   type    = "A"
  183.   ttl     = 5
  184.   records = [aws_instance.frontend.private_ip]
  185.  
  186. }
  187.  
  188. resource "aws_route53_record" "backend" {
  189.   zone_id = aws_route53_zone.uber.zone_id
  190.   name    = "backend"
  191.   type    = "A"
  192.   ttl     = 5
  193.   records = [aws_instance.backend.private_ip]
  194.  
  195. }
  196.  
  197. resource "aws_route53_record" "bastiom" {
  198.   zone_id = aws_route53_zone.uber.zone_id
  199.   name    = "bastion"
  200.   type    = "A"
  201.   ttl     = 5
  202.   records = [aws_instance.bastion.private_ip]
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment