Ravindra BagaleCourses & study guides मराठी

Chapter 38: Terraform CLI Commands fmt validate plan apply state and more

Chala mitrano, aaj Terraform che sagle commands ek ek karun. fmt, validate, plan, show, apply, destroy, state, output, -replace, refresh-only aani graph. Pratyek command cha output kasa disto, hirva aani laal, te baghu.

What you will learn in this chapter

  • fmt and validate in every pipeline
  • Saved plans, show and -detailed-exitcode
  • apply, destroy and why -target is rare
  • state list, show, mv, rm and backups
  • output for scripts
  • -replace instead of taint
  • refresh-only and graph

Concepts in this chapter

  1. 38.1fmt and validate — the free checks
  2. 38.2plan, show and saved plan files
  3. 38.3apply and destroy — and why -target is an emergency tool
  4. 38.4state list, show, mv and rm
  5. 38.5output — read results from state
  6. 38.6taint versus -replace
  7. 38.7refresh-only and graph

The chapter recap is at the end of the last concept page.

Practice Tasks

About this scenario

Suppose we are on the DevOps team at Airbnb, the travel stays company, working from San Francisco. Sophia Martinez leads the team, and we look after the host payout service. Costs are counted in USD. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Airbnb is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.

0 / 12

Easy Format the code

Why: Standard formatting keeps reviews about logic, not spaces.

What to do: Format every .tf file in this folder and its sub-folders.

How:
  1. Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
  2. Write the tool name, then the sub-command (the action word).
  3. Add the flags the task needs, each followed by its value.
  4. Add names, files or paths at the end, then click Check answer.

Easy Initialise a Terraform folder

Why: init downloads providers and sets up the backend before anything else works.

What to do: Initialise the Terraform working folder.

How:
  1. Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
  2. Write the tool name, then the sub-command (the action word).
  3. Add the flags the task needs, each followed by its value.
  4. Add names, files or paths at the end, then click Check answer.

Easy Validate the config

Why: validate catches typos before you talk to the cloud.

What to do: Check the configuration for syntax and reference errors.

How:
  1. Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
  2. Write the tool name, then the sub-command (the action word).
  3. Add the flags the task needs, each followed by its value.
  4. Add names, files or paths at the end, then click Check answer.

Easy Output a value

Why: Outputs hand values to scripts and other modules.

What to do: Write output "web_ip" with value = aws_instance.web.public_ip.

How:
  1. Write the block type (resource, variable, output, module...) and its labels in quotes.
  2. Open { and write each argument as name = value.
  3. Put text values in double quotes; numbers and true/false without quotes.
  4. Close every block with } and click Check answer.

Easy Name the practice

Why: IaC is the base of repeatable cloud setups.

What to do: Airbnb describes its servers, networks and databases in text files kept in Git and creates them with a tool. What is this called (three-letter short form)?

How:
  1. Write down the facts or numbers the task gives you.
  2. Work it out step by step.
  3. Type only the final answer (one word or number) in the box.
  4. Click Check answer.

Medium Save a plan file

Why: Applying a saved plan applies exactly what was reviewed.

What to do: Create a plan and save it to the file airbnb.tfplan.

How:
  1. Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
  2. Write the tool name, then the sub-command (the action word).
  3. Add the flags the task needs, each followed by its value.
  4. Add names, files or paths at the end, then click Check answer.

Medium Plan with a variable value

Why: The same code builds dev and prod with different values.

What to do: Run a plan with the variable region set to us-west-1 and using the file prod.tfvars.

How:
  1. Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
  2. Write the tool name, then the sub-command (the action word).
  3. Add the flags the task needs, each followed by its value.
  4. Add names, files or paths at the end, then click Check answer.

Medium Apply the saved plan

Why: No surprise changes: only the reviewed plan runs.

What to do: Apply the saved plan file airbnb.tfplan.

How:
  1. Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
  2. Write the tool name, then the sub-command (the action word).
  3. Add the flags the task needs, each followed by its value.
  4. Add names, files or paths at the end, then click Check answer.

Medium List resources in state

Why: state list shows what Terraform thinks it manages.

What to do: List every resource address in the state.

How:
  1. Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
  2. Write the tool name, then the sub-command (the action word).
  3. Add the flags the task needs, each followed by its value.
  4. Add names, files or paths at the end, then click Check answer.

Hard Plan one resource only

Why: During an incident you may need to touch only one resource.

What to do: Plan only the resource aws_instance.web.

How:
  1. Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
  2. Write the tool name, then the sub-command (the action word).
  3. Add the flags the task needs, each followed by its value.
  4. Add names, files or paths at the end, then click Check answer.

Hard Remote state in S3

Why: Shared remote state with locking stops two people breaking each other.

What to do: Write a terraform block with backend "s3": bucket = "airbnb-tfstate", key = "prod/terraform.tfstate", region = "us-west-1", dynamodb_table = "tf-lock".

How:
  1. Write the block type (resource, variable, output, module...) and its labels in quotes.
  2. Open { and write each argument as name = value.
  3. Put text values in double quotes; numbers and true/false without quotes.
  4. Close every block with } and click Check answer.

Hard Import an existing bucket

Why: Import brings hand-made resources under code control.

What to do: Import the existing S3 bucket airbnb-logs into the address aws_s3_bucket.logs.

How:
  1. Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
  2. Write the tool name, then the sub-command (the action word).
  3. Add the flags the task needs, each followed by its value.
  4. Add names, files or paths at the end, then click Check answer.

Ravi Sir's Cards: 17 revision, interview and comparison cards for this chapter · मराठी cards