Everyone Can Learn Program
中文 Start learning
Specialty · Cloud and SRELesson 1 of 5Free to read

Terraform basics · Cloud and Reliability

How do you create servers and storage with code instead of clicking?

Course contents

Why 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 address aws_s3_bucket.assets.
  • Variables make one file work for several environments: variable "env" { default = "staging" }, used as var.env or inside text as ${var.env}.
  • count makes several copies: count = 3 gives aws_instance.web[0], [1], [2]; inside, count.index is 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 lesson

Run 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
}
Step by step
  1. L1A variable with a default; another environment can pass a different value.
  2. L6Inside text, ${...} inserts a value: shop-assets-staging.
  3. L9Its address is aws_instance.web.
  4. L17After apply, Terraform prints the bucket's name.

Several copies with count

Run it in the lesson

count 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}"
  }
}
Step by step
  1. L1Two copies: aws_instance.web[0] and aws_instance.web[1].
  2. 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?

  1. AShows what would change, without changing anything
  2. BCreates the resources
  3. CDeletes everything not in the files
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?

  1. Aaws_instance.api[0] and aws_instance.api[1]
  2. Baws_instance.api1 and aws_instance.api2
  3. Capi.aws_instance[2]
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.

  1. PredictCount the resources

    On an empty account, how many resources will terraform plan create?

  2. 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 variable instance_type (default "t3.micro") for the server.

  3. WriteServers and a backup bucket

    Write Terraform for:

    • a variable replicas, default 3
    • aws_instance.api, one per replica (count), with ami = "ami-0abc123", instance_type = "t3.small" and a tag Name of api-0, api-1, …
    • aws_s3_bucket.backups with bucket = "shop-backups"
    • an output backup_bucket whose value is the bucket's name

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

TermChineseDefinition
Infrastructure as code基础设施即代码Cloud resources described in versioned files.
TerraformTerraformA tool that creates and updates cloud resources from files.
terraform planterraform planA preview of what Terraform would change.