Terraform script that sets up VPC peering between two VPCs


Terraform script that sets up VPC peering between two VPCs


This script creates two VPCs, "vpc1" and "vpc2", and then creates a VPC peering connection between them using the aws_vpc_peering_connection resource. The auto_accept property is set to true, which means that the peering connection will be automatically accepted, so you don't need to manually accept it.

script creates two route resources, one for each VPC, that allow traffic to flow between the VPCs using the VPC peering connection. The route_table_id property is set to the main route table of each VPC, and the destination_cidr_block property is set to the CIDR block of the other VPC

Here is the below code for the same :

#########################################################################

resource "aws_vpc" "vpc1" {

  cidr_block = "10.0.0.0/16"

  enable_dns_hostnames = true

}


resource "aws_vpc" "vpc2" {

  cidr_block = "10.1.0.0/16"

  enable_dns_hostnames = true

}


resource "aws_vpc_peering_connection" "example" {

  vpc_id = aws_vpc.vpc1.id

  peer_vpc_id = aws_vpc.vpc2.id

  auto_accept = true

}


resource "aws_route" "vpc1_to_vpc2" {

  route_table_id = aws_vpc.vpc1.main_route_table_id

  destination_cidr_block = aws_vpc.vpc2.cidr_block

  vpc_peering_connection_id = aws_vpc_peering_connection.example.id

}


resource "aws_route" "vpc2_to_vpc1" {

  route_table_id = aws_vpc.vpc2.main_route_table_id

  destination_cidr_block = aws_vpc.vpc1.cidr_block

  vpc_peering_connection_id = aws_vpc_peering_connection.example.id

}


###################################################################

For more Terraform scripts Read below :


Post a Comment

Previous Post Next Post

Contact Form