Creating a VPN connection
This article provides an example of creating and configuring a VPN connection using Terraform.
By following the steps in the guide, you will:
- Create a configuration file.
- Add resources and data sources for a virtual network.
- Add a resource for the VPN connection.
- Create the added resources.
For a complete description of the parameters, see the Terraform provider documentation.
-
Check the quotas. Make sure that the selected region has enough resources to create networks. Different regions may have different quotas.
If necessary, increase the quotas.
-
ссылка, if you have not already done so.
-
Place the provider settings file in the directory from which you will work with the project, and from that directory run the command:
terraform initWait for Terraform initialization to complete.
In the directory from which you will work with the project, create the vpn.tf file. This file will describe the configuration of the connection being created.
-
Depending on the SDN you use, copy the contents of one of the tabs below into the
vpn.tffile.NeutronSprutdata "vkcs_networking_network" "extnet" {name = "ext-net"}resource "vkcs_networking_network" "network" {name = "vpnaas_network"sdn = "neutron"}resource "vkcs_networking_subnet" "subnet" {name = "vpnaas_subnet"network_id = vkcs_networking_network.network.idcidr = "192.168.199.0/24"}resource "vkcs_networking_router" "router" {name = "router"sdn = "neutron"external_network_id = data.vkcs_networking_network.extnet.id}resource "vkcs_networking_router_interface" "router_interface" {router_id = vkcs_networking_router.router.idsubnet_id = vkcs_networking_subnet.subnet.id}To create the network, the following objects are required:
-
Resources (resource):
- vkcs_networking_network — the network in which the ВМ will be created.
- vkcs_networking_subnet — a subnet in this network.
- vkcs_networking_router — a router that connects the private network to external networks.
- vkcs_networking_router_interface — an interface that connects the router to the internal network.
-
Data source (data source): vkcs_networking_network — an external network for the router to obtain a public IP address, as well as to allocate a Floating IP address to entities in the private network.
-
-
Edit the settings values for your connection.
For more details on creating virtual networks using Terraform, see the hands-on guide ссылка.
-
Copy the VPN connection settings into the
vpn.tffile:resource "vkcs_vpnaas_service" "service" {router_id = vkcs_networking_router.router.id}resource "vkcs_vpnaas_ipsec_policy" "policy_1" {name = "ipsec-policy"}resource "vkcs_vpnaas_ike_policy" "policy_2" {name = "ike-policy"}resource "vkcs_vpnaas_endpoint_group" "group_1" {type = "cidr"endpoints = ["10.0.0.24/24", "10.0.0.25/24"]}resource "vkcs_vpnaas_endpoint_group" "group_2" {type = "subnet"endpoints = [ vkcs_networking_subnet.subnet.id ]}resource "vkcs_vpnaas_site_connection" "connection" {name = "connection"ikepolicy_id = vkcs_vpnaas_ike_policy.policy_2.idipsecpolicy_id = vkcs_vpnaas_ipsec_policy.policy_1.idvpnservice_id = vkcs_vpnaas_service.service.idpsk = "secret"peer_address = "192.168.10.1"peer_id = "192.168.10.1"local_ep_group_id = vkcs_vpnaas_endpoint_group.group_2.idpeer_ep_group_id = vkcs_vpnaas_endpoint_group.group_1.iddpd {action = "restart"timeout = 42interval = 21}depends_on = [vkcs_networking_router_interface.router_interface]}The following resources are used to add a VPN connection:
-
vkcs_vpnaas_service — manages the VPN service within VK Cloud. Includes the
router_idparameter — the router ID. By changing the value of this parameter, you will create a new service. If you need to use an existing router, specify its ID (data.vkcs_networking_router.router.idordata.vkcs_dc_router.router.id) using a data source. -
vkcs_vpnaas_ipsec_policy — manages the IPSec policy of the resource within VK Cloud. Includes the
nameparameter — the name of the policy being created. By changing the value of this parameter, you will change the name of an existing policy. -
vkcs_vpnaas_ike_policy — manages the IKE policy of the resource within VK Cloud. Includes the
nameparameter — the name of the policy being created. By changing the value of this parameter, you will change the name of an existing policy. -
vkcs_vpnaas_endpoint_group — manages an endpoint group within VK Cloud. Includes the following parameters:
type— the type of endpoints in the group. Can besubnet,cidr,network,router, orvlan. By changing the value of this parameter, you will create a new group.endpoints— a list of endpoints of the same type included in the endpoint group. The type of list items is determined by thetypeparameter. By changing the value of theendpointsparameter, you will create a new group.
-
vkcs_vpnaas_site_connection — manages an IPSec site connection resource within VK Cloud. Includes the following parameters:
-
name— the connection name. By changing the value of this parameter, you will change the name of an existing connection. -
ikepolicy_id— the IKE policy ID. By changing the value of this parameter, you will create a new connection. -
ipsecpolicy_id— the IPsec policy ID. By changing the value of this parameter, you will create a new connection. -
vpnservice_id— the VPN service ID. By changing the value of this parameter, you will create a new connection. -
psk— the public key. Accepts anystringvalues. -
peer_address— the FQDN or public IP address (IPv4 or IPv6) of the peer gateway. -
peer_id— the peer router ID for authentication. Can take values of the following types:<АДРЕС_IPv4>,<АДРЕС_IPv6>,<EMAIL>,<KEY_ID>,<FQDN>. Typically, the value of this parameter matches the value of thepeer_addressparameter. By changing the value of this parameter, you will change the policy of an existing connection. -
local_ep_group_id— the endpoint group ID that includes the private subnets of the local connection. Requires thepeer_ep_group_idparameter to be specified if backward compatibility mode is not enabled, wherepeer_cidrsvalues are already provided together with thesubnet_idvalue of the VPN service. By changing the value of this parameter, you will change an existing connection. -
peer_ep_group_id— the endpoint group ID that includes the private CIDR addresses of the peer connection in the<IP-АДРЕС>/<ПРЕФИКС>format. Requires thelocal_ep_group_idparameter to be specified if backward compatibility mode is not enabled, wherepeer_cidrsvalues are already provided together with thesubnet_idvalue of the VPN service. -
dpd— a dictionary of settings for the Dead Peer Detection (DPD) protocol. Includes the following resources:action— the DPD action. Possible values:clear,hold,restart,disabled,restart-by-peer. Default value:hold.timeout— the DPD timeout in seconds. Acceptspositive integervalues that are greater thaninterval. Default value:120.interval— the DPD interval in seconds. Acceptspositive integervalues. Default value:30.
-
depends_on— the VPN connection will start after the specified resources are created.
-
-
-
Edit the settings values for your connection.
-
Go to the directory containing the
vpn.tffile. -
Make sure the configuration files are correct and contain the required changes:
terraform validate && terraform plan -
Apply the changes:
terraform applyWhen prompted for confirmation, enter
yes. -
Wait for the operation to complete.
Make sure that the network and infrastructure were created successfully:
- Go to the VK Cloud management console.
- Go to Virtual networks → VPN. Make sure the VPN connection is created and includes all resources added in the example.
If the resources created with Terraform are no longer needed, delete them:
-
Go to the directory with the Terraform configuration files.
-
Run the command:
terraform destroyWhen prompted for confirmation, enter
yes. -
Wait for the operation to complete.