创建NAT防火墙实例

本文介绍如何使用Terraform创建NAT防火墙实例。

说明

当前示例代码支持一键运行,您可以直接运行代码。一键运行

前提条件

  • 由于阿里云账号(主账号)具有资源的所有权限,一旦发生泄露将面临重大风险。建议您使用RAM用户,并为该RAM用户创建AccessKey,具体操作方式请参见创建RAM用户创建AccessKey

  • 使用以下示例为RAM用户授权,需要为该RAM用户授予以下权限:云防火墙CloudFirewall权限和AliyunBSSFullAccess(管理费用中心BSS)权限。具体操作方式请参见RAM用户授权

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "yundun-cloudfirewall:CreateSecurityProxy",
                    "yundun-cloudfirewall:DescribeNatFirewallList",
                    "yundun-cloudfirewall:DeleteSecurityProxy"
                ],
                "Resource": "*"
            },
            {
                "Effect": "Allow",
                "Action": [
                    "bssapi:*",
                    "bss:*"
                ],
                "Resource": "*"
            }
        ]
    }
  • 准备Terraform运行环境,您可以选择以下任一方式来使用Terraform。

    在 Terraform Explorer 中使用 Terraform中使用Terraform:阿里云提供了Terraform的在线运行环境,您无需安装Terraform,登录后即可在线使用和体验Terraform。适用于零成本、快速、便捷地体验和调试Terraform的场景。

    Cloud Shell中使用Terraform:阿里云Cloud Shell中预装了Terraform的组件,并已配置好身份凭证,您可直接在Cloud Shell中运行Terraform的命令。适用于低成本、快速、便捷地访问和使用Terraform的场景。

    在本地安装和配置Terraform:适用于网络连接较差或需要自定义开发环境的场景。

    重要

    请确保Terraform版本不低于v0.12.28。如需检查现有版本,请运行terraform --version命令。

使用的资源

alicloud_cloud_firewall_nat_firewall:NAT防火墙。

创建NAT防火墙

操作步骤

本示例将创建一个NAT边界防火墙。

  1. 创建一个工作目录,并且在工作目录中创建以下名为main.tf的配置文件。main.tfTerraform主文件,定义了将要部署的资源。

    variable "name" {
      default = "terraform-example"
    }
    variable "region" {
      default = "cn-heyuan"
    }
    provider "alicloud" {
      region = var.region
    }
    # 创建VPC 
    resource "alicloud_vpc" "vpc" {
      vpc_name   = "dd-tf-vpc-01"
      cidr_block = "192.168.0.0/16"
    }
    # 创建一个Vswitch CIDR 块为 192.168.10.0/24
    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"
    }
    resource "alicloud_nat_gateway" "default2iRZpC" {
      eip_bind_mode    = "MULTI_BINDED"
      vpc_id           = alicloud_vpc.vpc.id
      nat_gateway_name = var.name
      payment_type     = "PayAsYouGo"
      vswitch_id       = alicloud_vswitch.vsw.id
      nat_type         = "Enhanced"
      network_type     = "internet"
    }
    resource "alicloud_eip_address" "defaultyiRwgs" {
      address_name = var.name
    }
    resource "alicloud_eip_association" "defaults2MTuO" {
      instance_id   = alicloud_nat_gateway.default2iRZpC.id
      allocation_id = alicloud_eip_address.defaultyiRwgs.id
      mode          = "NAT"
      instance_type = "Nat"
    }
    
    resource "alicloud_snat_entry" "defaultAKE43g" {
      snat_ip           = alicloud_eip_address.defaultyiRwgs.ip_address
      snat_table_id     = alicloud_nat_gateway.default2iRZpC.snat_table_ids
      source_vswitch_id = alicloud_vswitch.vsw.id
    }
    
    resource "alicloud_cloud_firewall_nat_firewall" "default" {
      nat_gateway_id = alicloud_nat_gateway.default2iRZpC.id
      nat_route_entry_list {
        nexthop_type     = "NatGateway"
        route_table_id   = alicloud_vpc.vpc.route_table_id
        nexthop_id       = alicloud_nat_gateway.default2iRZpC.id
        destination_cidr = "0.0.0.0/0"
      }
      firewall_switch = "close"
      vswitch_auto    = "true"
      status          = "closed"
      region_no       = "cn-heyuan"
      lang            = "zh"
      proxy_name      = "CFW-example"
      vswitch_id      = alicloud_snat_entry.defaultAKE43g.source_vswitch_id
      strict_mode     = "0"
      vpc_id          = alicloud_vpc.vpc.id
      vswitch_cidr    = "192.168.20.0/24"
    }
  2. 执行以下命令,初始化Terraform运行环境。

    terraform init

    返回如下信息,表示Terraform初始化成功。

    Initializing the backend...
    
    Initializing provider plugins...
    - Reusing previous version of hashicorp/alicloud from the dependency lock file
    - Using previously-installed hashicorp/alicloud v1.233.1
    
    ╷
    │ 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.
    ╵
    
    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. 创建执行计划,并预览变更。

    terraform plan
  4. 执行以下命令,创建NAT防火墙 。

    terraform apply

    在执行过程中,根据提示输入yes并按下Enter键,等待命令执行完成,若出现以下信息,则表示NAT防火墙创建成功。

    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_eip_address.defaultyiRwgs: Creating...
    alicloud_vpc.vpc: Creating...
    alicloud_vpc.vpc: Creation complete after 6s [id=vpc-f8z5jo13wgl5r7hvaune1]
    alicloud_vswitch.vsw: Creating...
    alicloud_eip_address.defaultyiRwgs: Creation complete after 8s [id=eip-f8zl1g2y5l3dntqquisq2]
    alicloud_vswitch.vsw: Creation complete after 4s [id=vsw-f8z2brmg92u7q0n6z8tj4]
    alicloud_nat_gateway.default2iRZpC: Creating...
    alicloud_nat_gateway.default2iRZpC: Still creating... [10s elapsed]
    alicloud_nat_gateway.default2iRZpC: Still creating... [20s elapsed]
    alicloud_nat_gateway.default2iRZpC: Still creating... [30s elapsed]
    alicloud_nat_gateway.default2iRZpC: Creation complete after 36s [id=ngw-f8z11qox17rucwjb19z9q]
    alicloud_snat_entry.defaultAKE43g: Creating...
    alicloud_eip_association.defaults2MTuO: Creating...
    alicloud_eip_association.defaults2MTuO: Creation complete after 1s [id=eip-f8zl1g2y5l3dntqquisq2:ngw-f8z11qox17rucwjb19z9q]
    alicloud_snat_entry.defaultAKE43g: Still creating... [10s elapsed]
    alicloud_snat_entry.defaultAKE43g: Creation complete after 13s [id=stb-f8z0medq9q9m76xzz6zf6:snat-f8zw224vtt4ke9kzinpjq]
    alicloud_cloud_firewall_nat_firewall.default: Creating...
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [10s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [20s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [30s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [40s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [50s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [1m0s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [1m10s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [1m20s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [1m30s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [1m40s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [1m50s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [2m0s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [2m10s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [2m20s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [2m30s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [2m40s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [2m50s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [3m0s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [3m10s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [3m20s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [3m30s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [3m40s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [3m50s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [4m0s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [4m10s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [4m20s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [4m30s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [4m40s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [4m50s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [5m0s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [5m10s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Still creating... [5m20s elapsed]
    alicloud_cloud_firewall_nat_firewall.default: Creation complete after 5m21s [id=proxy-natf0200977a7dd4bad910c]
    
    Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
  5. 验证结果

    执行terraform show命令

    您可以使用以下命令查询Terraform已创建的资源详细信息。

    terraform show

    ce7063de33d512f0828a52ddde422ff6

    登录云防火墙控制台

    image

清理资源

当您不再需要上述通过Terraform创建或管理的资源时,请运行以下命令以释放资源。关于terraform destroy的更多信息,请参见Terraform常用命令

terraform destroy

完整示例

示例代码

variable "name" {
  default = "terraform-example"
}
variable "region" {
  default = "cn-heyuan"
}
provider "alicloud" {
  region = var.region
}
# 创建VPC 
resource "alicloud_vpc" "vpc" {
  vpc_name   = "dd-tf-vpc-01"
  cidr_block = "192.168.0.0/16"
}
# 创建一个Vswitch CIDR 块为 192.168.10.0/24
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"
}
resource "alicloud_nat_gateway" "default2iRZpC" {
  eip_bind_mode    = "MULTI_BINDED"
  vpc_id           = alicloud_vpc.vpc.id
  nat_gateway_name = var.name
  payment_type     = "PayAsYouGo"
  vswitch_id       = alicloud_vswitch.vsw.id
  nat_type         = "Enhanced"
  network_type     = "internet"
}
resource "alicloud_eip_address" "defaultyiRwgs" {
  address_name = var.name
}
resource "alicloud_eip_association" "defaults2MTuO" {
  instance_id   = alicloud_nat_gateway.default2iRZpC.id
  allocation_id = alicloud_eip_address.defaultyiRwgs.id
  mode          = "NAT"
  instance_type = "Nat"
}

resource "alicloud_snat_entry" "defaultAKE43g" {
  snat_ip           = alicloud_eip_address.defaultyiRwgs.ip_address
  snat_table_id     = alicloud_nat_gateway.default2iRZpC.snat_table_ids
  source_vswitch_id = alicloud_vswitch.vsw.id
}

resource "alicloud_cloud_firewall_nat_firewall" "default" {
  nat_gateway_id = alicloud_nat_gateway.default2iRZpC.id
  nat_route_entry_list {
    nexthop_type     = "NatGateway"
    route_table_id   = alicloud_vpc.vpc.route_table_id
    nexthop_id       = alicloud_nat_gateway.default2iRZpC.id
    destination_cidr = "0.0.0.0/0"
  }
  firewall_switch = "close"
  vswitch_auto    = "true"
  status          = "closed"
  region_no       = "cn-heyuan"
  lang            = "zh"
  proxy_name      = "CFW-example"
  vswitch_id      = alicloud_snat_entry.defaultAKE43g.source_vswitch_id
  strict_mode     = "0"
  vpc_id          = alicloud_vpc.vpc.id
  vswitch_cidr    = "192.168.20.0/24"
}