DEV Community

Cover image for Popular Terraform Commands for Cloud Provisioning
Nishkarsh Raj
Nishkarsh Raj

Posted on • Updated on

Popular Terraform Commands for Cloud Provisioning

  • 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.

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
}
Enter fullscreen mode Exit fullscreen mode

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

Commands

1. Fmt

$ terraform fmt
Enter fullscreen mode Exit fullscreen mode

Format the HCL script into canonical form for increased readability.

  • Before format

Alt Text

  • After format

Alt Text

2. Init

$ terraform init
Enter fullscreen mode Exit fullscreen mode

Install the plugins and dependencies mentioned for the provider.

Alt Text

3. Validate

$ terraform validate
Enter fullscreen mode Exit fullscreen mode

Validate syntax and find errors in the HCL script.

Alt Text

4. Plan

$ terraform plan
Enter fullscreen mode Exit fullscreen mode

See the changes that would occur on provisioning the infrastructure

Alt Text

Alt Text

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.
Enter fullscreen mode Exit fullscreen mode

Alt Text

6. Apply

$ terraform apply
$ terraform apply -auto-approve # to skip user interaction
Enter fullscreen mode Exit fullscreen mode

Alt Text

Alt Text

7. Destroy

$ terraform destroy
$ terraform destroy -auto-approve # to skip user interaction
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)