Typical Practice of Managing IPsec VPN Gateway Using Terraform

VPC VPC

  • API Reference
    • ACL-Related Interfaces
      • Add ACL rule
      • Delete ACL rules
      • Query ACL rules
      • Query ACL
      • Update ACL rules
    • API function release records
    • API Overview
    • Appendix
    • Common Headers and Error Responses
    • Dedicated Gateway-Related Interfaces
      • Bind physical dedicated line
      • Create dedicated gateway
      • Create health check for dedicated gateway
      • Query dedicated gateway details
      • Query dedicated gateway list
      • Release dedicated gateway
      • Unbind physical dedicated line
      • Update dedicated gateway
    • Elastic Network Interface-Related Interfaces
      • Add intranet IP to elastic network interface in batches
      • Add Secondary IP Address of Elastic Network Interface
      • Create elastic network interface
      • Delete elastic network interface
      • Delete Intranet IP from elastic network interface in batches
      • Delete Secondary IP Address of Elastic Network Interface
      • Elastic network interface binds to EIP
      • Elastic Network Interface Mounts Cloud Product Instance
      • Elastic network interface unbinds from EIP
      • Elastic Network Interface Unmounts Cloud Product Instance
      • Query elastic network interface list
      • Query elastic network interface status
      • Query the specified elastic network interface
      • Update elastic network interface with enterprise security group
      • Update elastic network interface with regular security group
      • Update elastic network interface
    • Gateway Bandwidth Limiting Rule Related Interfaces
      • Create gateway bandwidth limiting rules
      • Delete Gateway Bandwidth Limiting Rule
      • Modify Gateway Bandwidth Limiting Rule
      • View Gateway Bandwidth Limiting Rule
    • General Description
    • High-Availability Virtual IP Address(HAVIP) Related Interfaces
      • Bind EIP to high-availability virtual IP address
      • Bind instance to high-availability virtual IP address
      • Create high-availability virtual IP address
      • Delete high-availability virtual IP address
      • Query high-availability virtual IP address list
      • Query the specified high-availability virtual IP address
      • Unbind EIP from high-availability virtual IP address
      • Unbind instance from high-availability virtual IP address
      • Update high-availability virtual IP address
    • Interface Overview
    • IPv6 Gateway-Related Interfaces
      • Add IPv6 egress-only policy
      • Bandwidth resizing for IPv6 gateway
      • Create IPv6 gateway bandwidth limiting policy
      • Create IPv6 gateway
      • Delete IPv6 gateway bandwidth limiting policy
      • Delete IPv6 Gateway
      • Delete the IPv6 egress-only policy
      • Query IPv6 gateway bandwidth limiting policy list
      • Query IPv6 gateway
      • Query the IPv6 egress-only policy list
      • Update IPv6 gateway bandwidth limiting policy
      • Update the IPv6 gateway release protection switch
    • NAT Gateway-Related Interfaces
      • Bind DNAT EIP to the NAT gateway
      • Bind SNAT EIP to the NAT gateway
      • Create DNAT rules in batches
      • Create DNAT rules
      • Create NAT gateway
      • Create SNAT rules in batches
      • Create SNAT rules
      • Delete DNAT rules
      • Delete SNAT rules
      • NAT Gateway Binds EIP
      • NAT gateway renewal
      • NAT Gateway Resize
      • NAT Gateway Unbinds EIP
      • Query DNAT rules
      • Query NAT gateway details
      • Query NAT gateway list
      • Query SNAT rules
      • Release NAT gateway
      • Unbind DNAT EIP from the NAT gateway
      • Unbind SNAT EIP from the NAT gateway
      • Update DNAT rules
      • Update NAT Delete Protection Switch
      • Update NAT gateway name
      • Update SNAT rules
    • Network Detection Related Interfaces
      • Create Network Probe
      • Delete network probe
      • Query network probe details
      • Query network probe list.
      • Update probe
    • Parameter Template-Related Interfaces
      • Add IP addresses to the IP address family
      • Add IP addresses to the IP address group
      • Create IP address family
      • Create IP address set
      • Delete IP address family
      • Delete IP address from IP address group
      • Delete IP address set
      • Query IP address family list
      • Query IP address group list
      • Query specified IP address family
      • Query specified IP address group
      • Remove IP address group from IP address family
      • Update IP address family
      • Update IP address set
    • Peering Connections-Related Interfaces
      • Application to process the peering connection
      • Bandwidth resizing for peering connections
      • Create peering connections
      • Disable DNS synchronization for the peering connections.
      • Enable DNS synchronization for the peering connections
      • List of peering connections to be queried.
      • Peering connection renewal
      • Release peering connections
      • Update the local API name and remarks for peering connections
      • Update the peering connection release protection switch
      • View details of peering connection
    • Route Table Related Interfaces
      • Create route rules
      • Delete route rules
      • Primary-standby switch
      • Query route table
      • Query routing rules
      • Update route rules
    • Security Group Related Interfaces
      • Enterprise security group
        • Authorize enterprise security group rules
        • Create enterprise security group
        • Delete enterprise security group rules
        • Delete enterprise security group
        • Query enterprise security group list
        • Update enterprise security group rules
      • Regular security group
        • Authorize regular security group rules
        • Create regular security group
        • Delete regular security group rules
        • Delete regular security group
        • Query regular security group list
        • Revoke regular security group rules
        • Update regular security group rules
        • View security group details
    • Service domain
    • Service Network Interface Card (SNIC)-Related Interfaces
      • Create service network interface card
      • Delete service network interface card
      • Query mountable public services
      • Query service network interface card details
      • Query service network interface card list
      • Update regular security group for service network interface card
      • Update service network interface card with enterprise security group
      • Update the service network interface card
    • Subnet-Related Interfaces
      • Create reserved network segment
      • Create subnet
      • Delete reserved network segment
      • Delete subnet
      • Query specified subnet
      • Query subnet list
      • Query the reserved network segment list
      • Update subnet
    • VPC-Related Interfaces
      • Create VPC
      • Delete VPC
      • Open VPC relay
      • Query IP Usage by Products in VPC
      • Query specified VPC
      • Query VPC intranet IP
      • Query VPC list
      • Shut down VPC relay
      • Update VPC
    • VPN-Related Interfaces
      • Bind EIP
      • Create SSL VPN server
      • Create SSL VPN users in batches
      • Create VPN tunnel
      • Create VPN
      • Delete SSL VPN server
      • Delete SSL VPN user
      • Delete VPN tunnel
      • Query SSL-VPN server
      • Query SSL-VPN user
      • Query VPN details
      • Query VPN List
      • Query VPN tunnels
      • Release VPN
      • Unbind EIP
      • Update SSL VPN server
      • Update SSL VPN user
      • Update VPN release protection switch
      • Update VPN tunnel
      • Update VPN
      • VPN gateway renewal
    • High-Availability Virtual IP Address (HAVIP) Related Interfaces
      • Bind EIP to high-availability virtual IP address
      • Bind instance to high-availability virtual IP address
      • Create high-availability virtual IP address
      • Delete high-availability virtual IP address
      • Query high-availability virtual IP address list
      • Query the specified high-availability virtual IP address
      • Unbind EIP from high-availability virtual IP address
      • Unbind instance from high-availability virtual IP address
      • Update high-availability virtual IP address
  • FAQs
    • Common Questions Overview
    • NAT FAQs
    • Route Table FAQs
    • Service Network Interface Card(SNIC) Common Questions
    • VPC FAQs
    • VPN FAQs
    • Service Network Interface Card (SNIC) Common Questions
  • Function Release Records
  • Operation guide
    • Access control
      • ACL
      • Parameter Template
      • Security group
    • Identity and access management
    • Monitor and Operations
      • NAT Gateway Instance Diagnosis
    • Network Connection
      • Dedicated gateway
      • IPv6 gateway
      • Layer 2 Gateway
      • NAT Gateway
        • Private Network NAT(Network Address Translation) Gateway
        • Public Network NAT(Network Address Translation) Gateway
        • Private Network NAT (Network Address Translation) Gateway
        • Public Network NAT (Network Address Translation) Gateway
      • Peering Connections
      • VPN Gateway
        • GRE VPN Gateway
        • IPsec VPN Gateway
        • SSL VPN Gateway
    • Network Diagnostics
      • Flow log
      • Gateway bandwidth limiting
      • Network probe
      • Path analysis
      • Port verification
      • Traffic monitor
    • Network interface card
      • Elastic network interface
      • High-availability virtual IP address (HAVIP)
      • Service network interface card
    • Network topology
    • Route table
    • Subnet
    • Tag Management
    • Using IPv6
    • VPC
  • Product Description
    • Application scenarios
    • Product advantages
    • Product features
    • Product Introduction
    • Related concepts
    • Usage restrictions
  • Product pricing
  • SDK
    • Go-SDK
      • ACL
      • Dedicated gateway
      • Elastic network interface
      • Exception handling
      • High-availability virtual IP address (HAVIP)
      • Initialization
      • Install the SDK Package
      • IPv6Gateway
      • NAT
      • Network probe
      • Overview
      • Peering Connections
      • Route
      • Security group
        • Enterprise security group
        • Regular security group
      • Service network interface card
      • Subnet
      • VPC
      • VPN
    • Java-SDK
      • ACL
      • Dedicated gateway
      • Elastic network interface
      • Enterprise security group
      • High-availability virtual IP address (HAVIP)
      • Install the SDK Package
      • IPv6 gateway
      • NAT
      • Network probe
      • Overview
      • Parameter Template
      • Peering Connections
      • Regular security group
      • Route
      • Service network interface card
      • Subnet
      • Version history
      • vpc
      • VPN
    • PHP-SDK
      • ACL
      • Install the SDK Package
      • NAT
      • Overview
      • Peering Connections
      • Route
      • Security group
      • Subnet
      • Version history
      • vpc
    • Python-SDK
      • ACL
      • Dedicated gateway
      • Elastic network interface
      • Enterprise security group
      • High-availability virtual IP address (HAVIP)
      • Install the SDK Package
      • IPv6Gateway
      • NAT
      • Network probe
      • Overview
      • Peering Connections
      • Route
      • Security group
      • Service network interface card
      • Subnet
      • Version history
      • VPC
      • VPN
  • Service Level Agreement (SLA)
    • IPv6 Gateway Service Level Agreement SLA
    • NAT(Network Address Translation) Gateway Service Level Agreement SLA
    • Peering Connections Service Level Agreement SLA
    • Service Network Interface Card(SNIC) Service Level Agreement SLA
    • VPN Gateway Service Level Agreement SLA
    • NAT (Network Address Translation) Gateway Service Level Agreement SLA
    • Service Network Interface Card (SNIC) Service Level Agreement SLA
  • Typical Practices
    • Configure Custom Service and Use Service Network Interface Card(SNIC) to Provide Service for Other VPCs
    • HAVIP Combined with Keepalived to Achieve Master-Backup Multi-Machine High Availability
    • Layer 2 Gateway Combined with Dedicated Line Access ET to Build Large Layer 2 Network Between IDC and Cloud VPC
    • Network Layer Security Description
    • Peering Connections Typical Practice
    • Security Group Configuration Practice (Advanced Level)
    • Security Group Configuration Practice (Beginner Level)
    • Typical Practice of Managing Elastic Network Interface Using Terraform
    • Typical Practice of Managing IPsec VPN Gateway Using Terraform
    • Using Keepalived in VPC to Achieve High-Availability Architecture
    • VPC Custom Route Table to Achieve Secure Traffic Mutual Access
    • Windows Using HAVIP to Configure Master-Backup Multi-Machine
    • Configure Custom Service and Use Service Network Interface Card (SNIC) to Provide Service for Other VPCs
  • VPC CLI
    • Configure BCE-CLI
    • Install BCE-CLI
    • Route Table Related Operations
    • Subnet Related Operations
    • Using VPC Service via CLI
    • Version Change Records
    • VPC Related Operations
All documents
menu
No results found, please re-enter

VPC VPC

  • API Reference
    • ACL-Related Interfaces
      • Add ACL rule
      • Delete ACL rules
      • Query ACL rules
      • Query ACL
      • Update ACL rules
    • API function release records
    • API Overview
    • Appendix
    • Common Headers and Error Responses
    • Dedicated Gateway-Related Interfaces
      • Bind physical dedicated line
      • Create dedicated gateway
      • Create health check for dedicated gateway
      • Query dedicated gateway details
      • Query dedicated gateway list
      • Release dedicated gateway
      • Unbind physical dedicated line
      • Update dedicated gateway
    • Elastic Network Interface-Related Interfaces
      • Add intranet IP to elastic network interface in batches
      • Add Secondary IP Address of Elastic Network Interface
      • Create elastic network interface
      • Delete elastic network interface
      • Delete Intranet IP from elastic network interface in batches
      • Delete Secondary IP Address of Elastic Network Interface
      • Elastic network interface binds to EIP
      • Elastic Network Interface Mounts Cloud Product Instance
      • Elastic network interface unbinds from EIP
      • Elastic Network Interface Unmounts Cloud Product Instance
      • Query elastic network interface list
      • Query elastic network interface status
      • Query the specified elastic network interface
      • Update elastic network interface with enterprise security group
      • Update elastic network interface with regular security group
      • Update elastic network interface
    • Gateway Bandwidth Limiting Rule Related Interfaces
      • Create gateway bandwidth limiting rules
      • Delete Gateway Bandwidth Limiting Rule
      • Modify Gateway Bandwidth Limiting Rule
      • View Gateway Bandwidth Limiting Rule
    • General Description
    • High-Availability Virtual IP Address(HAVIP) Related Interfaces
      • Bind EIP to high-availability virtual IP address
      • Bind instance to high-availability virtual IP address
      • Create high-availability virtual IP address
      • Delete high-availability virtual IP address
      • Query high-availability virtual IP address list
      • Query the specified high-availability virtual IP address
      • Unbind EIP from high-availability virtual IP address
      • Unbind instance from high-availability virtual IP address
      • Update high-availability virtual IP address
    • Interface Overview
    • IPv6 Gateway-Related Interfaces
      • Add IPv6 egress-only policy
      • Bandwidth resizing for IPv6 gateway
      • Create IPv6 gateway bandwidth limiting policy
      • Create IPv6 gateway
      • Delete IPv6 gateway bandwidth limiting policy
      • Delete IPv6 Gateway
      • Delete the IPv6 egress-only policy
      • Query IPv6 gateway bandwidth limiting policy list
      • Query IPv6 gateway
      • Query the IPv6 egress-only policy list
      • Update IPv6 gateway bandwidth limiting policy
      • Update the IPv6 gateway release protection switch
    • NAT Gateway-Related Interfaces
      • Bind DNAT EIP to the NAT gateway
      • Bind SNAT EIP to the NAT gateway
      • Create DNAT rules in batches
      • Create DNAT rules
      • Create NAT gateway
      • Create SNAT rules in batches
      • Create SNAT rules
      • Delete DNAT rules
      • Delete SNAT rules
      • NAT Gateway Binds EIP
      • NAT gateway renewal
      • NAT Gateway Resize
      • NAT Gateway Unbinds EIP
      • Query DNAT rules
      • Query NAT gateway details
      • Query NAT gateway list
      • Query SNAT rules
      • Release NAT gateway
      • Unbind DNAT EIP from the NAT gateway
      • Unbind SNAT EIP from the NAT gateway
      • Update DNAT rules
      • Update NAT Delete Protection Switch
      • Update NAT gateway name
      • Update SNAT rules
    • Network Detection Related Interfaces
      • Create Network Probe
      • Delete network probe
      • Query network probe details
      • Query network probe list.
      • Update probe
    • Parameter Template-Related Interfaces
      • Add IP addresses to the IP address family
      • Add IP addresses to the IP address group
      • Create IP address family
      • Create IP address set
      • Delete IP address family
      • Delete IP address from IP address group
      • Delete IP address set
      • Query IP address family list
      • Query IP address group list
      • Query specified IP address family
      • Query specified IP address group
      • Remove IP address group from IP address family
      • Update IP address family
      • Update IP address set
    • Peering Connections-Related Interfaces
      • Application to process the peering connection
      • Bandwidth resizing for peering connections
      • Create peering connections
      • Disable DNS synchronization for the peering connections.
      • Enable DNS synchronization for the peering connections
      • List of peering connections to be queried.
      • Peering connection renewal
      • Release peering connections
      • Update the local API name and remarks for peering connections
      • Update the peering connection release protection switch
      • View details of peering connection
    • Route Table Related Interfaces
      • Create route rules
      • Delete route rules
      • Primary-standby switch
      • Query route table
      • Query routing rules
      • Update route rules
    • Security Group Related Interfaces
      • Enterprise security group
        • Authorize enterprise security group rules
        • Create enterprise security group
        • Delete enterprise security group rules
        • Delete enterprise security group
        • Query enterprise security group list
        • Update enterprise security group rules
      • Regular security group
        • Authorize regular security group rules
        • Create regular security group
        • Delete regular security group rules
        • Delete regular security group
        • Query regular security group list
        • Revoke regular security group rules
        • Update regular security group rules
        • View security group details
    • Service domain
    • Service Network Interface Card (SNIC)-Related Interfaces
      • Create service network interface card
      • Delete service network interface card
      • Query mountable public services
      • Query service network interface card details
      • Query service network interface card list
      • Update regular security group for service network interface card
      • Update service network interface card with enterprise security group
      • Update the service network interface card
    • Subnet-Related Interfaces
      • Create reserved network segment
      • Create subnet
      • Delete reserved network segment
      • Delete subnet
      • Query specified subnet
      • Query subnet list
      • Query the reserved network segment list
      • Update subnet
    • VPC-Related Interfaces
      • Create VPC
      • Delete VPC
      • Open VPC relay
      • Query IP Usage by Products in VPC
      • Query specified VPC
      • Query VPC intranet IP
      • Query VPC list
      • Shut down VPC relay
      • Update VPC
    • VPN-Related Interfaces
      • Bind EIP
      • Create SSL VPN server
      • Create SSL VPN users in batches
      • Create VPN tunnel
      • Create VPN
      • Delete SSL VPN server
      • Delete SSL VPN user
      • Delete VPN tunnel
      • Query SSL-VPN server
      • Query SSL-VPN user
      • Query VPN details
      • Query VPN List
      • Query VPN tunnels
      • Release VPN
      • Unbind EIP
      • Update SSL VPN server
      • Update SSL VPN user
      • Update VPN release protection switch
      • Update VPN tunnel
      • Update VPN
      • VPN gateway renewal
    • High-Availability Virtual IP Address (HAVIP) Related Interfaces
      • Bind EIP to high-availability virtual IP address
      • Bind instance to high-availability virtual IP address
      • Create high-availability virtual IP address
      • Delete high-availability virtual IP address
      • Query high-availability virtual IP address list
      • Query the specified high-availability virtual IP address
      • Unbind EIP from high-availability virtual IP address
      • Unbind instance from high-availability virtual IP address
      • Update high-availability virtual IP address
  • FAQs
    • Common Questions Overview
    • NAT FAQs
    • Route Table FAQs
    • Service Network Interface Card(SNIC) Common Questions
    • VPC FAQs
    • VPN FAQs
    • Service Network Interface Card (SNIC) Common Questions
  • Function Release Records
  • Operation guide
    • Access control
      • ACL
      • Parameter Template
      • Security group
    • Identity and access management
    • Monitor and Operations
      • NAT Gateway Instance Diagnosis
    • Network Connection
      • Dedicated gateway
      • IPv6 gateway
      • Layer 2 Gateway
      • NAT Gateway
        • Private Network NAT(Network Address Translation) Gateway
        • Public Network NAT(Network Address Translation) Gateway
        • Private Network NAT (Network Address Translation) Gateway
        • Public Network NAT (Network Address Translation) Gateway
      • Peering Connections
      • VPN Gateway
        • GRE VPN Gateway
        • IPsec VPN Gateway
        • SSL VPN Gateway
    • Network Diagnostics
      • Flow log
      • Gateway bandwidth limiting
      • Network probe
      • Path analysis
      • Port verification
      • Traffic monitor
    • Network interface card
      • Elastic network interface
      • High-availability virtual IP address (HAVIP)
      • Service network interface card
    • Network topology
    • Route table
    • Subnet
    • Tag Management
    • Using IPv6
    • VPC
  • Product Description
    • Application scenarios
    • Product advantages
    • Product features
    • Product Introduction
    • Related concepts
    • Usage restrictions
  • Product pricing
  • SDK
    • Go-SDK
      • ACL
      • Dedicated gateway
      • Elastic network interface
      • Exception handling
      • High-availability virtual IP address (HAVIP)
      • Initialization
      • Install the SDK Package
      • IPv6Gateway
      • NAT
      • Network probe
      • Overview
      • Peering Connections
      • Route
      • Security group
        • Enterprise security group
        • Regular security group
      • Service network interface card
      • Subnet
      • VPC
      • VPN
    • Java-SDK
      • ACL
      • Dedicated gateway
      • Elastic network interface
      • Enterprise security group
      • High-availability virtual IP address (HAVIP)
      • Install the SDK Package
      • IPv6 gateway
      • NAT
      • Network probe
      • Overview
      • Parameter Template
      • Peering Connections
      • Regular security group
      • Route
      • Service network interface card
      • Subnet
      • Version history
      • vpc
      • VPN
    • PHP-SDK
      • ACL
      • Install the SDK Package
      • NAT
      • Overview
      • Peering Connections
      • Route
      • Security group
      • Subnet
      • Version history
      • vpc
    • Python-SDK
      • ACL
      • Dedicated gateway
      • Elastic network interface
      • Enterprise security group
      • High-availability virtual IP address (HAVIP)
      • Install the SDK Package
      • IPv6Gateway
      • NAT
      • Network probe
      • Overview
      • Peering Connections
      • Route
      • Security group
      • Service network interface card
      • Subnet
      • Version history
      • VPC
      • VPN
  • Service Level Agreement (SLA)
    • IPv6 Gateway Service Level Agreement SLA
    • NAT(Network Address Translation) Gateway Service Level Agreement SLA
    • Peering Connections Service Level Agreement SLA
    • Service Network Interface Card(SNIC) Service Level Agreement SLA
    • VPN Gateway Service Level Agreement SLA
    • NAT (Network Address Translation) Gateway Service Level Agreement SLA
    • Service Network Interface Card (SNIC) Service Level Agreement SLA
  • Typical Practices
    • Configure Custom Service and Use Service Network Interface Card(SNIC) to Provide Service for Other VPCs
    • HAVIP Combined with Keepalived to Achieve Master-Backup Multi-Machine High Availability
    • Layer 2 Gateway Combined with Dedicated Line Access ET to Build Large Layer 2 Network Between IDC and Cloud VPC
    • Network Layer Security Description
    • Peering Connections Typical Practice
    • Security Group Configuration Practice (Advanced Level)
    • Security Group Configuration Practice (Beginner Level)
    • Typical Practice of Managing Elastic Network Interface Using Terraform
    • Typical Practice of Managing IPsec VPN Gateway Using Terraform
    • Using Keepalived in VPC to Achieve High-Availability Architecture
    • VPC Custom Route Table to Achieve Secure Traffic Mutual Access
    • Windows Using HAVIP to Configure Master-Backup Multi-Machine
    • Configure Custom Service and Use Service Network Interface Card (SNIC) to Provide Service for Other VPCs
  • VPC CLI
    • Configure BCE-CLI
    • Install BCE-CLI
    • Route Table Related Operations
    • Subnet Related Operations
    • Using VPC Service via CLI
    • Version Change Records
    • VPC Related Operations
  • Document center
  • arrow
  • VPCVPC
  • arrow
  • Typical Practices
  • arrow
  • Typical Practice of Managing IPsec VPN Gateway Using Terraform
Table of contents on this page
  • Overview
  • Prerequisites
  • Requirement scenarios
  • Solution overview
  • Configuration steps
  • Environment preparation
  • Deployment process
  • Query the VPN gateway list and IPSec Tunnel list
  • Resource Update
  • Resource deletion
  • Testing and verification
  • Related products

Typical Practice of Managing IPsec VPN Gateway Using Terraform

Updated at:2025-10-16

Overview

The IPsec VPN gateway connects the user IDC to a virtual private cloud through an encrypted public network tunnel.

vpc-vpn02.png

Prerequisites

  • You have already registered a Baidu AI Cloud account. If not, please complete the registration first.
  • Ensure that the gateway devices in the user's data center support both the IKEv1 and IKEv2 protocols. Local gateway devices with these protocols can interconnect with the cloud-based VPN gateway.
  • The gateway device in the user's data center must have a configured static Public IP.
  • Ensure that the network segments between the user's data center and the VPC do not overlap.
  • Understand the security group rules within the VPC and confirm that these rules allow the gateway devices in the user's data center to access cloud resources. For specific steps, please refer to "Querying Security Group Rules" and "Adding Security Group Rules."

Requirement scenarios

A company has created a VPC on Baidu AI Cloud with the network segment 172.16.0.0/16. The user's data center has a network segment of 10.24.0.0/20, with the gateway device at the data center using Public IP 211.XX.XX.XX. Due to business needs, the company requires interconnection between the user's data center and the cloud VPC. Users can establish a connection between the data center and the cloud VPC using an IPsec VPN Gateway and Tunnels, enabling smooth cloud-to-ground connectivity.

Solution overview

As illustrated below, users can connect their data centers to Baidu AI Cloud VPN through an IPSec VPN gateway.

Architecture Diagram.drawio.png

Configuration steps

Environment preparation

Terraform: A tool for safely and efficiently building, changing and versioning infrastructure. It is driven by configuration files where you define various components, and Terraform completes the creation, modification and management of all components based on these files. Currently, Baidu AI Cloud has integrated with Terraform. You can use Terraform to create and manage various Baidu AI Cloud resources such as CFC, BOS, VPC and BCC. For details, please refer to Terraform BaiduCloud Provider.

Deployment process

  1. Sign in to the Terraform official website and download the installation package suitable for your operating system.
  2. Set up the Terraform Baidu Provider information. Refer to the sample code below to create a configuration file, and save it as version.tf in the current directory.
Plain Text
1    terraform {
2      required_providers {
3        baiducloud = {
4          source = "baidubce/baiducloud"
5 version = "1.15.6" # Version No.; you can check version update information on the official website
6        }
7      }
8    }
  1. Run the command terraform init. Terraform will initialize the workspace based on the version.tf file in the current directory.
  2. Edit the Terraform configuration file by creating a new main.tf file. This file should specify metadata for the VPC, subnet, EIP, security group, VPN gateway, IPSec tunnel, and route table. Follow the configuration and steps outlined below:

For more parameter information, please refer to the official Baidu Provider Documentation

  • Create VPC
Plain Text
1resource "baiducloud_vpc" "vpc" {
2  name = "terraform_vpc"
3  cidr = "172.16.0.0/16"
4}
  • Create a subnet corresponding to an availability zone. This example adopts North China-Beijing-Available Zone D
Plain Text
1resource "baiducloud_subnet" "subnet" {
2  name        = "terraform_subnet"
3  zone_name   = "cn-bj-d"
4  cidr        = "172.16.32.0/20"
5  vpc_id      = baiducloud_vpc.vpc.id
6  description = "terraform test subnet"
7}
  • Create EIP
Plain Text
1resource "baiducloud_eip" "eip" {
2# EIP_BP
3  bandwidth_in_mbps = 1
4  billing_method    = "ByBandwidth"
5  payment_timing    = "Postpaid"
6}
  • Create a security group
Plain Text
1resource "baiducloud_security_group" "sg" {
2  name        = "terraform-sg"
3  description = "security group created by terraform"
4  vpc_id      = baiducloud_vpc.vpc.id
5}
  • Create security group rules
Plain Text
1#This allows inbound ICMP protocol. This example only performs ping tests. Ports and protocols can be enabled based on actual demands.
2resource "baiducloud_security_group_rule" "sgr1_in" {
3  security_group_id = baiducloud_security_group.sg.id
4  remark            = "remark"
5  protocol          = "icmp"
6  port_range        = ""
7  direction         = "ingress"
8  source_ip         = "all"
9}
10# Enable egress ICMP protocol
11resource "baiducloud_security_group_rule" "sgr1_out" {
12  security_group_id = baiducloud_security_group.sg.id
13  remark            = "remark"
14  protocol          = "icmp"
15  port_range        = ""
16  direction         = "egress"
17  source_ip         = "all"
18}
  • Create VPN gateway
Plain Text
1resource "baiducloud_vpn_gateway" "vpn_gateway" {
2# Wait for the EIP creation to complete before creating the VPN gateway.
3  depends_on     = [baiducloud_eip.eip]
4  vpn_name       = "test_vpn_gateway"
5  vpc_id         = baiducloud_vpc.vpc.id
6  description    = "test desc"
7# Bill type: prepaid, postpaid
8  payment_timing = "Postpaid"
9# Bind EIP
10  eip            = baiducloud_eip.eip.eip
11}
  • Create IPSec tunnel
Plain Text
1resource "baiducloud_vpn_conn" "vpn_conn" {
2  vpn_id        = baiducloud_vpn_gateway.vpn_gateway.id
3# The shared key is a Unicode string for IPsec connection certification. Both ends must use the same pre-shared key
4  secret_key    = "ddd22@www"
5# Local IP: Subnet in Baidu AI Cloud VPC that needs to enter the VPN tunnel. This example fills in the IP address of the created subnet
6  local_subnets = [
7    baiducloud_subnet.subnet.cidr
8  ]
9# Peer gateway refers to the IPsec VPN service gateway in the user's data center, which must be used in conjunction with Baidu AI Cloud's VPN gateway.
10  remote_ip      = "211.xx.xx.xx"
11# The peer network segments requiring VPN tunnel connectivity
12  remote_subnets = [
13    "10.24.0.0/20"
14  ]
15  description   = "test VPN conn"
16  vpn_conn_name = "vpnconn"
17  ike_config    = {
18# Select the IKE protocol version. Currently, IKE V1 and IKE V2 are supported
19    ike_version   = "v1"
20# Select the IKE V1 negotiation mode.
21# Main mode: High-security negotiation process.
22# Aggressive mode: Fast negotiation with a high success rate.
23# After successful negotiation, both modes provide identical information transmission security.
24    ike_mode      = "main"
25# Select the encryption algorithm for phase 1 negotiation, supporting aes, aes192, aes256, and 3des
26    ike_enc_alg   = "aes"
27# Phase 1 negotiation certification algorithms supported: sha1, md5, sha2_256, sha2_384, and sha2_512
28    ike_auth_alg  = "sha1"
29# Select the Diffie-Hellman Key Exchange algorithm for phase 1 negotiation
30    ike_pfs       = "group2"
31# Set the lifecycle of the SA negotiated in phase 1. Default value: 28,800 seconds.
32    ike_life_time = 28800
33  }
34  ipsec_config = {
35# Select the encryption algorithm for phase 2 negotiation, supporting aes, aes192, aes256, and 3des
36    ipsec_enc_alg   = "aes"
37# Select the certification algorithm for phase 2 negotiation, supporting sha1, md5, sha2_256, sha2_384 and sha2_512
38    ipsec_auth_alg  = "sha1"
39# Select the Diffie-Hellman Key Exchange algorithm for phase 2 negotiation
40    ipsec_pfs       = "group2"
41# Set the lifecycle of the SA negotiated in phase 2 Default value is 28,800 seconds
42    ipsec_life_time = 28800
43  }
44}
  • Create route table
Plain Text
1resource "baiducloud_route_rule" "route_rule" {
2  route_table_id      = baiducloud_vpc.vpc.route_table_id
3# Source IP address In this example, it is the IP address of the subnet created by the user.
4  source_address      = baiducloud_subnet.subnet.cidr
5# Destination IP address In this example, it is the IP address of the user's data center
6  destination_address = "10.24.0.0/20"
7# Next hop Instance
8  next_hop_id         = baiducloud_vpn_gateway.vpn_gateway.id
9# Next Hop Type: In this example, it should be VPN.
10  next_hop_type       = "vpn"
11  description         = "created by terraform"
12}
  1. Run the command terraform plan, and Terraform will generate a resource change plan based on the details in the current configuration file.
  2. Execute the command terraform apply and confirm with "yes" when prompted to automatically carry out the resource creation.

Query the VPN gateway list and IPSec Tunnel list

Plain Text
1data "baiducloud_vpn_gateways" "default" {
2# Enter the VPC ID here to query all gateway lists and details under the VPC
3  vpc_id = "vpc-xxxxxxx"
4}
5data "baiducloud_vpn_conns" "default" {
6# Enter the VPN ID here to query detailed information about the IPSec Tunnel associated with the VPN
7  vpn_id = "vpn-xxxxxxx"
8}

Resources can be filtered in results using a filter, example code:

Plain Text
1# Regular expressions can be used for filtering. The following code block filters resources whose names match the expression tf-test-VPN-* in the results.
2filter {
3   name = "name"
4   values = ["tf-test-VPN-*"]
5}

Resource Update

Update the VPN gateway and VPN tunnel configuration parameters in the main.tf file, then complete steps 5 and 6 of resource creation to finalize the resource update.

Resource deletion

The following command can be used to delete resources:

Plain Text
1    #By default, the command releases all resources in the current main.tf
2    terraform destroy
3 #You can destroy specific resources through the -target option, in the format: -target=<resource type>.<resource name>, for example:
4    terraform destroy -target baiducloud_vpn_gateway.test-vpn

Testing and verification

Upon completion of all configurations, users can create a Baidu Cloud Compute Instance within the subnet using Terraform as defined below to test network connectivity. Once security group rules are confirmed effective, use the ping Command to verify connectivity:

-$ ping 10.24.x.x

Plain Text
1# Baidu Cloud Compute Creation Example: Specific specification parameters can be flexibly configured as needed
2data "baiducloud_images" "images" {
3  image_type = "System"
4  name_regex = "8.4 aarch"
5  os_name    = "CentOS"
6}
7resource "baiducloud_instance" "server1" {
8# Availability zone This example creates a subnet in North China-Beijing-Zone D. The Availability Zone here should match the subnet
9  availability_zone = "cn-bj-d"
10# BCC specification
11  instance_spec     = "bcc.gr1.c1m4"
12# The image ID adopts CentOS 8.4 aarch image
13  image_id          = data.baiducloud_images.images.0.id
14# Payment type
15  billing           = {
16    payment_timing = "Postpaid"
17  }
18# Subnet ID subnet created via Terraform
19  subnet_id       = baiducloud_subnet.subnet.id
20# Security group security group created via Terraform
21  security_groups = [baiducloud_security_group.sg.id]
22}

At the same time, devices in the user data center should be able to connect to instances within the VPC.

Through the IPSec VPN, the user's data center has successfully connected to the cloud platform's internal resources, achieving seamless cloud-to-on-premises connectivity.

Related products

VPN gateway, Virtual Private Cloud (VPC), Baidu Cloud Compute (BCC), EIP

Previous
Typical Practice of Managing Elastic Network Interface Using Terraform
Next
Using Keepalived in VPC to Achieve High-Availability Architecture