Ravindra BagaleCourses & study guides मराठी

DevOps

Chapter 12: Terraform Introduction

Friends, today we start Terraform — Infrastructure as Code. If you create a server with console clicks, you cannot build the same thing again the same way. When you write it as code, Git can review it, you see a plan, and when the lab is done you destroy it. This is the intro. Chapters 33 to 43 go deep on every topic. Do not worry.

What you will learn in this chapter

  • Why Infrastructure as Code beats console clicks
  • Install Terraform and confirm your AWS identity
  • The init, plan, apply, destroy workflow
  • Where Terraform stops and Ansible starts
  • A map of the deep Terraform chapters 33 to 43

Where this goes next. Chapter 33 covers providers, resources, variables, outputs, locals and data sources. Chapter 34 covers state, the S3 backend and workspaces. Chapter 35 covers modules. Chapter 36 covers count, for_each, dynamic, lifecycle and depends_on. Chapter 37 covers import, moved and removed. Chapter 38 covers every CLI command. Chapter 39 covers provisioners and secrets. Chapter 40 is the AWS lab. Chapter 41 covers environments and Jenkins. Chapter 42 covers drift and troubleshooting. Chapter 43 has interview questions and the capstone.

Concepts in this chapter

  1. 12.1Why IaC — console clicks versus code
  2. 12.2Install Terraform and check the version
  3. 12.3The core workflow — init, plan, apply, destroy
  4. 12.4Terraform versus Ansible — build the house, then furnish it

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 Atlassian, the developer software company, working from Sydney. Liam O'Connor leads the team, and we look after the issue tracker webhook service. Costs are counted in AUD. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Atlassian 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 Provider block

Why: The provider tells Terraform which cloud and region to talk to.

What to do: Write a provider "aws" block with region = "ap-southeast-2".

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 S3 bucket resource

Why: Buckets in code are reviewed and never forgotten.

What to do: Write a resource aws_s3_bucket named logs with bucket = "atlassian-logs" and tags { Team = "devops" }.

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.

Medium Pin the provider version

Why: Pinning stops a new provider release from breaking your plan.

What to do: Write a terraform block with required_providers aws = { source = "hashicorp/aws", version = "~> 5.0" }.

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.

Medium EC2 instance using a variable

Why: Referencing var.* avoids hard-coding sizes.

What to do: Write resource aws_instance "web" with ami = "ami-0abcdef1234567890", instance_type = var.instance_type and tags Name = "issue-tracker-webhook".

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.

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 atlassian.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 ap-southeast-2 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.

Hard Create several copies with count

Why: count builds N identical resources from one block.

What to do: Write resource aws_instance "worker" with count = 4, ami = "ami-0abcdef1234567890", instance_type = "t3.small" and tags Name = "worker-${count.index}".

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 Security group for HTTPS

Why: Opening only 443 keeps the attack surface small.

What to do: Write resource aws_security_group "web" with name = "web-sg" and one ingress block: from_port = 443, to_port = 443, protocol = "tcp", cidr_blocks = ["0.0.0.0/0"].

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 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.

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