Terraform Basics for Cloud Automation and Infrastructure as Code
This video explains how to use Terraform to automate cloud infrastructure provisioning and ensure consistent, repeatable deployments.
What this guide covers
After reading this guide, you will be able to write, apply, and manage a basic Terraform configuration to automate AWS cloud infrastructure provisioning declaratively. You’ll move away from manual console clicks to consistent, versioned infrastructure as code.
When to use it
- Setting up a new EC2 instance or other AWS resources repeatedly across environments
- Preventing configuration drift caused by manual changes in cloud consoles
- Version-controlling your cloud infrastructure definitions alongside application code
- Auditing and reviewing infrastructure changes in code before deployment
The move, step by step
- Install Terraform: Download and install Terraform from https://terraform.io/downloads based on your OS. Verify with
terraform version. - Create a Terraform config file: Make a file named
main.tfwith a resource block:resource "aws_instance" "web" { ami = "ami-12345678" instance_type = "t2.micro" tags = { Name = "ExampleInstance" } } - Initialize Terraform: Run
terraform initin your project directory to download the AWS provider and prepare your working folder. - Plan the deployment: Run
terraform planto see what changes Terraform will make on AWS without applying them yet. - Apply the changes: Run
terraform applyto provision your EC2 instance as defined. Confirm withyes. - Manage and update: Modify your
main.tffile to change instance properties, then repeatterraform planandterraform applyto update infrastructure. - Version control your config files: Add your
.tffiles to git so that all infrastructure changes are tracked and auditable. - Destroy test resources: When done testing, run
terraform destroyto clean up resources created by Terraform.
Example
Input main.tf:
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t2.micro"
tags = {
Name = "ExampleInstance"
}
}
Commands:
terraform init
terraform plan
terraform apply
Expected output snippet after terraform apply:
aws_instance.web: Creating...
aws_instance.web: Creation complete after 30s [id=i-0abcd1234efgh5678]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Common mistakes
- Mistake: Using console clicks to change resources after applying Terraform → Fix: Always update
.tffiles and re-apply to prevent drift. - Mistake: Forgetting to run
terraform initbefore commands → Fix: Runterraform initonce per project directory before planning or applying. - Mistake: Hardcoding AMI IDs without confirming they exist in your AWS region → Fix: Verify AMI availability for your chosen region to avoid errors.
- Mistake: Not committing Terraform files to version control → Fix: Use git or another VCS to track infrastructure changes alongside code.
Next step
Download Terraform and create your first main.tf file with the example EC2 resource block above. Then run terraform init, terraform plan, and terraform apply to deploy it on AWS. After that, come back and try the next move from the video.
Pick the smallest version of this guide and try it in your tool of choice in the next 20 minutes.
Get the next AI/career guide in your inbox
One short, practical guide on AI tools, cloud, and the modern career stack. No fluff.