Terraform is a software toolkit designed for organizing infrastructure, including its creation and further management in the cloud. Its main advantage lies in automating all processes related to infrastructure deployment.
Private IP addresses are intended for organizing interaction between users within the same network. They are used to identify users who have connected to the network, as well as to grant them specific permissions for work.
In this guide, we will explain how Terraform can be used to set up private IP addresses and subsequently manage them.
This guide is suitable for providers where users can select their own IP addresses. Note that Hostman doesn’t provide this option at the moment.
First, compile a list of requirements for the future private IP addresses:
Next, you need to write the code responsible for creating private IP addresses.
provider "provider_name" {
project = "<project_name>"
region = "<region>"
}
resource "resource_name" "subnet_name" {
name = "subnet_name"
ip_cidr_range = "IP_address_range"
network = "network_name"
}
resource "resource_name_for_private_ip" "private_ip_name" {
name = "private_ip_name"
subnetwork = resource_name.subnet_name.self_link
}
Let’s break down the configuration to better understand it.
provider "provider_name" {
project = "<project_name>"
region = "<region>"
}
Here we specify the provider name, project name, and region. All of these are required for creating future resources.
resource "resource_name" "subnet_name" {
name = "subnet_name"
ip_cidr_range = "IP_address_range"
network = "network_name"
}
This part creates a subnet, which will later be used to assign resources. Here, you should specify the subnet name, IP address range, and the name of the network where the subnet will be created.
resource "resource_name_for_private_ip" "private_ip_name" {
name = "private_ip_name"
subnetwork = resource_name.subnet_name.self_link
}
The last fragment is where we create the private IP address. It will be assigned within the subnet you set up in the previous fragment. The private IP address will fall within the range specified earlier.
We also use a reference to the self_link
attribute (URI of the created resource) for the previously created subnet.
After writing the code, you can proceed to create the resource creation.
Run terraform plan
to display which resources will be created. If everything is correct, run terraform apply
to create the resources.
With Terraform, you can modify existing resources by editing your configuration.
If you need to edit, update, or delete private IP addresses you created, change the settings in the configuration file and run terraform plan
to verify the settings.
If the configuration is valid, use terraform apply
to implement the changes.
Modules allow you to organize containers for multiple resources at once, which will be used together. You can also call modules multiple times, enabling you to package resource configurations and reuse them.
The root module can call other child modules.
If you plan to create a large number of private IP addresses, you can use modules to reuse your code.
Variables allow you to customize aspects of Terraform modules without changing the module's source code. This feature enables modules to be shared across different Terraform configurations, making your module more modular and reusable.
The root module will contain only references to variables. All variable content will be in another module, which you can edit as needed. It is recommended to create a module named variables.tf
for convenience.
Example of using variables to create private IP addresses:
provider "provider_name" {
project = var.project_id
region = var.region
}
module "private_ip_address" {
source = "./modules/private-ip-address"
subnetwork_name = var.subnetwork_name
ip_cidr_range = var.ip_cidr_range
network_name = var.network_name
ip_address_name = var.ip_address_name
}
variable "project_id" {
type = string
description = "Project identifier."
}
variable "region" {
type = string
description = "Server region."
}
variable "subnetwork_name" {
type = string
description = "Subnet name."
}
variable "ip_cidr_range" {
type = string
description = "IP address range for the subnet."
}
variable "network_name" {
type = string
description = "Network name."
}
variable "ip_address_name" {
type = string
description = "private IP address name."
}
You also need to create a file containing the variable declarations.
Before making changes to the configuration, always check them using terraform plan
. Only after that should you apply changes using terraform apply
.
When creating new resources, always check which people and services are granted access. This applies not only to the process of creating private IP addresses.
In addition, you can use security scanning tools such as tfsec and Checkov. They can help detect potential vulnerabilities in your configuration.
Provide sufficient documentation for each resource and module you create, including leaving comments in the code. This will help you easily understand what is happening in your infrastructure and quickly resolve issues.
In this guide, we have described in detail the process of creating and managing private IP addresses in Terraform, as well as provided additional tips for improving your configuration.