Importing Existing NIOS Objects to Terraform
Infoblox IPAM Plug-in for Terraform allows you to import resources (NIOS objects) that already exist in NIOS. For the list of resources that are supported by the plug-in, see Resources and Data Sources Supported by Infoblox IPAM Plug-In for Terraform.
Use one of the following methods to import NIOS objects:
Consider the following when using the import functionality:
For A, AAAA, PTR, host, and network resources, the import operation does not support fields with dynamically allocated values such as
cidr
,allocate_prefix_len
,reserve_ip
, andreserve_ipv6
that are supposed to be used only during the creation of Terraform resources. If configured in an object being imported, the import operation sets the values of such fields tonull
in the terraform.tfstate file.To remove the imported allocation and association resources from Terraform by using the
terraform destroy
command, you must do the following:Run the
terraform apply
command for Terraform, to identify the dependencies between the resources and to determine the order in which the resources must be managed.Run the
terraform destroy
command.
Import by Using a CLI Command
Use the terraform import
CLI command to import NIOS objects. The import operation includes the following steps:
Creating a Terraform configuration file for the resource you want to import.
Obtaining a reference for the NIOS object you want to import.
Importing the resource using the obtained reference.
To import a resource to Terraform, do the following:
Create a Terraform configuration (.tf) file for the resource you want to import.
You must include all attributes and their values configured in NIOS.
For example, the following resource block is for an object with namea_rec_1_imported
and the attributes defined in NIOS:resource "infoblox_a_record" "a_rec_1_imported" { fqdn = "rec-a-1.imported.test.com" ip_addr = "192.168.1.2" ttl = 10 comment = "A-record to be imported" ext_attrs = jsonencode({ "Location" = "New office" }) }
Based on the type of object that you want to import, obtain a reference for the NIOS object as follows:
To import a resource other than a host record, use an appropriate tool to get a reference for the NIOS object.
The following example shows a cURL command that can be run in a fully-trusted environment to get a reference for an A record object in NIOS:curl -k -u admin:infoblox https://172.17.0.2:443/wapi/v2.11.1/record:a
To import a host record, which is represented by the
infoblox_ip_allocation
andinfoblox_ip_association
resources in Terraform, add the Terraform Internal ID extensible attribute with a randomly generated value in the form of a UUID to the record.
- For steps for adding the extensible attribute in NIOS, refer to the Infoblox NIOS Documentation.
- To generate a UUID, you may use the command-line tooluuid
for Linux-based systems or any other appropriate tool.
Note that the Terraform Internal ID extensible attribute does not show in the terraform.tfstate file. Use it to create or import theinfoblox_ip_allocation
andinfoblox_ip_association
resources. Do not add it in a resource block with other extensible attributes.
Run the import command as follows:
For a resource other than a host record, use the format:
terraform import RESOURCE_TYPE.RESOURCE_NAME RESOURCE_REFERENCE
Example:terraform import infoblox_a_record.a_rec_1_imported record:a/ZG5zLmJpbmRfYSQuX2RlZmF1bHQub3JnLmV4YW1wbGUsc3RhdGljMSwxLjIuMy40:rec-a-1.imported.test.com/default
For a host record, use the format:
terraform import RESOURCE_TYPE.RESOURCE_NAME TERRAFORM_INTERNAL_ID
Example:terraform import infoblox_ip_allocation.ip_allocation_import 841d2d08-698a-11ed-8ebb-87cc2c60a394
Import by Using the import
Block
As a prerequisite, for the object that you need to import, obtain the reference ID assigned to the object in NIOS. For instructions, see step 2 in the Import by Using a CLI Command section.
Define the import
block in the Terraform Configuration (.tf) file of a resource that must be imported. In the .tf file of the resource to import, include the following block:
import {
to = <resource_type>.<resource_name>
id = "reference_id"
}
The parameters you can define in the import
block are:
Parameter | Required/Optional | Description |
---|---|---|
| Required | Specifies the instance address of this resource in the Terraform state file. |
| Required | Specifies the reference ID assigned to an object in NIOS. |
Examples
import {
to = infoblox_a_record.imported_records
id = "record:a/ZG5zLmJpbmRfYSQuX2RlZmFW1wbGUxLHRlc3QxLDEyLjEyLjEy:test1.example1.org/default"
}
The following example is to import all A records from the specified DNS zone. The example is using the data source block to retrieve the reference IDs of A records and pass them to the import
block.
//import all A-records from the zone /example1.org
data "infoblox_a_record" "data_arec" {
filters = {
zone = "example1.org "
view = "default"
}
}
import {
for_each = data.infoblox_a_record.data_arec.results
id = each.value.id
to = infoblox_a_record.imported_records["${each.value.fqdn}"]
}
resource "infoblox_a_record" "imported_records" {
for_each = { for record in data.infoblox_a_record.data_arec.results : record.fqdn => record }
fqdn = each.value.fqdn
ip_addr = each.value.ip_addr
dns_view = each.value.dns_view
ttl = each.value.ttl
comment = each.value.comment
ext_attrs = each.value.ext_attrs
}
Note
When using the Terraform import
block for a resource, a new Terraform internal ID is assigned to the resource when the terraform plan
command is run for the first time. If a subsequent terraform apply
is aborted, the record will still retain the Terraform Internal ID though the resource is not managed by Terraform.