How to create an instance with Terraform?

Learn how to launch an instance using Terraform with this step-by-step tutorial.

What is a Terraform?

Terraform is an infrastructure provisioning tool that allows you to construct infrastructure using code, often called Infrastructure as Code. Terraform, as a result, enables you to automate and maintain your infrastructure, platform, and applications that operate on that platform.

How does Terraform function?

Terraform uses configuration files and version control to define and manage your whole infrastructure. This is accomplished by utilizing the two primary components of Terraform architecture: Core and Providers.

Read and follow these instructions carefully. The instructions below describe how to create an instance with Terraform on Ubuntu.

Get Sarted

Step 1: Install Terraform

(a) Add the HashiCorp GPG key.

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

(b) For Terraform Repository.

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

(c) Run the system update command and install Terraform.

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

(d) Check if Terraform is installed correctly or not.

terraform -v

Step 2: Creating a provider

You specify "providers" for your cloud environment in Terraform. Your OpenStack infrastructure resources are hosted by a "provider." Make a file called provider.tf.

touch providers.tf
nano providers.tf

Step 3: Paste the following code

The OpenStack provider is used to communicate with the numerous OpenStack resources. Before it can be utilized, the provider must be set up with the appropriate credentials.

terraform {
  required_providers {
    openstack = {
      source  = "terraform-provider-openstack/openstack"
      version = "~> 1.48.0"
    }
  }
}

provider "openstack" {
  user_name   = var.username
  tenant_name = var.project_name
  password    = var.password
  auth_url    = "https://api-us-east-at-1.openstack.acecloudhosting.com:5000"
  region      = "us-east-at-1"
}

Step 4: Initialize the Terraform environment

Now, you need to initialize the working directory, which actually contains the terraform .tf file.

terraform init

Step 5: Create a variable file

touch variables.tf
nano varibles.tf

Step 6: Paste the following variables and edit as per your usage

variable "username" {

 type = string

 description = "Please enter your username for cloud console"

}

variable "password" {

 type = string

 description = "Please enter password for your cloud console"

}

variable "project_name" {

 type = string

 description = "Please mention the name of your project"

}


variable "instance_name" {

 type = string

 description = "Name of the instance to be created"

 default = "myinstance"

}

variable "region_name" {

 type = string

 description = "Name of the region where instance is to be created"

 default = "us-east-at-1"

 sensitive = true

}

variable "count_instance" {

 description = "Number of instances to be created"

 default = 1

}

variable "image_id" {

type = string

description = "ID of the image to be used with instance"

default = "c83dd18c-a2fa-4a85-91fb-553e16397be1"

}

variable "flavor_name" {

 type = string

 description = "Name of the flavor to be used with the instance"

 default = "c.8"

 sensitive = true

}

variable "key_name" {

 type = string

 description = "Name of the key to be associated with the instance"

 default = "mykeypair"

}

variable "volume_size" {

 description = "Size of the volume to be used with the instance"

 default = 10

}

Step 7: Create the main file

touch main.tf
nano main.tf

Step 8: Paste the following command

#Creating an instance
resource "openstack_compute_instance_v2" "instance_1" { #Resource block to create an instance with a volume
  name            = var.instance_name
  region          = var.region_name
  count           = var.count_instance
  flavor_name       = var.flavor_name
  key_pair        = var.key_name
  network {
    name      = "External_Net" # name of the network to be used
  }
  security_groups = ["default"] #Security group name from the cloud



 block_device {
    uuid                  = var.image_id
    source_type           = "image"
    destination_type      = "volume"
    volume_size           = var.volume_size
    boot_index            = 0
    delete_on_termination = true #set to false to save volume for backup
  }
}

Step 9: Create an execution plan

This command is a quick method to see if the execution plan for a set of modifications fits your expectations without affecting actual resources or the state.

After executing the command, enter your console username, password, and project name.

terraform plan

Step 10: Execute the actions

terraform apply

After executing the command, enter your console username, password, and project name again and enter yes to confirm.

Now instance has been created, you can verify it on the portal.

Step 11: Delete the instance

terraform destroy

After executing the command, enter yes to confirm. Your instance will be deleted.

Last updated