Terraform Commands for your first cloud provisioning

Terraform is a System Provisioning tool developed by HashiCorp which is primarily used IaC (Infrastructure as Code) tool for managing Cloud platforms and its associated services. Since terraform is used mainly for cloud management here are 5 popular commands used for setting up and managing some services.

  1. terraform init
    It’s the first command you need to execute. This command performs several different initialization steps in order to prepare a working directory for use. This command is naturally very important as no commands pertaining to our cloud can be executed without initialization.
A look at init running

2. terraform plan
It’s a command used prior to actually applying the configurations over the cloud. It’s an important feature of Terraform that allows a user to see which actions Terraform will perform prior to making any changes, increasing confidence that a change will have the desired effect once applied.

A sample output of terraform plan

3. terraform apply
The bread and butter of terraform the actual command to execute the plan, setup your services and that’s it. By default, apply scans the current directory for the configuration and applies the changes appropriately. If no explicit plan file is given on the command line, terraform apply will create a new plan automatically and prompt for approval to apply it

terraform apply in action

4. terraform destroy
Now we come to the antithesis of the apply command ; the terraform destroy. Our exit button, the shutdown button to applied plans. The terraform destroy command terminates resources defined in your Terraform configuration. This command is the reverse of terraform apply in that it terminates all the resources specified by the configuration. It does not destroy resources running elsewhere that are not described in the current configuration.

type yes and it’s gone

5. terraform validate
Finally we come the terraform validate command. A very handy command for beginners it allows for syntax check of the Terraform files. A syntax check is done on all the Terraform files in the directory and will display an error if any of the files don’t validate. A thing to remember though the syntax check does not cover every syntax common issues.

Rights and Wrongs by the Validation of validate

For the people still not clear about the configuration files here is an sample which can be used just by changing the credentials in the file.

provider "aws"{
region = "ap-south-1"
profile = "spiner"
}
resource "aws_instance" "basicOs" {
count = "2"
ami = "ami-0447a12f28fddb066"
instance_type = "t2.micro"
key_name = "basicKey_spiner"
security_groups = ["basicSecurity"]
tags = {
Name = "firstOS -${ count.index + 1}"
}
}

This is all about how to begin using terraform for cloud infrastructure provisioning.