Getting Started

How to Configure Terraform for E2E

To use Terraform with E2E, you need to install Terraform and configure a provider file.

Install Terraform

You can install the latest version of Terraform on most operating systems from the command line using various package managers. Click your operating system’s tab below to view instructions on how to Terraform.

Linux :-

To install Terraform on Ubuntu, add the HashiCorp GPG key to your system:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

Next, add the official HashiCorp Terraform Linux repository to apt:

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

Then update apt and install Terraform:

sudo apt-get update && sudo apt-get install terraform

Once installed, verify the installation:

terraform -v

MacOS :-

To install Terraform on MacOS using Homebrew, run the following command in a terminal:

brew install terraform

Once installed, verify Terraform’s installation:

terraform -v

Windows :-

To install Terraform on Windows using Chocolatey, run the following command from the command prompt:

choco install terraform

Once installed, verify Terraform’s installation:

terraform -v

CentOS :-

To install Terraform on CentOS, install the yum-config-manager to manage your repositories:

sudo yum install -y yum-utils

Use yum-config-manager to add the official HashiCorp Linux repository:

sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo

Then install Terraform:

sudo yum -y install terraform

Once installed, verify Terraform’s installation:

terraform -v

To use the e2e provider with Terraform, you have to configure the plugin using a provider file. This file tells Terraform which provider you’re using (e2e) and where to find the necessary credentials (your e2e API token, and your auth To start, create and move into a directory from which you will configure and deploy your infrastructure. This is also where you create the provider file. To install this provider, copy and paste this code into your Terraform configuration. Then, run terraform init. Checkout the terraform registery for e2e provider here and choose the latest version.

terraform {
  required_providers {
    e2e = {
      source = "e2eterraformprovider/e2e"
      version = "2.0.4"    # checkout the latest version from terraform registery
    }
  }
}

provider "e2e" {
  # Configuration options
}

Provider Configuration Schema

Argument Reference

Example usage

terraform {
  required_providers {
    e2e = {
      source  = "e2eterraformprovider/e2e"
      version = "2.0.4"      # use latest version
    }
  }
}

provider "e2e" {
api_key    = "your api_key"
auth_token = "your auth_token"
}

Execute Terraform

Once you have configured your Terraform files, you can deploy all of the resources you have configured from the command line. Terraform requires three steps for deployment: initializing the directory, reviewing an execution plan, and applying (executing) the Terraform plan. Initialization prepares the working directory for use by accounting for any changes in Terraform’s backend configuration. The planning step provides you a detailed manifest of the resources for you to review before execution. Lastly, the terraform apply command executes the deployment of the resources into your account. To initialize the working directory:

terraform init

If Terraform was successful in initializing the directory, you receive the message Terraform has been successfully initialized!. Next, you need to create and view your Terraform plan. To create your Terraform plan:

terraform plan

Terraform returns a manifest of resources it will deploy when you apply the plan.After reviewing the plan, you can apply it and deploy the resources to your account. To execute the plan: Before applying terraform will prompt you to confirm by typing yes. You must checkout the changes in the configuration before applying.

terraform apply

You can checkout the current terraform state:

terraform show

You can also delete your resource created by terraform

terraform destroy

But it will destroy all the resources. To destroy a target resource use the command below

terraform destroy  —- target e2e_node.demo_node_name

Here resource of type e2e_node and name demo_node_name is deleted. Or you can simply clear the resource from configuration file and then command terraform apply. The resource will be deleted.

Create Terraform Configuration Files

Once you have configured Terraform to access your e2e account, you can begin developing Terraform files that describe and declare the e2e resources that you want to deploy into your account. Terraform configuration files are text files stored with .tf extensions. They are human-readable and they support comments. During deployment, Terraform loads all files with a .tf extension and creates a manifest of resources to deploy called a “plan”. You can divide resource configurations between as many or as few .tf files as you want. Below is the sample terraform file for launching a node. You can copy paste the file into your working directory as a new .tf file. The fields inside the resource e2e_node is discussed in the next section. example.tf

terraform {
    required_providers {
      e2e = {
        source = "e2eterraformprovider/e2e"
        version = "2.0.4"
    }
  }
}
provider "e2e" {
  api_key = "your e2e api key"
  auth_token = "your e2e auth bearer token"
  }
resource "e2e_node" "demo_node_1" {
    name="C2-4GB-416B"
    region="Delhi"
    plan="C2.12GB"
    image="CentOS-7"
}

Importing already existing infrastructure

Write Config for Resource To Be Importe. Terraform import does not generate the configuration files by itself. Thus, you need to create the corresponding configuration for the node resource manually. you can skip a few arguments anyway. In a moment we will take a look at how to adjust our configuration to reflect the exact resource. For now, append the main.tf file with node config. remember that you have to include the required fields (refer e2e_node docs)

resource "e2e_node" "mynode" {
  name  = "unknown"
  plan  = "unknown"
  image = "unknown"
  region = "unknown"
}

Think of it as if the cloud resource (node) and its corresponding configuration were available in our files. All that’s left to do is to map the two into our state file. We do that by running the import command as follows.

terraform import e2e_node.mynode <Node ID>

Later after import is successfull. Run terraform plan. The plan indicates that it would attempt to replace the node resource. But this goes completely against our purpose. We could do it anyway by simply not caring about the existing resources, and creating new resources using configuration. Observe the plan output, and find all those attributes which cause the replacement (4 in our case). The plan output will highlight the same. use terraform show command and copy the values of those fields to the configuration file. Closing this gap should avoid the replacement of the node resource.