Skip to content

Latest commit

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Basic Terraform

A minimal Terraform project that fetches live GitHub API data, applies conditional logic, and writes a local file — exercising the core HCL primitives in one coherent workflow.

TerraformProvider: httpProvider: local

Overview

This project demonstrates Terraform fundamentals — data sources, locals, conditional expressions, resources, and outputs — using a single practical example: querying the GitHub API for the hashicorp/terraform repository's star count and writing a verdict to a local file based on a popularity threshold.

Intended for engineers learning Infrastructure as Code concepts or reviewing HCL syntax for the first time.

Highlights

  • Live external data — uses the http data source to call the GitHub REST API at plan/apply time, with no hardcoded values
  • Conditional resource content — the written file's content is determined by a ternary expression over the fetched star count, demonstrating dynamic resource configuration
  • End-to-end primitive coverage — a single ~18-line configuration exercises data source, locals, resource, and output in sequence
  • Zero-auth, zero-setup — the GitHub API call is unauthenticated, requiring no tokens or credentials to run

Features

Data Fetching

  • HTTP GET to the GitHub API (/repos/hashicorp/terraform) at apply time
  • Response body parsed inline with jsondecode

Conditional Logic

  • locals block extracts stargazers_count from the decoded response
  • Ternary expression branches on a 10,000-star threshold to produce different file content

Output

  • stars output variable surfaces the live count in the terminal after apply
  • repo_info.txt written to the working directory with the evaluated verdict

Tech Stack

LayerTechnologyPurpose
IaC languageHashiCorp Configuration Language (HCL)Define infrastructure declaratively
RuntimeTerraform ~> 1.10Plan, apply, and destroy infrastructure
Provider: httphashicorp/http ~> 3.4Fetch data from external HTTP endpoints
Provider: localhashicorp/local ~> 2.5Write files to the local filesystem

Architecture

flowchart LR
A[GitHub REST API] -->|http data source| B[stargazers_count]
B --> C{over 10000 stars?}
C -->|yes| D[repo_info.txt: popular]
C -->|no| E[repo_info.txt: not popular]
B --> F[output: stars]
Loading

How It Works

  1. Initterraform init downloads the hashicorp/http and hashicorp/local provider plugins.
  2. Data fetch — on terraform plan or apply, the http data source sends a GET request to https://api.github.com/repos/hashicorp/terraform.
  3. Parse — a locals block decodes the JSON response body and extracts stargazers_count.
  4. Branch — a conditional expression compares the count against 10,000 to select one of two strings.
  5. Write — the local_file resource writes the result to repo_info.txt in the working directory.
  6. Output — the stars output variable prints the raw count to the terminal.

Setup

Prerequisites

Run

# Download required providers
terraform init
# Preview the execution plan (no changes applied)
terraform plan
# Apply — fetch live data and write repo_info.txt
terraform apply
# Tear down managed resources
terraform destroy

Usage

After terraform apply, the terminal shows the live star count:

Outputs:
stars = 43218

And repo_info.txt in the working directory contains:

This repo is popular!

If the star count were below 10,000, the file would contain:

This repo is not as popular.

Key Decisions

DecisionRationaleTradeoff
Unauthenticated GitHub API callZero configuration needed — no tokens, no environment variablesSubject to GitHub's 60 requests/hour unauthenticated rate limit
10,000-star thresholdRound number that cleanly separates popular repos from lesser-known ones for demo purposesArbitrary; a real use case would parameterize this as an variable
local_file resource over output aloneDemonstrates a write-side resource, not just read/output primitivesAdds a file to the working directory that must be cleaned up with terraform destroy

About

Built to get hands-on with Terraform's core primitives — providers, data sources, locals, resources, and outputs — in a self-contained example that produces observable, real-world output without requiring cloud credentials.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages