The Latitude.sh Terraform provider supports importing existing infrastructure into Terraform. This feature is useful when you want to add existing Latitude.sh resources to Terraform to start managing them with code. Before importing your resources, it helps to understand how Terraform keeps track of your infrastructure. Terraform maps the attributes and metadata of your resources to its internal state and save it to a local file named
terraform.tfstate. Every change to a .tf file will be compared with the Terraform state to determine what changes need to be made.
Here is an example of a .tfstate file:
terraform refresh before terraform plan.
If you need more advanced state management, use the
terraform state command
to edit it in a more controlled environment.Migrating your existing Infrastructure
If you already have resources deployed on Latitude.sh, importing them on Terraform is straightforward. Let’s walk through an example:Creating the .tf file
To start the migration of our infrastructure, let’s initialize a Terraform project in a new directory:
Importing resources
Let’s exemplify the import process with a Project resource. To begin we’ll need animport block with two attributes
- to: The resource defined inside Terraform that will be used to manage your infrastructure.
- id: The id of the existing resource.
You can find the id of the project in the Home section of the dashboard.
terraform plan should render something like this:
terraform plan again. If everything looks good, run terraform apply to import the resource:
Finding resources IDs
As we’ve seen, it is necessary to pass the ID of the resource to the import block. The fastest way to obtain the IDs is by using our CLI, but you can also find them from the dashboard and API. To learn how to set up the CLI, see the Latitude.sh CLI documentation page. After installation, you can use thelsh command in your terminal to access your infrastructure.
For example, to find the IDs of your tags, run lsh tags list.

Importing using for_each
Here we have two servers that we want to import using Terraform’s for_each syntax:
for_each keyword to loop over the objects and get their key-value pair to build our resource. Running terraform apply should import the servers successfully.
Always run
terraform plan before applying your changes to verify the changes
that will be made.Generating Configuration
Terraform allows us to generate the resource based on our import block. Let’s import the rest of our infrastructure.projectID:resourceID. This is needed for nested resources. On the provider documentation, you can verify if a resource is a nested resource or not.
To automatically generate the resource blocks, run:
generated_resources.tf file with the resource blocks of the remaining infrastructure. Generating configuration is still an experimental feature, and it’s highly encouraged for you to verify the generated resources.
You can edit and organize resources in different files. When you’ve finished verifying the generated resources, run terraform apply to finish your configuration.
Now you know everything needed to import your existing Latitude.sh infrastructure to Terraform.
Happy coding!