Importing ACI Configuration to Terraform

If you already have ACI running and then want to move to a DevOps approach and bring that configuration into Terraform, how can that be performed?

The ACI Terraform provider supports importing configuration. All that is required is the resource and name of that resource.

In the following example, I will use the Cisco ACI lab to import the tenant Heroes.

To import into Terraform for ACI all that is required is the resource and name.

Create New Terraform File

I have created an import.tf file that contains the Heroes tenant resource and the name of it. If the name argument is different to what has been configured in ACI it will be overwritten according to the Cisco docs.

Add Import Stage to CI File

I have added an import stage to my .gitlab-ci.yml file. This will add the tenant resource only.

If you were to add any new resources, then this will need to be updated. It is only here to serve as an example. This is a manual process to import everything.

Lastly, this stage will only run if there is a change detected to the import.tf file.

Here is the complete .gitlab-ci.yml file.

Running the Pipeline

When the pipeline runs, if there is a change detected to import.tf then the stage will run and import.

Before the import state is run, let’s look at the current terraform state file. This file is located under: Operate >> Terraform States
Download the JSON file by clicking on the three dots under Actions

If you download the state, there won’t be a reference for the Heroes tenant.
Run the pipeline to import the tenant.

Return to the terraform state file: Operate >> Terraform States
Inside this file will be the new tenant Heroes

The Heroes tenant has now been imported. It is not configuration, so running any destroy will lose the configuration.

Add to Configuration

Even though the tenant has been imported to the Terraform state file, it is not part of the configuration file.
The configuration can be pulled out of the JSON download for the Terraform state file. Below is the output from the Terraform state file.

To get the configuration for the resource out of this, use the ACI Terraform provider docs. In the reference guide, all the arguments have been referenced, but only some populated.

I am going to add in the configuration to the main.tf file.

For this to work, the tenant resource in the import.tf file must be removed. As this changes the file, the import stage will run in the .gitlab-ci file. I have modified this to be manual, so I can skip it.

Reviewing the build, the Heroes tenant is not going to be created as expected.

If I were to manually delete the Heroes tenant and re-run the build, I would expect to see it.

Leave a Comment

Your email address will not be published. Required fields are marked *