Terraform CLI Commands

Some common CLI commands to work with Terraform.

Lets consider this basic example of creating two ec2 instance resource and displaying the public IP of the instances as output.

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "4.16.0"
    }
  }
}

provider "aws" {
  # Configuration options
  region = "ap-southeast-2"
}

provider "aws" {
  # Configuration options
  region = "us-east-1"
  alias = "usa"
}



resource "aws_instance" "webserver_au" {
  instance_type = "t2.micro"
  ami = "ami-0e35fc52b166effec"
  key_name = "aukeypair2022"
  security_groups = ["sshSG"]
  tags = {
    "Name" = "webserver"
  }
}

resource "aws_instance" "webserver_usa" {
  provider = aws.usa
  instance_type = "t2.micro"
  ami = "ami-005de95e8ff495156"
  key_name = "useast12022pem"
  security_groups = ["usawebserver"]
  tags = {
    "Name" = "webserver"
  }
}


output "webserver_au" {

  value=aws_instance.webserver_au.public_ip

}


output "webserver_usa" {

  value=aws_instance.webserver_usa.public_ip

}
  • To display all outputs
$ terraform output
webserver_au = "13.211.160.82"
webserver_usa = "54.242.30.209"
  • To list the resources in the current state file:
$ terraform state list
aws_instance.webserver_au
aws_instance.webserver_usa
  • To get details of the particular resource from state file:
$ terraform state show aws_instance.webserver_au
# aws_instance.webserver_au:
resource "aws_instance" "webserver_au" {
    [1m[0mami[0m[0m                                  = "ami-0e35fc52b166effec"
    [1m[0marn[0m[0m                                  = "arn:aws:ec2:ap-southeast-2:602760531384:instance/i-07da2b9924f6b7f0b"
    [1m[0massociate_public_ip_address[0m[0m          = true
    [1m[0mavailability_zone[0m[0m                    = "ap-southeast-2b"
    [1m[0mcpu_core_count[0m[0m                       = 1
    [1m[0mcpu_threads_per_core[0m[0m                 = 1
    [1m[0mdisable_api_termination[0m[0m              = false
    [1m[0mebs_optimized[0m[0m                        = false
    [1m[0mget_password_data[0m[0m                    = false
    [1m[0mhibernation[0m[0m                          = false
    [1m[0mid[0m[0m                                   = "i-07da2b9924f6b7f0b"
    [1m[0minstance_initiated_shutdown_behavior[0m[0m = "stop"
    [1m[0minstance_state[0m[0m                       = "running"
    [1m[0minstance_type[0m[0m                        = "t2.micro"
  • To list the providers used
$ terraform providers

Providers required by configuration:
.
└── provider[registry.terraform.io/hashicorp/aws] 4.16.0

Providers required by state:

    provider[registry.terraform.io/hashicorp/aws]
  • To taint a resource [ to be replaced on the next "terraform apply"]

    $ terraform taint aws_instance.webserver_usa
    Resource instance aws_instance.webserver_usa has been marked as tainted.
    
  • To replace a particular resource

    $ terraform apply -replace aws_instance.webserver_usa
    [0m[1maws_instance.webserver_au: Refreshing state... [id=i-07da2b9924f6b7f0b][0m
    [0m[1maws_instance.webserver_usa: Refreshing state... [id=i-0cb226110619967e6][0m