Why you need Terraform

Why you need Terraform

We have launched our own Terraform provider.

Now it’s even easier to manage your Gcore Cloud infrastructure.

With this tool, you can create, modify, and delete resources in our cloud using the Infrastructure as Code methodology.

We’ll now explain what Terraform is and how to use it with Gcore.

What is Terraform

Terraform is an open-source tool developed by HashiCorp in 2014. The Infrastructure as Code (IaC) approach allows you to describe the cloud infrastructure through a set of configuration files, thereby setting the rules for how everything should be configured.

Why you need Terraform

You write the code. Terraform reads it and, through API calls, brings everything to the described state.

Pros of Terraform:

  • There is no need to create, modify, and delete resources manually in the provider’s control panel.
  • The code manages the cloud networks and resources, and is also the documentation.

Terraform uses the HashiCorp Configuration Language programming language. It’s relatively simple and logical. But you can use JSON instead if you want.

Three advantages of Terraform

1. Versatility. This tool is supported by many different cloud providers. Therefore, using Terraform, you can manage infrastructure across multiple clouds at once. In addition, it works with Docker, Kubernetes, Chef, and other systems. With Terraform, you can embed any application in any language into your architecture.

2. Security. Without Terraform, every software application needs to be updated in the same place where it’s installed. Thus, each server accumulates its own unique software update history. Minor differences in the programs in the different systems lead to the “configuration drift”, which creates vulnerabilities for hacker attacks.

Terraform is based on the concept of immutable infrastructure. This means that any update to the code results in a new configuration. Consequently, all software can be updated easily and quickly across the entire system simultaneously.

3. Convenience and simplicity. The immutable infrastructure concept also makes rollbacks a lot easier. With Terraform, it’s as easy as picking a configuration from a list.

Also, Terraform is declarative code. To manage the infrastructure, you only need to specify what form it should take, and the tool determines the best ways to bring the system to that state.

Another advantage of Terraform is that it builds the architecture through the API. No agent software required, no separate server for configuration management, and no unnecessary security checks. Interaction with the system is direct, giving you almost free rein in terms of orchestration.

How to work with Terraform in Gcore Cloud

With our Terraform provider, you can manage your entire cloud infrastructure:

  • virtual machines
  • bare metal servers
  • drives
  • firewall groups
  • load balancers
  • networks
  • floating IP addresses
  • reserved IP addresses

Let’s take a look at the basic settings to get you started with Terraform.

How to install and configure Terraform

To install Terraform, download the distribution kit for your operating system, unpack the binary file from the archive, and add it to Path. The HashiCorp website has detailed installation instructions for each operating system.

You start working with Terraform by creating configuration files with the .tf extension. In them, you will describe your infrastructure with code.

To use Terraform in the Gcore Cloud architecture, specify the provider in the configuration file and configure its settings:

terraform {
required_version = ">= 0.13.0"
required_providers {
gcore = {
source = "local.gcore.com/repo/gcore"
version = "~>0.0.15"
}
}
}
provider gcore {
user_name = "test"
password = "test"
gcore_platform = "https://api.gcore.com/id"
gcore_api = "https://api.gcore.com/cloud"
}

After that, run the terraform init command. It initializes the provider and downloads a set of modules needed to work with our resources.

After that, you can create resources and execute various commands.

How to use Terraform

After configuring the provider, resource configurations are set in the file.

For example, this is how a cloud network is created:

resource "gcore_network" "network" {
name = "network_example"
mtu = 1450
type = "vxlan"
region_id = 1
project_id = 1
}

Useful commands and tools

Once you’ve got your infrastructure set up, it’s time to use the terraform plan command. This feature is needed mainly for testing. It shows what changes Terraform is about to make. If you make mistakes while creating the configuration, Terraform will point them out.

Correct errors, if any. Now you can proceed to the next step: terraform apply. This command makes changes to the existing infrastructure. After you enter it, Terraform will ask you to confirm the action. Enter “yes”.

The terraform import command allows you to transfer the resource configuration manually created in the control panel into the code.

The terraform refresh command is used if you have a ready-made resource configuration in your code, but you have made changes in the control panel manually and now want these changes to be reflected in the configuration files. Terraform will take the existing architecture as a reference and fix the code.

Apart from this, the Terraform syntax offers many useful tools. For example, modules provide the option to combine different sets of resources into logical blocks, and to reuse these blocks later. With the help of expressions you can search and access various data in the code: for example, take an element from a list or find a value based on a condition.

For more information on working with our Terraform provider, see GitHub.

Not signed up for Gcore Cloud yet? Streamline your infrastructure management with our cloud and Terraform provider right now. Or start with a free consultation.

More about Gcore Cloud

Get a free consultation

Why you need Terraform

Subscribe
to our newsletter

Get the latest industry trends, exclusive insights, and Gcore
updates delivered straight to your inbox.