Terraform

Terraform Introduction

  • Terraform is a tool for performing system provisioning
  • It uses HCL syntax (Hashicorp Configuration Language)

HCL (Hashicorp Configuration Language)

Terraform uses HCL for performing the provisioning type.

<block> <resource-type> <logical-name> {
    arg1 = value1
    arg2 = value2
}

The syntax is divided into

block: Block name defines the type of block

Resource-Type: Resource type is the predefined names for every resource. like "local_file", where $local$ defines the provider type which is local. $file$ define the resource.

Resource-name: It is the name of resource

arguments: All the arguments

For e.g.

Creating a file in local resource

resource "local_file" "basic_file" {
    filename = "/home/basic_file.txt"
    content = "This is a basic file created by Terraform"
}

AWS EC2 Instance provisioning

resource "aws_instance" "backendserver"{
    ami = "ami-10101010"
    instance_type = "t2.micro"
}

AWS S3 Bucket provisioning

resource "aws_s3_bucket" "doc_bucket" {
    bucket = "document_bucket_for_backend"
    acl = "private"
}


## Start provisioning

1. Initiate the provisioning 
```terraform
terraform init
  • This command check the configuration files
  • Initiate the working directory with name .terraform
  • Understand about the provider type and install essential plugins
  1. Review the plan
terraform plan 

This command will display the terraform action plan

  1. Apply the changes
terraform apply
  1. To show the resources
terraform show
  1. To Destroy the complete provisioning
terraform destroy

Terraform Configuration file Structure

  • main.tf Main file contains resource configuration

  • variables.tf Contains variable declarations

  • outputs.tf Contains Outputs from Resources

  • provider.tf Contains Provider Definition

Multiple provider

To illustrate this,

lets make use of new provider "random"

To create random details we can use random as resource type

resource "random_pet" "my-employee" {
    prefix = "Mr."
    seperator = "."
    length = "1"
}

Defining the variables

Variable Syntax

variable "<variable_name>" {
    default = "<value>"
}