Popular Terraform Commands for Cloud Provisioning
Tags: aws, 100daysofcode, tutorial, codenewbie
Published: 2020-11-21 04:20:00

* Let's use the following HCL script for provisioning multiple EC2 instances, creating a VPC with a VPN and also launching an S3 bucket on AWS Cloud. 

Although the following script is meant specifically for AWS cloud, the terraform commands have the same usage for all cloud platforms.

```tf
provider "aws" {
  region = "ap-south-1"
}

resource "aws_instance" "nish" {
  ami           = "ami-02b5fbc2cb28b77b8"
  count         = 2
  instance_type = "t2.micro"
  tags = {
    Name = "noicecurse"
  }
}

resource "aws_s3_bucket" "nish" {
  bucket = "shiftnoicecurse" # name must be universally unique for all AWS users
  acl    = "private"
}

resource "aws_vpc" "vpc" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_vpn_gateway" "vpn_gateway" {
  vpc_id = "vpc-0a5a4d4f01bc6769e" #hardcoding default vpc id
}

resource "aws_customer_gateway" "customer_gateway" {
  bgp_asn    = 65000
  ip_address = "172.0.0.1"
  type       = "ipsec.1"
}

resource "aws_vpn_connection" "main" {
  vpn_gateway_id      = aws_vpn_gateway.vpn_gateway.id
  customer_gateway_id = aws_customer_gateway.customer_gateway.id
  type                = "ipsec.1"
  static_routes_only  = true
}
```

> It is assumed that the AWS credentials are stored locally by using AWS-CLI.

### Commands

#### 1. Fmt

```
$ terraform fmt
``` 

Format the HCL script into canonical form for increased readability.

* Before format

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ymytficnj4m11d1qdx8n.png)

* After format

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9vrt1e494erc7bu4v4ml.png)

#### 2. Init

```
$ terraform init
```

Install the plugins and dependencies mentioned for the provider.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3awopu8u35fhjhign62q.png)

#### 3. Validate

```
$ terraform validate
```

Validate syntax and find errors in the HCL script.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3s4rr8049vat72tqdpuk.png)

#### 4. Plan

```
$ terraform plan
```

See the changes that would occur on provisioning the infrastructure

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pkfbbi3yzztba6yg5ayd.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7a87g10rghsola3ky3q2.png)

#### 5. Graph

```
$ terraform graph -h # help
$ terraform graph -draw-cycles # visual help for debugging
$ terraform graph -type=plan # type of graph ranging from plan, destroy, apply etc.
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x934fclmzsvxvd0dym6x.png)

#### 6. Apply

```
$ terraform apply
$ terraform apply -auto-approve # to skip user interaction
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l3d5xlcks1mxk3adgmez.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qf049v23sbn0dw88eht7.png)

#### 7. Destroy

```
$ terraform destroy
$ terraform destroy -auto-approve # to skip user interaction
```

**Note:** If you are getting started with your journey in the world of Terraform, I highly recommend you to focus on destroying resources. In my experience, I have left resources after creating them, and ended up spending a lot of capital on cloud resources. 

For an in-depth coverage around Terraform destroy, I highly recommend reading this blog by [Spacelift](https://spacelift.io/blog/how-to-destroy-terraform-resources).