Ravindra BagaleCourses & study guides मराठी

Chapter 33: Terraform Providers Resources Variables Outputs Locals and Data Sources

Chala mitrano, Terraform cha pahila khara paya. Provider mhanje AWS shi bolnara plugin, resource mhanje aapan banavto ti vastu, variable mhanje input, output mhanje result, local mhanje ekda lihilela naav, aani data source mhanje already asleli vastu fakta vachaychi. Pratyek topic var lab aahe. Shevti destroy visru naka.

What you will learn in this chapter

  • Pin the AWS provider with a ~> 5.0 constraint and a lock file
  • Write resources and reference them
  • Declare variables with validation and load tfvars
  • Print outputs and mark sensitive ones
  • Use locals for names and tags
  • Read existing infrastructure with data sources

Concepts in this chapter

  1. 33.1Providers — the plugin that talks to AWS
  2. 33.2Resources — the things Terraform creates
  3. 33.3Variables and tfvars — one code, many inputs
  4. 33.4Outputs — values you hand to people and other tools
  5. 33.5Locals — name a value once, use it everywhere
  6. 33.6Data sources — read what already exists

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 Persistent Systems, the software engineering company, working from Nagpur. Kavita Shinde leads the team, and we look after the project timesheet app. Costs are counted in INR. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Persistent Systems is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.

0 / 12

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-south-1".

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 = "persistent-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.

Easy Input variable

Why: Variables make one module usable for many environments.

What to do: Write a variable "instance_type" with type = string and default = "t3.micro".

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 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 Read an output

Why: Scripts read outputs like the load balancer address.

What to do: Print only the raw value of the output lb_dns.

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 Name the practice

Why: IaC is the base of repeatable cloud setups.

What to do: Persistent Systems 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 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 = "project-timesheet".

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

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 = 3, 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 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 = "persistent-tfstate", key = "prod/terraform.tfstate", region = "ap-south-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.

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