0
0
Terraformcloud~5 mins

What is Infrastructure as Code in Terraform - CLI Guide

Choose your learning style9 modes available
Introduction
Infrastructure as Code lets you create and manage computer resources using files instead of clicking buttons. This helps you set up servers, networks, and storage automatically and repeatably.
When you want to create a new server without manually logging into a cloud console.
When you need to set up the same environment multiple times for testing or production.
When you want to keep track of your infrastructure changes like you do with code.
When you want to avoid mistakes from manual setup by automating the process.
When you want to share your infrastructure setup with teammates easily.
Config File - main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "example-instance"
  }
}

This file tells Terraform to use the AWS provider in the us-east-1 region. It creates one small server (t2.micro) using a common Amazon Machine Image (AMI). The tags section names the server for easy identification.

Commands
This command sets up Terraform in your folder by downloading the AWS provider plugin. It prepares Terraform to work with your configuration.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes required for your infrastructure.
This command shows what Terraform will do before making any changes. It helps you check your setup and avoid mistakes.
Terminal
terraform plan
Expected OutputExpected
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: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + tags = { + "Name" = "example-instance" } } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
This command creates the server as described in the file. The -auto-approve flag skips the confirmation step to speed up the process.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-0abcdef1234567890] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval to apply changes immediately
This command displays the current state of your infrastructure as Terraform sees it. It confirms the server was created.
Terminal
terraform show
Expected OutputExpected
aws_instance.example: id = i-0abcdef1234567890 ami = ami-0c55b159cbfafe1f0 instance_type = t2.micro tags = { Name = example-instance }
Key Concept

If you remember nothing else from this pattern, remember: Infrastructure as Code lets you create and manage your servers and resources using files so you can automate and repeat your setups safely.

Common Mistakes
Not running 'terraform init' before other commands
Terraform needs to download provider plugins first; without init, commands fail.
Always run 'terraform init' once before planning or applying changes.
Applying changes without reviewing the plan
You might create or destroy resources unintentionally.
Run 'terraform plan' first to see what will happen before 'terraform apply'.
Using incorrect or outdated AMI IDs
The server creation will fail if the AMI ID does not exist in the chosen region.
Use a valid AMI ID for your AWS region; check AWS console or documentation.
Summary
Write a Terraform file to describe your infrastructure resources.
Run 'terraform init' to prepare Terraform with needed plugins.
Use 'terraform plan' to preview changes before applying.
Apply changes with 'terraform apply' to create or update resources.
Check the current infrastructure state with 'terraform show'.