Create a VPC firewall with Terraform to inspect traffic between two VPCs connected by a VPC Peering Connection

Updated at:

This topic describes how to use Terraform to create a VPC firewall to inspect traffic between two VPCs that are connected by a VPC Peering connection.

Note

You can run the sample code in this topic in Terraform Explorer.

Prerequisites

  • Your Alibaba Cloud account, also known as a root account, has full control over all your resources. A compromised root account poses significant security risks. We recommend that you create and use a RAM user and an AccessKey for that user. For more information, see Create a RAM user and Create an AccessKey.

  • Grant the required permissions to the RAM user by using the following policy. For more information, see Manage permissions for a RAM user.

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "yundun-cloudfirewall:*",
                    "yundun-ndr:*",
                    "vpc:CreateVpc",
                    "vpc:DeleteVpc",
                    "vpc:DescribeVpcs",
                    "vpc:CreateVSwitch",
                    "vpc:DeleteVSwitch",
                    "vpc:DescribeVSwitches",
                    "vpc:CreateRouteEntry",
                    "vpc:DeleteRouteEntry",
                    "vpc:DescribeRouteEntries",
                    "vpc:CreateVpcPeerConnection",
                    "vpc:DeleteVpcPeerConnection",
                    "vpc:DescribeVpcPeerConnections",
                    "cloudfirewall:CreateVpcFirewall",
                    "cloudfirewall:DeleteVpcFirewall",
                    "cloudfirewall:DescribeVpcFirewalls"
                ],
                "Resource": "*"
            }
        ]
    }
  • Prepare a Terraform runtime environment. You can use one of the following methods to run Terraform.

    Use Terraform in Explorer: Alibaba Cloud provides an online runtime environment. No installation is required, and you can use and test Terraform online at no cost.

    Cloud Shell: Terraform is pre-installed with identity credentials pre-configured. You can run Terraform commands directly in Cloud Shell at a low cost.

    Install and configure Terraform on your computer: Best suited for environments with limited network connectivity or custom developer setups.

    Important

    Make sure that your Terraform version is 0.12.28 or later. To check the version, run the terraform --version command.

Resources

alicloud_cloud_firewall_vpc_firewall: creates and manages a VPC firewall.

Create a VPC firewall

This example shows how to create a VPC firewall.

  1. Create a working directory and create a configuration file named main.tf in the directory. The main.tf file is the main Terraform file that defines the resources to be deployed.

    variable "region" {
      default = "cn-heyuan"
    }
    provider "alicloud" {
      region = var.region
    }
    # Obtain the UID of the current Alibaba Cloud account.
    data "alicloud_account" "current" {
    }
    # Create VPC 1.
    resource "alicloud_vpc" "vpc" {
      vpc_name   = "tf-vpc-01"
      cidr_block = "192.168.0.0/16"
    }
    # Create VPC 2.
    resource "alicloud_vpc" "vpc1" {
      vpc_name   = "tf-vpc-02"
      cidr_block = "172.16.0.0/12"
    }
    # Create a vSwitch with the 192.168.10.0/24 CIDR block.
    resource "alicloud_vswitch" "vsw" {
      vpc_id       = alicloud_vpc.vpc.id
      cidr_block   = "192.168.10.0/24"
      zone_id      = "cn-heyuan-a"
      vswitch_name = "tf-vpc-01-vsw-1"
    }
    # Create another vSwitch with the 192.168.20.0/24 CIDR block.
    resource "alicloud_vswitch" "vsw1" {
      vpc_id       = alicloud_vpc.vpc.id
      cidr_block   = "192.168.20.0/24"
      zone_id      = "cn-heyuan-b"
      vswitch_name = "tf-vpc-01-vsw-2"
    }
    # Create a vSwitch with the 172.16.10.0/24 CIDR block.
    resource "alicloud_vswitch" "vsw2" {
      vpc_id       = alicloud_vpc.vpc1.id
      cidr_block   = "172.16.10.0/24"
      zone_id      = "cn-heyuan-a"
      vswitch_name = "tf-vpc-02-vsw-1"
    }
    # Create another vSwitch with the 172.16.20.0/24 CIDR block.
    resource "alicloud_vswitch" "vsw3" {
      vpc_id       = alicloud_vpc.vpc1.id
      cidr_block   = "172.16.20.0/24"
      zone_id      = "cn-heyuan-b"
      vswitch_name = "tf-vpc-02-vsw-2"
    }
    # Create a VPC Peering connection.
    resource "alicloud_vpc_peer_connection" "default" {
      # The name of the VPC Peering connection.
      peer_connection_name = "tf-vpc-peer-example"
      # The ID of the requester VPC.
      vpc_id = alicloud_vpc.vpc.id
      # The ID of the Alibaba Cloud account to which the accepter VPC belongs.
      accepting_ali_uid = data.alicloud_account.current.id
      # The region ID of the accepter VPC. If you create a VPC Peering connection in the same region, enter the same region ID as the requester. If you create a cross-region VPC Peering connection, enter a different region ID.
      accepting_region_id = "cn-heyuan"
      # The ID of the accepter VPC.
      accepting_vpc_id = alicloud_vpc.vpc1.id
      # The description.
      description = "tf-example"
      # Specifies whether to forcefully delete the instance.
      force_delete = true
    }
    # The accepter.
    resource "alicloud_vpc_peer_connection_accepter" "default" {
      instance_id = alicloud_vpc_peer_connection.default.id
      force_delete = true
    }
    # Configure a route entry for VPC 1.
    resource "alicloud_route_entry" "foo" {
      # The route table ID of VPC 1.
      route_table_id = alicloud_vpc.vpc.route_table_id
      # The destination CIDR block. You can specify a custom CIDR block.
      destination_cidrblock = "1.2.3.4/32"
      # The type of the next hop.
      nexthop_type = "VpcPeer"
      # The ID of the next hop.
      nexthop_id = alicloud_vpc_peer_connection.default.id
    }
    # Configure a route entry for VPC 2.
    resource "alicloud_route_entry" "foo1" {
      # The route table ID of VPC 2.
      route_table_id = alicloud_vpc.vpc1.route_table_id
      # The destination CIDR block. You can specify a custom CIDR block.
      destination_cidrblock = "4.3.2.1/32"
      # The type of the next hop.
      nexthop_type = "VpcPeer"
      # The ID of the next hop.
      nexthop_id = alicloud_vpc_peer_connection.default.id
    }
    # Wait for prerequisite resources to be created.
    resource "time_sleep" "wait_before_firewall" {
      # Wait for the route entries to be created.
      depends_on = [
        alicloud_route_entry.foo,
        alicloud_route_entry.foo1
      ]
      create_duration = "720s" # Set a duration based on your requirements.
    }
    # Delay the creation of the firewall.
    resource "null_resource" "wait_for_firewall" {
      provisioner "local-exec" {
        command = "echo 'Waiting for the firewall to be ready...'"
      }
      # Make sure that the prerequisite resources are created.
      depends_on = [time_sleep.wait_before_firewall]
    }
    # The VPC firewall for the VPC Peering connection.
    resource "alicloud_cloud_firewall_vpc_firewall" "default" {
      # Add a dependency on the prerequisite resources.
      depends_on = [
        null_resource.wait_for_firewall
      ]
      timeouts {
        create = "30m" # Set a timeout period for the creation.
      }
      # The name of the instance.
      vpc_firewall_name = "tf-vpc-firewall-example"
      # The UID of the member account.
      member_uid        = data.alicloud_account.current.id
      local_vpc {
        # The ID of the requester VPC.
        vpc_id = alicloud_vpc.vpc.id
        # The region.
        region_no = "cn-heyuan"
        # The route entries.
        local_vpc_cidr_table_list {
          # The ID of the route table.
          local_route_table_id = alicloud_vpc.vpc.route_table_id
          local_route_entry_list {
            # The next hop.
            local_next_hop_instance_id = alicloud_vpc_peer_connection.default.id
            # The destination CIDR block.
            local_destination_cidr = alicloud_route_entry.foo.destination_cidrblock
          }
        }
      }
      peer_vpc {
        # The ID of the accepter VPC.
        vpc_id = alicloud_vpc.vpc1.id
        # The region.
        region_no = "cn-heyuan"
        # The route entries.
        peer_vpc_cidr_table_list {
          # The ID of the route table.
          peer_route_table_id = alicloud_vpc.vpc1.route_table_id
          peer_route_entry_list {
            # The destination CIDR block.
            peer_destination_cidr = alicloud_route_entry.foo1.destination_cidrblock
            # The next hop.
            peer_next_hop_instance_id = alicloud_vpc_peer_connection.default.id
          }
        }
      }
      # The status of the resource. Valid values:
      # open: The VPC firewall is enabled after it is created.
      # close: The VPC firewall is disabled after it is created.
      status = "open"
    }
    output "vpc_id" {
      value = alicloud_vpc.vpc.id
    }
    output "vpc1_id" {
      value = alicloud_vpc.vpc1.id
    }
    output "route_table_id_vpc" {
      value = alicloud_vpc.vpc.route_table_id
    }
    output "route_table_id_vpc1" {
      value = alicloud_vpc.vpc1.route_table_id
    }
    output "foo_nexthop_id" {
      value = alicloud_vpc_peer_connection.default.id
    }
    output "foo1_nexthop_id" {
      value = alicloud_vpc_peer_connection.default.id
    }
    output "cidrblock" {
      value = alicloud_route_entry.foo.destination_cidrblock
    }
    output "cidrblock1" {
      value = alicloud_route_entry.foo1.destination_cidrblock
    }
  2. Run the following command to initialize the Terraform runtime environment.

    terraform init

    The following output indicates that Terraform is initialized:

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.203.0...
    
    
    Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers.
    
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. Create an execution plan and preview the changes.

    terraform plan
  4. Run the following command to create a VPC firewall to inspect traffic between the two VPCs that are connected by the VPC Peering connection:

    terraform apply

    When prompted, enter yes and press Enter. Wait for the command to complete. The following output indicates that the VPC firewall is created.

    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    alicloud_vpc.vpc: Creating...
    alicloud_vpc.vpc1: Creating...
    alicloud_vpc.vpc1: Creation complete after 6s [id=vpc-f8z3bgpc9436064a***]
    alicloud_vswitch.vsw2: Creating...
    alicloud_vswitch.vsw3: Creating...
    alicloud_vpc.vpc: Creation complete after 6s [id=vpc-f8zbmuyrti2q3t3exi***]
    alicloud_vpc_peer_connection.default: Creating...
    alicloud_vswitch.vsw1: Creating...
    alicloud_vswitch.vsw: Creating...
    alicloud_vswitch.vsw3: Creation complete after 4s [id=vsw-f8zxfkuawt6h3zorst***]
    alicloud_vswitch.vsw: Creation complete after 4s [id=vsw-f8zbfkhc4odb6fv3y4***]
    alicloud_vpc_peer_connection.default: Creation complete after 6s [id=pcc-rwz8io7yddag5y***]
    alicloud_vpc_peer_connection_accepter.default: Creating...
    alicloud_vswitch.vsw1: Creation complete after 7s [id=vsw-f8z88qcqyfbb5x2nj9***]
    alicloud_vswitch.vsw2: Creation complete after 7s [id=vsw-f8zqv2i961em95c7bv***]
    alicloud_vpc_peer_connection_accepter.default: Creation complete after 6s [id=pcc-rwz8io7yddag5ya***]
    alicloud_route_entry.foo: Creating...
    alicloud_route_entry.foo1: Creating...
    alicloud_route_entry.foo1: Creation complete after 6s [id=vtb-f8zbaphqlvdnb7njt1***:vrt-f8ze0dot16bcip5o8d***:4.3.2.1/32:VpcPeer:pcc-rwz8io7yddag5y***]
    alicloud_route_entry.foo: Creation complete after 6s [id=vtb-f8zukaban4cfna8f8k***:vrt-f8z23psp6f1ecy44z7***:1.2.3.4/32:VpcPeer:pcc-rwz8io7yddag5ya***]
    time_sleep.wait_before_firewall: Creating...
    time_sleep.wait_before_firewall: Still creating... [10s elapsed]
    ...
    time_sleep.wait_before_firewall: Creation complete after 12m0s [id=2024-11-05T01:44:57Z]
    null_resource.wait_for_firewall: Creating...
    null_resource.wait_for_firewall: Provisioning with 'local-exec'...
    null_resource.wait_for_firewall (local-exec): Executing: ["/bin/sh" "-c" "echo 'Waiting for the firewall to be ready...'"]
    null_resource.wait_for_firewall (local-exec): Waiting for the firewall to be ready...
    null_resource.wait_for_firewall: Creation complete after 0s [id=5344790266853010843]
    alicloud_cloud_firewall_vpc_firewall.default: Creating...
    alicloud_cloud_firewall_vpc_firewall.default: Still creating... [10s elapsed]
    ...
    alicloud_cloud_firewall_vpc_firewall.default: Creation complete after 12m1s [id=vfw-782be77253a0462e8***]
    
    Apply complete! Resources: 13 added, 0 changed, 0 destroyed.
  5. Verify the result

    Use the CLI

    You can run the following command to view details about the resources that Terraform created:

    terraform show

    image

    Use the console

    Log on to the Cloud Firewall console. On the Firewall > VPC Firewall page, search for the VPC firewall instance ID to view details about the instance.

Clean up resources

When you no longer need the resources created and managed by Terraform, run the following command to release the resources. For more information about the terraform destroy command, see Common Terraform commands.

terraform destroy

Complete sample code

Note

You can run the sample code in this topic in Terraform Explorer.

Sample code

variable "region" {
  default = "cn-heyuan"
}
provider "alicloud" {
  region = var.region
}
# Obtain the UID of the current Alibaba Cloud account.
data "alicloud_account" "current" {
}
# Create VPC 1.
resource "alicloud_vpc" "vpc" {
  vpc_name   = "tf-vpc-01"
  cidr_block = "192.168.0.0/16"
}
# Create VPC 2.
resource "alicloud_vpc" "vpc1" {
  vpc_name   = "tf-vpc-02"
  cidr_block = "172.16.0.0/12"
}
# Create a vSwitch with the 192.168.10.0/24 CIDR block.
resource "alicloud_vswitch" "vsw" {
  vpc_id       = alicloud_vpc.vpc.id
  cidr_block   = "192.168.10.0/24"
  zone_id      = "cn-heyuan-a"
  vswitch_name = "tf-vpc-01-vsw-1"
}
# Create another vSwitch with the 192.168.20.0/24 CIDR block.
resource "alicloud_vswitch" "vsw1" {
  vpc_id       = alicloud_vpc.vpc.id
  cidr_block   = "192.168.20.0/24"
  zone_id      = "cn-heyuan-b"
  vswitch_name = "tf-vpc-01-vsw-2"
}
# Create a vSwitch with the 172.16.10.0/24 CIDR block.
resource "alicloud_vswitch" "vsw2" {
  vpc_id       = alicloud_vpc.vpc1.id
  cidr_block   = "172.16.10.0/24"
  zone_id      = "cn-heyuan-a"
  vswitch_name = "tf-vpc-02-vsw-1"
}
# Create another vSwitch with the 172.16.20.0/24 CIDR block.
resource "alicloud_vswitch" "vsw3" {
  vpc_id       = alicloud_vpc.vpc1.id
  cidr_block   = "172.16.20.0/24"
  zone_id      = "cn-heyuan-b"
  vswitch_name = "tf-vpc-02-vsw-2"
}
# Create a VPC Peering connection.
resource "alicloud_vpc_peer_connection" "default" {
  # The name of the VPC Peering connection.
  peer_connection_name = "tf-vpc-peer-example"
  # The ID of the requester VPC.
  vpc_id = alicloud_vpc.vpc.id
  # The ID of the Alibaba Cloud account to which the accepter VPC belongs.
  accepting_ali_uid = data.alicloud_account.current.id
  # The region ID of the accepter VPC. If you create a VPC Peering connection in the same region, enter the same region ID as the requester. If you create a cross-region VPC Peering connection, enter a different region ID.
  accepting_region_id = "cn-heyuan"
  # The ID of the accepter VPC.
  accepting_vpc_id = alicloud_vpc.vpc1.id
  # The description.
  description = "tf-example"
  # Specifies whether to forcefully delete the instance.
  force_delete = true
}
# The accepter.
resource "alicloud_vpc_peer_connection_accepter" "default" {
  instance_id = alicloud_vpc_peer_connection.default.id
  force_delete = true
}
# Configure a route entry for VPC 1.
resource "alicloud_route_entry" "foo" {
  # The route table ID of VPC 1.
  route_table_id = alicloud_vpc.vpc.route_table_id
  # The destination CIDR block. You can specify a custom CIDR block.
  destination_cidrblock = "1.2.3.4/32"
  # The type of the next hop.
  nexthop_type = "VpcPeer"
  # The ID of the next hop.
  nexthop_id = alicloud_vpc_peer_connection.default.id
}
# Configure a route entry for VPC 2.
resource "alicloud_route_entry" "foo1" {
  # The route table ID of VPC 2.
  route_table_id = alicloud_vpc.vpc1.route_table_id
  # The destination CIDR block. You can specify a custom CIDR block.
  destination_cidrblock = "4.3.2.1/32"
  # The type of the next hop.
  nexthop_type = "VpcPeer"
  # The ID of the next hop.
  nexthop_id = alicloud_vpc_peer_connection.default.id
}
# Wait for prerequisite resources to be created.
resource "time_sleep" "wait_before_firewall" {
  # Wait for the route entries to be created.
  depends_on = [
    alicloud_route_entry.foo,
    alicloud_route_entry.foo1
  ]
  create_duration = "720s" # Set a duration based on your requirements.
}
# Delay the creation of the firewall.
resource "null_resource" "wait_for_firewall" {
  provisioner "local-exec" {
    command = "echo 'Waiting for the firewall to be ready...'"
  }
  # Make sure that the prerequisite resources are created.
  depends_on = [time_sleep.wait_before_firewall]
}
# The VPC firewall for the VPC Peering connection.
resource "alicloud_cloud_firewall_vpc_firewall" "default" {
  # Add a dependency on the prerequisite resources.
  depends_on = [
    null_resource.wait_for_firewall
  ]
  timeouts {
    create = "30m" # Set a timeout period for the creation.
  }
  # The name of the instance.
  vpc_firewall_name = "tf-vpc-firewall-example"
  # The UID of the member account.
  member_uid        = data.alicloud_account.current.id
  local_vpc {
    # The ID of the requester VPC.
    vpc_id = alicloud_vpc.vpc.id
    # The region.
    region_no = "cn-heyuan"
    # The route entries.
    local_vpc_cidr_table_list {
      # The ID of the route table.
      local_route_table_id = alicloud_vpc.vpc.route_table_id
      local_route_entry_list {
        # The next hop.
        local_next_hop_instance_id = alicloud_vpc_peer_connection.default.id
        # The destination CIDR block.
        local_destination_cidr = alicloud_route_entry.foo.destination_cidrblock
      }
    }
  }
  peer_vpc {
    # The ID of the accepter VPC.
    vpc_id = alicloud_vpc.vpc1.id
    # The region.
    region_no = "cn-heyuan"
    # The route entries.
    peer_vpc_cidr_table_list {
      # The ID of the route table.
      peer_route_table_id = alicloud_vpc.vpc1.route_table_id
      peer_route_entry_list {
        # The destination CIDR block.
        peer_destination_cidr = alicloud_route_entry.foo1.destination_cidrblock
        # The next hop.
        peer_next_hop_instance_id = alicloud_vpc_peer_connection.default.id
      }
    }
  }
  # The status of the resource. Valid values:
  # open: The VPC firewall is enabled after it is created.
  # close: The VPC firewall is disabled after it is created.
  status = "open"
}
output "vpc_id" {
  value = alicloud_vpc.vpc.id
}
output "vpc1_id" {
  value = alicloud_vpc.vpc1.id
}
output "route_table_id_vpc" {
  value = alicloud_vpc.vpc.route_table_id
}
output "route_table_id_vpc1" {
  value = alicloud_vpc.vpc1.route_table_id
}
output "foo_nexthop_id" {
  value = alicloud_vpc_peer_connection.default.id
}
output "foo1_nexthop_id" {
  value = alicloud_vpc_peer_connection.default.id
}
output "cidrblock" {
  value = alicloud_route_entry.foo.destination_cidrblock
}
output "cidrblock1" {
  value = alicloud_route_entry.foo1.destination_cidrblock
}