Terraform : Get Started

Related image

 

Terraform uses text files with .tf extension to describe infrastructure are called Terraform configurations files.

The format of the configuration files can be in two formats.

Terraform format – It is more human-readable, supports comments, and recommended format for most Terraform files. Terraform format ends in .tf

Syntax:

/* Create an instance
on AWS. 
*/
resource "aws_instance" "web-server" {
# Amazon Linux 2 64 Bit
    ami = "ami-0998858ab6ad47da8"
    instance_type = "t2.micro"
    count = 1
}

JSON format – It is meant for machines to create, modify, and update, but can also be done by Terraform operators if you prefer. JSON format ends in .tf.json.

Syntax: 

{
 "resource": {
    "aws_instance": {
       "web-server": {
          "ami": "ami-0998858ab6ad47da8",
          "instance_type": "t2.micro",
          "count": 1
        }
    }
 }
}

Provider

Providers are responsible for managing the life cycle of a resource such as create, read, update, delete.

Providers require some sort of configuration to provide authentication information, endpoint URLs, etc.

The provider needs to be configured with proper credentials before it can be used.

AWS Provider:

The Amazon Web Services (AWS) provider is used to interact with resources in AWS.

# Configure the AWS Provider
provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region = "us-east-1"
}

# Create a web server
resource "aws_instance" "web" {
# ...
}

Google Cloud Platform Provider:

The Google provider is used to configure infrastructure in Google Cloud Platform.

provider "google" {
  credentials = "${file("account.json")}"
  project = "my-project-id"
  region = "us-central1"
}

resource "google_compute_instance" "default" {
  name         = "test"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"
}

Azure Provider:

The Azure Provider can be used to configure infrastructure in Microsoft Azure using the Azure Resource Manager API’s.

Terraform supports different methods for authenticating to Azure

  • using Azure CLI
  • using Managed Service Identity
  • using a Service Principal and a Client Certificate
  • using a Service Principal and a Client Secret
# Configure the Azure Provider
provider "azurerm" {
  subscription_id = "00000000-0000-0000-0000-000000000000"
  tenant_id       = "11111111-1111-1111-1111-111111111111"
}
# Create Resource Group
resource "azurerm_resource_group" "test" {
  name     = "production"
  location = "West US"
}

Terraform Initialization

The terraform init command is used to initialize a working directory containing Terraform configuration files.

This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control.

This command is always safe to run multiple times, to bring the working directory up to date with changes in the configuration.

Initializing working directory

$ mkdir aws-terraform
$ cd aws-terraform/
# Create provider and instance configuration files
$ vi provider.tf
$ vi instance.tf
$ ls 
instance.tf  provider.tf
$ terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "aws" (1.52.0)...
........
.........

Check currently configured providers in working directory, use following command:

$ terraform providers
.
└── provider.aws

 

To launch instance in AWS, use terraform apply command.

$ terraform apply
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Default: us-east-1
  Enter a value: 


An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

........
..........
aws_instance.example: Still creating... (30s elapsed)
aws_instance.example: Creation complete after 35s (ID: i-07d46ddedb1d3eae5)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Once task completed, check in the AWS console.

To delete the instance, use terraform destroy command

$ terraform destroy
provider.aws.region
  The region where AWS operations will take place. Examples
  are us-east-1, us-west-2, etc.

  Default: us-east-1
  Enter a value: 
........
..........
............aws_instance.example: Destroying... (ID: i-07d46ddedb1d3eae5)
aws_instance.example: Destruction complete after 1m10s

Destroy complete! Resources: 1 destroyed.

To check the execution plan, terraform plan command

$ terraform plan
provider.aws.region
......
........
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:
......
..........