Add an access control policy to a VPC firewall policy group by using Terraform
This topic describes how to use Terraform to add an access control policy to a VPC firewall policy group.
You can run the sample code in this topic directly in Terraform Explorer.
Prerequisites
An Alibaba Cloud account (root account) has full permissions on all resources. Leaked credentials for this account create a high security risk. For better security, we recommend using a RAM user and creating an AccessKey pair for that user. For more information, see Create a RAM user and Create an AccessKey pair.
Grant permissions to the RAM user based on the following sample code. For more information, see Grant permissions to RAM users.
{ "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.
ImportantEnsure your Terraform version is 0.12.28 or later. To check the version, run the
terraform --versioncommand.
Resources used
alicloud_cloud_firewall_vpc_firewall_control_policy: Adds an access control policy.
Add an access control policy to a VPC firewall policy group
Create a working directory and create a configuration file named
main.tfin the directory. The main.tf file is the main Terraform file that defines the resources to be deployed. The following code creates a VPC firewall instance and adds an access control policy to it.variable "region" { default = "cn-heyuan" } provider "alicloud" { region = var.region } # Get the UID of the current Alibaba Cloud account. data "alicloud_account" "current" { } # Create VPC 1. resource "alicloud_vpc" "vpc" { vpc_name = "dd-tf-vpc-01" cidr_block = "192.168.0.0/16" } # Create VPC 2. resource "alicloud_vpc" "vpc1" { vpc_name = "dd-tf-vpc-02" cidr_block = "172.16.0.0/12" } # Create a vSwitch and allocate the 192.168.10.0/24 CIDR block to the vSwitch. resource "alicloud_vswitch" "vsw" { vpc_id = alicloud_vpc.vpc.id cidr_block = "192.168.10.0/24" zone_id = "cn-heyuan-a" vswitch_name = "dd-tf-vpc-01-example-1" } # Create another vSwitch and allocate the 192.168.20.0/24 CIDR block to the vSwitch. resource "alicloud_vswitch" "vsw1" { vpc_id = alicloud_vpc.vpc.id cidr_block = "192.168.20.0/24" zone_id = "cn-heyuan-b" vswitch_name = "dd-tf-vpc-01-example-2" } # Create a vSwitch and allocate the 172.16.10.0/24 CIDR block to the vSwitch. resource "alicloud_vswitch" "vsw2" { vpc_id = alicloud_vpc.vpc1.id cidr_block = "172.16.10.0/24" zone_id = "cn-heyuan-a" vswitch_name = "dd-tf-vpc-02-example-11" } # Create another vSwitch and allocate the 172.16.20.0/24 CIDR block to the vSwitch. resource "alicloud_vswitch" "vsw3" { vpc_id = alicloud_vpc.vpc1.id cidr_block = "172.16.20.0/24" zone_id = "cn-heyuan-b" vswitch_name = "dd-tf-vpc-02-example-22" } # Create a VPC peering connection. resource "alicloud_vpc_peer_connection" "default" { # The name of the VPC peering connection. peer_connection_name = "terraform-example-vpc-peer-connection" # The ID of the requester VPC. vpc_id = alicloud_vpc.vpc.id # The ID of the Alibaba Cloud account to which the accepter VPC of the VPC peering connection belongs. accepting_ali_uid = data.alicloud_account.current.id # The region ID of the accepter VPC. For same-region peering, enter the same region ID as the requester. For cross-region peering, enter a different region ID. accepting_region_id = "cn-heyuan" # The ID of the accepter VPC. accepting_vpc_id = alicloud_vpc.vpc1.id # Description. description = "terraform-example" # Specifies whether to forcefully delete the VPC peering connection. force_delete = true } # The accepter. resource "alicloud_vpc_peer_connection_accepter" "default" { instance_id = alicloud_vpc_peer_connection.default.id } # Configure a route entry for VPC-A. resource "alicloud_route_entry" "foo" { # The ID of the route table for VPC-A. route_table_id = alicloud_vpc.vpc.route_table_id # The destination CIDR block. You can customize this value. 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-B. resource "alicloud_route_entry" "foo1" { # The ID of the route table for VPC-B. route_table_id = alicloud_vpc.vpc1.route_table_id # The destination CIDR block. You can customize this value. 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 } # Create the prerequisite resources first. resource "time_sleep" "wait_before_firewall" { # Make sure that the network instances are created. depends_on = [ alicloud_route_entry.foo, alicloud_route_entry.foo1 ] create_duration = "720s" # Set the duration based on your needs. } # Delay. resource "null_resource" "wait_for_firewall" { provisioner "local-exec" { command = "echo waiting for firewall to be ready" } # Make sure that the prerequisite resources are created. depends_on = [time_sleep.wait_before_firewall] } # A VPC firewall instance for a VPC peering connection. resource "alicloud_cloud_firewall_vpc_firewall" "default" { # Prerequisites. depends_on = [ null_resource.wait_for_firewall ] timeouts { create = "30m" # Set a creation timeout. } # Instance name. vpc_firewall_name = "tf-test" # The UID of the user. member_uid = data.alicloud_account.current.id local_vpc { # The ID of the requester VPC. vpc_id = alicloud_vpc.vpc.id # Region. region_no = "cn-heyuan" # Route entry. local_vpc_cidr_table_list { # Route table ID. local_route_table_id = alicloud_vpc.vpc.route_table_id local_route_entry_list { # 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 # Region. region_no = "cn-heyuan" # Route entry. peer_vpc_cidr_table_list { # Route table ID. 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 # Next hop. peer_next_hop_instance_id = alicloud_vpc_peer_connection.default.id } } } # The status of the resource. Valid values: # open: Protection is automatically enabled after the VPC firewall is created. # close: Protection is not automatically enabled after the VPC firewall is created. status = "open" }resource "alicloud_cloud_firewall_vpc_firewall_control_policy" "default" { # The priority of the access control policy. The value starts from 1. A smaller value indicates a higher priority. order = "1" # The destination address in the access control policy. destination = "0.0.0.0/0" # The application type supported by the access control policy. application_name = "ANY" # The description of the access control policy for the VPC firewall. description = "Created_by_Terraform" # The type of the source address in the access control policy. Valid values: net and group. source_type = "net" # (Optional) The destination port in the access control policy. dest_port = "80/88" # The action that Cloud Firewall performs on matching traffic. Valid values: accept, drop, and log. acl_action = "accept" # The language of the request and response content. Valid values: zh and en. lang = "zh" # If you set destination_type to net, the value of destination must be a CIDR block. destination_type = "net" # The source address in the access control policy for the VPC firewall. source = "0.0.0.0/0" # The type of the destination port in the access control policy. Valid values: port and group. dest_port_type = "port" # The protocol type in the access control policy. Valid values: ANY, TCP, UDP, and ICMP. proto = "TCP" # The enabled status of the access control policy. By default, the policy is enabled after it is created. release = true # The UID of the current Alibaba Cloud account. member_uid = data.alicloud_account.current.id # The ID of the VPC firewall instance. vpc_firewall_id = alicloud_cloud_firewall_vpc_firewall.default.id }TerraformRun the following command to initialize the Terraform runtime environment:terraform initThe following command output indicates that Terraform is initialized.
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/alicloud... - Using hashicorp/alicloud v1.231.0 from the shared cache directory Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. ╷ │ Warning: Additional provider information from registry │ │ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud: │ - For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers. ╵ ╷ │ Warning: Incomplete lock file information for providers │ │ Due to your customized provider installation methods, Terraform was forced to calculate lock file checksums locally for the following providers: │ - hashicorp/alicloud │ │ The current .terraform.lock.hcl file only includes checksums for linux_amd64, so Terraform running on another platform will fail to install these providers. │ │ To calculate additional checksums for another platform, run: │ terraform providers lock -platform=linux_amd64 │ (where linux_amd64 is the platform to generate) ╵ Terraform has been successfully initialized!Create an execution plan and preview the changes.
terraform planRun the following command to add the access control policy to the VPC firewall:
terraform applyWhen prompted, enter
yesand press Enter. Wait until the command completes. The following output indicates that the access control policy was successfully added to the VPC firewall.Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # alicloud_cloud_firewall_vpc_firewall_control_policy.default will be created + resource "alicloud_cloud_firewall_vpc_firewall_control_policy" "default" { + acl_action = "accept" + acl_uuid = (known after apply) + application_id = (known after apply) + application_name = "ANY" + description = "Created_by_Terraform" + dest_port = "80/88" + dest_port_group_ports = (known after apply) + dest_port_type = "port" + destination = "0.0.0.0/0" + destination_group_cidrs = (known after apply) + destination_group_type = (known after apply) + destination_type = "net" + hit_times = (known after apply) + id = (known after apply) + lang = "zh" + member_uid = "1413397765616***" + order = 1 + proto = "TCP" + release = true + source = "0.0.0.0/0" + source_group_cidrs = (known after apply) + source_group_type = (known after apply) + source_type = "net" + vpc_firewall_id = "vfw-c7536567ab694fb1a***" } Plan: 1 to add, 0 to change, 0 to destroy. 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_cloud_firewall_vpc_firewall_control_policy.default: Creating... alicloud_cloud_firewall_vpc_firewall_control_policy.default: Creation complete after 0s [id=vfw-c7536567ab694fb1a59f:ca14e184-15dc-4a68-b0d8-fb71a15ff***] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.Verify the result
Command line
Run the following command to view the details of the resources created by Terraform:
terraform show# alicloud_cloud_firewall_vpc_firewall_control_policy.default: resource "alicloud_cloud_firewall_vpc_firewall_control_policy" "default" { acl_action = "accept" acl_uuid = "ba164e52-acd2-4899-bf72-6816b13a****" application_id = "0" application_name = "ANY" description = "Created_by_Terraform" dest_port = "80/88" dest_port_group_ports = [] dest_port_type = "port" destination = "0.X.X.0/0" destination_group_cidrs = [] destination_type = "net" hit_times = 0 id = "vfw-d7b8ce273791475b****:ba164e52-acd2-4899-bf72-6816b13a****" lang = "zh" member_uid = "1415189284827****" order = 1 proto = "TCP" release = true source = "0.X.X.0/0" source_group_cidrs = [] source_type = "net" vpc_firewall_id = "vfw-d7b8ce273791475b****" }Console
Log on to the Cloud Firewall console. On the Access Control > VPC Border page, view the details of the access control policy for the VPC firewall.

Release resources
When the resources that Terraform created are no longer needed, run the following command to release them. For more information about the terraform destroy command, see Common commands.
terraform destroyComplete example
You can run the sample code in this topic directly in Terraform Explorer.