chrisluiz16

VPC-PEER

May 7th, 2023
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.24 KB | Source Code | 0 0
  1. # Fetching the details of prod VPC
  2.  
  3. data "aws_vpc" "prod" {
  4.   filter {
  5.     name   = "tag:Env"
  6.     values = ["Prod"]
  7.   }
  8. }
  9.  
  10. # Fetching the details of the dev VPC
  11.  
  12. data "aws_vpc" "dev" {
  13.   filter {
  14.     name   = "tag:Env"
  15.     values = ["Dev"]
  16.   }
  17. }
  18.  
  19.  
  20. # Fetching the details of Zomato-prod public route tables
  21.  
  22. data "aws_route_table" "public" {
  23.   filter {
  24.     name   = "tag:Name"
  25.     values = ["Zomato-Prod-public"]
  26.   }
  27. }
  28. # Fetching the details of Zomato-dev public route tables
  29.  
  30. data "aws_route_table" "dev_public" {
  31.   filter {
  32.     name   = "tag:Name"
  33.     values = ["Zomato-Dev-public"]
  34.   }
  35. }
  36.  
  37.  
  38. # Fetching the details Zomato-private  prod route tables
  39.  
  40. data "aws_route_table" "private" {
  41.   filter {
  42.     name   = "tag:Name"
  43.     values = ["Zomato-Prod-private"]
  44.   }
  45. }
  46. # Fetching the details Zomato-private dev route tables
  47.  
  48. data "aws_route_table" "dev_private" {
  49.   filter {
  50.     name   = "tag:Name"
  51.     values = ["Zomato-Dev-private"]
  52.   }
  53. }
  54.  
  55.  
  56. # Creating the VPC peer connection
  57.  
  58. resource "aws_vpc_peering_connection" "dev" {
  59.   peer_vpc_id = data.aws_vpc.dev.id
  60.   vpc_id      = data.aws_vpc.prod.id
  61.   auto_accept = true
  62.   tags = {
  63.     "Name" = "Peer connection"
  64.   }
  65. }
  66.  
  67.  
  68. #Prod Route tables edit
  69.  
  70. resource "aws_route" "prod_public" {
  71.   route_table_id            = data.aws_route_table.public.route_table_id
  72.   destination_cidr_block    = data.aws_vpc.dev.cidr_block
  73.   vpc_peering_connection_id = aws_vpc_peering_connection.dev.id
  74. }
  75.  
  76. resource "aws_route" "prod_private" {
  77.   route_table_id            = data.aws_route_table.private.route_table_id
  78.   destination_cidr_block    = data.aws_vpc.dev.cidr_block
  79.   vpc_peering_connection_id = aws_vpc_peering_connection.dev.id
  80. }
  81.  
  82. #Dev Route tables edi
  83.  
  84. resource "aws_route" "dev_public" {
  85.   route_table_id            = data.aws_route_table.dev_public.route_table_id
  86.   destination_cidr_block    = data.aws_vpc.prod.cidr_block
  87.   vpc_peering_connection_id = aws_vpc_peering_connection.dev.id
  88. }
  89.  
  90. resource "aws_route" "dev_private" {
  91.   route_table_id            = data.aws_route_table.dev_private.route_table_id
  92.   destination_cidr_block    = data.aws_vpc.prod.cidr_block
  93.   vpc_peering_connection_id = aws_vpc_peering_connection.dev.id
  94. }
  95.  
  96.  
  97. # Security group editing for zomato-prod bastion in order to access to zomato dev frontend & backend
  98.  
  99. data "aws_subnet" "zomato_prod" {
  100.   filter {
  101.     name   = "tag:Name"
  102.     values = ["Zomato-Prod-public1"]
  103.   }
  104. }
  105.  
  106. data "aws_security_group" "frontend" {
  107.   filter {
  108.     name   = "tag:Name"
  109.     values = ["Zomato-Dev-frontend"]
  110.   }
  111. }
  112.  
  113. data "aws_security_group" "backend" {
  114.   filter {
  115.     name   = "tag:Name"
  116.     values = ["Zomato-Dev-backend"]
  117.   }
  118. }
  119.  
  120. resource "aws_security_group_rule" "dev_frontend" {
  121.   type              = "ingress"
  122.   from_port         = 22
  123.   to_port           = 22
  124.   protocol          = "tcp"
  125.   cidr_blocks       = [data.aws_subnet.zomato_prod.cidr_block]
  126.   security_group_id = data.aws_security_group.frontend.id
  127. }
  128. resource "aws_security_group_rule" "dev_backend" {
  129.   type              = "ingress"
  130.   from_port         = 22
  131.   to_port           = 22
  132.   protocol          = "tcp"
  133.   cidr_blocks       = [data.aws_subnet.zomato_prod.cidr_block]
  134.   security_group_id = data.aws_security_group.backend.id
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment