Terraform basics · Cloud and Reliability
How do you create servers and storage with code instead of clicking?
Course contentsWhy you need it
Clicking through a cloud console works once. But nobody remembers which boxes were ticked, staging slowly drifts away from production, and rebuilding after a disaster takes days. Infrastructure as code describes servers, buckets and networks in files: reviewed like code, kept in Git, and applied the same way every time. Terraform is the most widely used tool for it.
The intuition
A Terraform file describes the resources you want:
resource "aws_s3_bucket" "assets" {
bucket = "shop-assets-${var.env}"
}resource "TYPE" "NAME": the type comes from the cloud provider; the name is yours. Together they form the addressaws_s3_bucket.assets.- Variables make one file work for several environments:
variable "env" { default = "staging" }, used asvar.envor inside text as${var.env}. - count makes several copies:
count = 3givesaws_instance.web[0],[1],[2]; inside,count.indexis 0, 1, 2. - Outputs print useful values after applying, like an address or a name.
You don't write steps; you write the end result. terraform plan compares it with what exists and shows what it would do:
+ aws_s3_bucket.assets will be created
Plan: 1 to add, 0 to change, 0 to destroy.terraform apply then does it. Press Run here to see the plan; nothing is created for real.
Definition
Infrastructure as code
Describing servers, networks and storage in files that are versioned, reviewed and applied by a tool.
Resource
One thing in the cloud that Terraform manages, addressed as TYPE.NAME (with [i] when it uses count).
Plan
Terraform's preview of what it will create (+), change (~), replace (-/+) or destroy (-).
Examples
A bucket and a server
Run it in the lessonRun it: on an empty account, everything will be created.
variable "env" {
default = "staging"
}
resource "aws_s3_bucket" "assets" {
bucket = "shop-assets-${var.env}"
}
resource "aws_instance" "web" {
ami = "ami-0abc123"
instance_type = "t3.micro"
tags = {
Name = "web-${var.env}"
}
}
output "bucket_name" {
value = aws_s3_bucket.assets.bucket
}- L1A variable with a default; another environment can pass a different value.
- L6Inside text, ${...} inserts a value: shop-assets-staging.
- L9Its address is aws_instance.web.
- L17After apply, Terraform prints the bucket's name.
Several copies with count
Run it in the lessoncount turns one block into numbered resources.
variable "web_count" {
default = 2
}
resource "aws_instance" "web" {
count = var.web_count
ami = "ami-0abc123"
instance_type = "t3.micro"
tags = {
Name = "web-${count.index}"
}
}- L1Two copies: aws_instance.web[0] and aws_instance.web[1].
- L100 for the first, 1 for the second: names web-0 and web-1.
Common mistakes
Changing things by hand too
A manual change in the console makes reality differ from the files (drift). The next plan will try to undo it. Change the files instead.
Hard-coding per-environment values
A bucket named shop-logs-staging can't be reused for production. Put what differs between environments in variables.
Applying without reading the plan
The plan is the safety check. Always read it, especially the lines with - and -/+, before typing yes.
Quick quiz
1. What does `terraform plan` do?
Show the answer
Answer: A
plan is a preview; apply makes the changes.
2. `resource "aws_instance" "api"` with `count = 2`. What are the addresses?
Show the answer
Answer: A
TYPE.NAME, with an index from 0 when count is used.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictCount the resources
On an empty account, how many resources will
terraform plancreate? - ModifyUse variables
This file only works for staging. Add a variable
env(default"staging") and use it in the bucket name (shop-logs-<env>), and a variableinstance_type(default"t3.micro") for the server. - WriteServers and a backup bucket
Write Terraform for:
- a variable
replicas, default3 aws_instance.api, one per replica (count), withami = "ami-0abc123",instance_type = "t3.small"and a tagNameofapi-0,api-1, …aws_s3_bucket.backupswithbucket = "shop-backups"- an output
backup_bucketwhose value is the bucket's name
- a variable
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Infrastructure as code | 基础设施即代码 | Cloud resources described in versioned files. |
| Terraform | Terraform | A tool that creates and updates cloud resources from files. |
| terraform plan | terraform plan | A preview of what Terraform would change. |