Skip to main content
Terraform is an infrastructure as code (IaC) driven software that allows you to programmatically configure your project infrastructure using configuration files and version control. Terraform ensures logical management and monitoring of resources such as servers, networks, and cloud storage instead of a manual operation for each required resource. These infrastructure configurations are handled via a declarative language called HCL (HashiCorp Configuration Language). For example, when you create a project or deploy a server, Terraform parses your code and translates it to an API call to the resource provider. Terraform integrations are called Providers. Providers are published to the Terraform Registry and can be consumed to quickly integrate infrastructure providers with the resources that compose your infrastructure. This guide will teach you how to use the Latitude.sh Terraform provider. Latitude.sh is a verified HashiCorp partner.

About the integration

With the Latitude.sh Terraform provider you can:
  • Create and update projects
  • Create and update servers
  • Create and update SSH keys
  • Use data sources and outputs to retrieve data.
If you already use Terraform to manage your infrastructure, getting started with the Latitude.sh provider is straightforward. If you don’t use Terraform yet, it shouldn’t take more than 10 minutes to get familiar with the basic concepts and start versioning your infrastructure.

Getting started

1

Install Terraform CLI

Install the Terraform CLI. This guide explains how to do this for your operating system.
2

Create API token

Create a Latitude.sh API token. Here is how to create an API token.
3

Export API token

Export the API token as a variable

Example integration

We will start by creating a project, getting the id of the project we just created, then adding our ssh key to that project, and finally creating a server that deploys to our new project and uses the SSH key we just added.
1

Create project directory

Create a directory for your Terraform project.
2

Create main.tf

Create a file named main.tf with the following content:
3

Initialize project

Run the following command in your terminal to Initialize the project.
You should see a success message after initializing the project that will contain something like this:
Terraform has been successfully initialized! You may now begin working with Terraform. Try running “terraform plan” to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
4

Create configuration files

Now we are finally ready to start creating infrastructure with Terraform. While you can add your codified resources to main.tf, as your infrastructure grows, it gets hard and dangerous to manage things from a single file, so it is usually best to split your files from the beginning.Create a file named variables.tf with the following content. This is optional but useful for this guide.
variables.tf
Create the following files
outputs.tf
project.tf
server.tf
billing accepts hourly and monthly for on-demand projects, and yearly for reserved projects. If you omit it, the server defaults to monthly, charged upfront based on the proration of the current billing cycle. On recent provider versions, terraform plan shows the resolved default (billing = "monthly") on create rather than (known after apply), so you can confirm how a new server will be billed before you apply. Use hourly for dynamic or short-lived workloads to avoid a full monthly charge. See On demand vs. Reserved for how each billing type is priced.
Optional if you have an SSH key in hand. If not, remove the ssh_keys from server.tf and the ssh section from variables.tf and ignore the next file.
ssh_key.tf

Run the plan

Now that we have all our files organized and referenced, we can create the infrastructure defined in them. Let’s start by reviewing the changes that will be made.
1

Review the plan

Run this on your terminal:
If you decide to use an SSH key, Terraform will ask for your public SSH key. Terraform will list every action it will take after we apply our plan. Review to make sure everything is how you want it to be.
2

Apply the plan

Apply the plan:
Paste your public SSH key again when requested and type yes on the prompt.

That’s it

You’ve just created three different resources with relationships between them. Commit this code to your repository to start versioning your infrastructure like a pro! Cleaning up is easy, just run the destroy command to rollback:
We touched the surface of what you can do with the Latitude.sh Terraform provider. To learn more, review the following links: