Skip to main content
New to Terraform? Start here
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:
Editing the state file directly is discouraged. Terraform modifies it to reflect your current infrastructure by running 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:
In our main file, we need to set the provider and its version:
Now we can initialize terraform to download the Latitude.sh provider and start managing our infrastructure:
Before we start importing our infrastructure, we need to export our Auth Token so Terraform can communicate with the Latitude.sh API and access our resources:
Now that we are all set up, let’s import our resources

Importing resources

Let’s exemplify the import process with a Project resource. To begin we’ll need an import 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.
Now let’s create the file for importing this project:
You’ll notice that we have set both the name and the environment of the Project resource. This is needed because both are required attributes. Running terraform plan should render something like this:
Here you’ll notice that there’s a ”-” signal before the description. That occurs because we haven’t defined a description attribute in our project resource block. to avoid updating the resource we will add the missing attribute to our resource.
Run terraform plan again. If everything looks good, run terraform apply to import the resource:
And it’s done! You can now manage this resource with Terraform. Now let’s import some other resources. All Latitude.sh resources and how to define them can be found on the Latitude.sh Terraform Provider docs.

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 the lsh command in your terminal to access your infrastructure. For example, to find the IDs of your tags, run lsh tags list. Tags list from the Latitude.sh CLI

Importing using for_each

Here we have two servers that we want to import using Terraform’s for_each syntax: Latitude.sh server list Create the following file:
Here we defined the server attributes as a local value object. Then we used the 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.
The IDs here are in the form of 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:
This will create a 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!