{"id":853,"date":"2019-02-27T11:53:19","date_gmt":"2019-02-27T08:53:19","guid":{"rendered":"https:\/\/artem.services\/?p=805"},"modified":"2019-03-12T16:47:20","modified_gmt":"2019-03-12T13:47:20","slug":"terraform-aws-cross-region-peering-2","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=853&lang=en","title":{"rendered":"Terraform &#8212; AWS Cross Region Peering"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"size-full wp-image-99 aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform.png\" alt=\"\" width=\"1210\" height=\"418\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform.png 1210w, https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform-300x104.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform-768x265.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform-1024x354.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2018\/10\/terraform-954x330.png 954w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><\/p>\n<p><span class=\"tlid-translation translation\"><span class=\"\" title=\"\"><strong>Terraform<\/strong> configuration example, which creates <strong>2<\/strong> <strong>VPCs<\/strong> in different regions (<strong>EU<\/strong> and <strong>US<\/strong>) and creates connectivity between them.<\/span><\/span><\/p>\n<h4>variables.tf<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ FIRST VPC \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nvariable &quot;VPC_1_REGION&quot; {\r\n  default = &quot;us-east-1&quot;\r\n}\r\n\r\nvariable &quot;VPC_1_NAME&quot; {\r\n  default = &quot;artem-terraform-US&quot;\r\n}\r\n\r\nvariable &quot;VPC_1_KEY_INSTANCE&quot; { \r\n  default = &quot;artem.gatchenko&quot;\r\n}\r\n\r\nvariable &quot;VPC_1_SUBNET&quot; { \r\n  default = &quot;192.168.1.0\/24&quot;\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ SECOND VPC \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nvariable &quot;VPC_2_REGION&quot; {\r\n  default = &quot;eu-west-2&quot;\r\n}\r\n\r\nvariable &quot;VPC_2_NAME&quot; {\r\n  default = &quot;artem-terraform-EU&quot;\r\n}\r\n\r\nvariable &quot;VPC_2_KEY_INSTANCE&quot; { \r\n  default = &quot;artem.gatchenko&quot;\r\n}\r\n\r\nvariable &quot;VPC_2_SUBNET&quot; { \r\n  default = &quot;192.168.2.0\/24&quot;\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ OTHER \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nvariable &quot;INSTANCE_TYPE&quot; {\r\n  default = &quot;t2.micro&quot;\r\n}\r\n\r\nvariable &quot;AMI&quot; {\r\n  type = &quot;map&quot;\r\n  default = {\r\n    eu-west-1 = &quot;ami-f90a4880&quot;\r\n    eu-west-2 = &quot;ami-f976839e&quot;\r\n    eu-west-3 = &quot;ami-0e55e373&quot;\r\n    us-east-1 = &quot;ami-0ff8a91507f77f867&quot;\r\n    us-west-1 = &quot;ami-0bdb828fd58c52235&quot;\r\n    eu-west-1 = &quot;ami-047bb4163c506cd98&quot;\r\n    ap-northeast-1 = &quot;ami-06cd52961ce9f0d85&quot;\r\n    ap-southeast-1 = &quot;ami-08569b978cc4dfa10&quot;\r\n  }\r\n}\r\n<\/pre>\n<h4><\/h4>\n<p><!--more--><\/p>\n<h4>main.tf<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nprovider &quot;aws&quot; {\r\n  region = &quot;us-east-1&quot;\r\n}\r\n\r\nprovider &quot;aws&quot; {\r\n  alias = &quot;vpc1&quot;\r\n  region = &quot;${var.VPC_1_REGION}&quot;\r\n}\r\n\r\nprovider &quot;aws&quot; {\r\n  alias = &quot;vpc2&quot;\r\n  region = &quot;${var.VPC_2_REGION}&quot;\r\n}\r\n<\/pre>\n<h4>vpc1.tf<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\/\/ CREATE VPC\r\nresource &quot;aws_vpc&quot; &quot;vpc1&quot; {\r\n  provider = &quot;aws.vpc1&quot;\r\n  cidr_block = &quot;${var.VPC_1_SUBNET}&quot;\r\n  enable_dns_hostnames = &quot;true&quot;\r\n  enable_dns_support = &quot;true&quot;\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_1_NAME}&quot;\r\n  }\r\n}\r\n\r\n\/\/ CREATE GATEWAY\r\nresource &quot;aws_internet_gateway&quot; &quot;vpc1&quot; {\r\n  provider = &quot;aws.vpc1&quot;\r\n  vpc_id = &quot;${aws_vpc.vpc1.id}&quot;\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_1_NAME}&quot;\r\n  }\r\n}\r\n\r\n\/\/ CREATE ROUTE TABLE\r\nresource &quot;aws_route_table&quot; &quot;vpc1&quot; {\r\n  provider = &quot;aws.vpc1&quot;\r\n  vpc_id = &quot;${aws_vpc.vpc1.id}&quot;\r\n  route {\r\n    cidr_block = &quot;0.0.0.0\/0&quot;\r\n    gateway_id = &quot;${aws_internet_gateway.vpc1.id}&quot;\r\n  }\r\n\r\n  route {\r\n    cidr_block = &quot;${var.VPC_2_SUBNET}&quot;\r\n    gateway_id = &quot;${aws_vpc_peering_connection.vpc_peering.id}&quot;\r\n  }\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_1_NAME}&quot;\r\n  }\r\n}\r\n\r\n\/\/ CREATE SUBNET\r\nresource &quot;aws_subnet&quot; &quot;vpc1&quot; {\r\n  provider = &quot;aws.vpc1&quot;\r\n  vpc_id     = &quot;${aws_vpc.vpc1.id}&quot;\r\n  cidr_block = &quot;${var.VPC_1_SUBNET}&quot;\r\n\r\n  map_public_ip_on_launch = &quot;true&quot;\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_1_NAME}&quot;\r\n  }\r\n}\r\n\r\nresource &quot;aws_route_table_association&quot; &quot;vpc1&quot; {\r\n  provider = &quot;aws.vpc1&quot;\r\n  subnet_id      = &quot;${aws_subnet.vpc1.id}&quot;\r\n  route_table_id = &quot;${aws_route_table.vpc1.id}&quot;\r\n}\r\n\r\n\/\/ CREATE SECURITY GROUP\r\nresource &quot;aws_security_group&quot; &quot;vpc1&quot; {\r\n  provider = &quot;aws.vpc1&quot;\r\n  vpc_id      = &quot;${aws_vpc.vpc1.id}&quot;\r\n\r\n  ingress {\r\n    from_port   = 22\r\n    to_port     = 22\r\n    protocol    = &quot;tcp&quot;\r\n    cidr_blocks = [&quot;0.0.0.0\/0&quot;]\r\n    description = &quot;Allow input SSH&quot;\r\n  }\r\n\r\n  ingress {\r\n    from_port   = 0\r\n    to_port     = 0\r\n    protocol    = &quot;-1&quot;\r\n    cidr_blocks = [&quot;${var.VPC_2_SUBNET}&quot;]\r\n    description = &quot;Allow all input traffic from other VPC&quot;\r\n  }\r\n\r\n  egress {\r\n    from_port   = 0\r\n    to_port     = 0\r\n    protocol    = &quot;-1&quot;\r\n    cidr_blocks = [&quot;0.0.0.0\/0&quot;]\r\n    description = &quot;Allow all ouput traffic from other VPC&quot;\r\n  }\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_1_NAME}&quot;\r\n    Description = &quot;${var.VPC_1_NAME}&quot;\r\n  }\r\n\r\n}\r\n\r\n\r\n\/\/ CREATE INSTANCE\r\nresource &quot;aws_instance&quot; &quot;vpc1&quot; {\r\n  provider = &quot;aws.vpc1&quot;\r\n\/\/  ami           = &quot;${lookup(var.AMI, var.region)}&quot;\r\n  ami = &quot;ami-0ff8a91507f77f867&quot;\r\n  instance_type = &quot;${var.INSTANCE_TYPE}&quot;\r\n  key_name      = &quot;${var.VPC_1_KEY_INSTANCE}&quot;\r\n  vpc_security_group_ids = [&quot;${aws_security_group.vpc1.id}&quot;]\r\n  subnet_id = &quot;${aws_subnet.vpc1.id}&quot;\r\n  associate_public_ip_address = true\r\n  source_dest_check = false\r\n\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_1_NAME}&quot;\r\n  }\r\n}\r\n\r\noutput &quot;aws-id-subnet-artem-terraform-VPC1&quot; {\r\n  value = &quot;${aws_subnet.vpc1.id}&quot;\r\n}\r\n<\/pre>\n<h4>vpc2.tf<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\/\/ CREATE VPC\r\nresource &quot;aws_vpc&quot; &quot;vpc2&quot; {\r\n  provider = &quot;aws.vpc2&quot;\r\n  cidr_block = &quot;${var.VPC_2_SUBNET}&quot;\r\n  enable_dns_hostnames = &quot;true&quot;\r\n  enable_dns_support = &quot;true&quot;\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_2_NAME}&quot;\r\n  }\r\n}\r\n\r\n\/\/ CREATE GATEWAY\r\nresource &quot;aws_internet_gateway&quot; &quot;vpc2&quot; {\r\n  provider = &quot;aws.vpc2&quot;\r\n  vpc_id = &quot;${aws_vpc.vpc2.id}&quot;\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_2_NAME}&quot;\r\n  }\r\n}\r\n\r\n\/\/ CREATE ROUTE TABLE\r\nresource &quot;aws_route_table&quot; &quot;vpc2&quot; {\r\n  provider = &quot;aws.vpc2&quot;\r\n  vpc_id = &quot;${aws_vpc.vpc2.id}&quot;\r\n  route {\r\n    cidr_block = &quot;0.0.0.0\/0&quot;\r\n    gateway_id = &quot;${aws_internet_gateway.vpc2.id}&quot;\r\n  }\r\n\r\n  route {\r\n    cidr_block = &quot;${var.VPC_1_SUBNET}&quot;\r\n    gateway_id = &quot;${aws_vpc_peering_connection.vpc_peering.id}&quot;\r\n  }\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_2_NAME}&quot;\r\n  }\r\n}\r\n\r\n\/\/ CREATE SUBNET\r\nresource &quot;aws_subnet&quot; &quot;vpc2&quot; {\r\n  provider = &quot;aws.vpc2&quot;\r\n  vpc_id     = &quot;${aws_vpc.vpc2.id}&quot;\r\n  cidr_block = &quot;${var.VPC_2_SUBNET}&quot;\r\n\r\n  map_public_ip_on_launch = &quot;true&quot;\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_2_NAME}&quot;\r\n  }\r\n}\r\n\r\nresource &quot;aws_route_table_association&quot; &quot;vpc2&quot; {\r\n  provider = &quot;aws.vpc2&quot;\r\n  subnet_id      = &quot;${aws_subnet.vpc2.id}&quot;\r\n  route_table_id = &quot;${aws_route_table.vpc2.id}&quot;\r\n}\r\n\r\n\/\/ CREATE SECURITY GROUP\r\nresource &quot;aws_security_group&quot; &quot;vpc2&quot; {\r\n  provider = &quot;aws.vpc2&quot;\r\n  vpc_id      = &quot;${aws_vpc.vpc2.id}&quot;\r\n\r\n  ingress {\r\n    from_port   = 22\r\n    to_port     = 22\r\n    protocol    = &quot;tcp&quot;\r\n    cidr_blocks = [&quot;0.0.0.0\/0&quot;]\r\n    description = &quot;Allow input SSH&quot;\r\n  }\r\n\r\n  ingress {\r\n    from_port   = 0\r\n    to_port     = 0\r\n    protocol    = &quot;-1&quot;\r\n    cidr_blocks = [&quot;${var.VPC_1_SUBNET}&quot;]\r\n    description = &quot;Allow all input traffic from other VPC&quot;\r\n  }\r\n\r\n  egress {\r\n    from_port   = 0\r\n    to_port     = 0\r\n    protocol    = &quot;-1&quot;\r\n    cidr_blocks = [&quot;0.0.0.0\/0&quot;]\r\n    description = &quot;Allow all ouput traffic from other VPC&quot;\r\n  }\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_2_NAME}&quot;\r\n    Description = &quot;${var.VPC_2_NAME}&quot;\r\n  }\r\n\r\n}\r\n\r\n\r\n\/\/ CREATE INSTANCE\r\nresource &quot;aws_instance&quot; &quot;vpc2&quot; {\r\n  provider = &quot;aws.vpc2&quot;\r\n\/\/  ami           = &quot;${lookup(var.AMI, var.region)}&quot;\r\n  ami = &quot;ami-f976839e&quot;\r\n  instance_type = &quot;${var.INSTANCE_TYPE}&quot;\r\n  key_name      = &quot;${var.VPC_2_KEY_INSTANCE}&quot;\r\n  vpc_security_group_ids = [&quot;${aws_security_group.vpc2.id}&quot;]\r\n  subnet_id = &quot;${aws_subnet.vpc2.id}&quot;\r\n  associate_public_ip_address = true\r\n  source_dest_check = false\r\n\r\n\r\n  tags {\r\n    Name = &quot;${var.VPC_2_NAME}&quot;\r\n  }\r\n}\r\n\r\noutput &quot;aws-id-subnet-artem-terraform-VPC2&quot; {\r\n  value = &quot;${aws_subnet.vpc2.id}&quot;\r\n}\r\n<\/pre>\n<h4>peering.tf<\/h4>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\/\/ CREATE PEERING BETWEEN VPC1 AND VPC2\r\n\r\nresource &quot;aws_vpc_peering_connection&quot; &quot;vpc_peering&quot; {\r\n  provider = &quot;aws.vpc1&quot;\r\n  peer_vpc_id = &quot;${aws_vpc.vpc2.id}&quot;\r\n  vpc_id = &quot;${aws_vpc.vpc1.id}&quot;\r\n  peer_region =&quot;${var.VPC_2_REGION}&quot;\r\n  \r\n  tags {\r\n    Name = &quot;VPC Peering VPC1 and VPC2&quot;\r\n  }\r\n}\r\n\r\nresource &quot;aws_vpc_peering_connection_accepter&quot; &quot;peering-accepter&quot; {\r\n  provider = &quot;aws.vpc2&quot;\r\n  provider                  = &quot;aws&quot;\r\n  vpc_peering_connection_id = &quot;${aws_vpc_peering_connection.vpc_peering.id}&quot;\r\n  auto_accept               = true\r\n}\r\n<\/pre>\n<p><span class=\"tlid-translation translation\"><span class=\"\" title=\"\">Download all one archive can be<\/span><\/span> <a href=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Terraform-CrossRegionPeering.zip\">here<\/a>.<\/p>\n<p><span class=\"tlid-translation translation\"><span class=\"\" title=\"\">How to run the Terraform template:<\/span><\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nterraform init\r\nterraform plan\r\nterraform apply\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Terraform configuration example, which creates 2 VPCs in different regions (EU and US) and creates connectivity between them. variables.tf<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[541],"tags":[543,593,595,555,597],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/853"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=853"}],"version-history":[{"count":3,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/853\/revisions"}],"predecessor-version":[{"id":859,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/853\/revisions\/859"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}