[go: up one dir, main page]

0% found this document useful (0 votes)
5 views4 pages

Terraform Guide

The document provides an overview of Terraform's architecture, including its components such as the CLI, providers, resources, modules, and state files. It outlines the Terraform workflow from writing configurations to applying and destroying infrastructure, and explains how to use modules for reusable configurations. Additionally, it compares Terraform modules to Python functions and describes how to define and use parameters in modules, along with the creation of a terraform.tfvars file for variable values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Terraform Guide

The document provides an overview of Terraform's architecture, including its components such as the CLI, providers, resources, modules, and state files. It outlines the Terraform workflow from writing configurations to applying and destroying infrastructure, and explains how to use modules for reusable configurations. Additionally, it compares Terraform modules to Python functions and describes how to define and use parameters in modules, along with the creation of a terraform.tfvars file for variable values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Terraform Basics and Module Usage

1. Terraform Architecture

Terraform has a client-centric architecture and is built on the following components:

• Terraform CLI: The command-line tool used to execute configurations.


• Providers: Plugins that allow Terraform to interact with cloud platforms like AWS, Azure, or GCP.
• Resources: Infrastructure elements defined in .tf files (e.g., EC2 instance).
• Modules: Reusable units of Terraform configurations.
• State Files: Track the current state of the deployed infrastructure.
• Backend: Defines where Terraform state is stored (locally or remotely).

2. Terraform Workflow

1. Write: Define resources using HCL.


2. Initialize: terraform init to set up providers and backends.
3. Plan: terraform plan shows the execution plan.
4. Apply: terraform apply provisions the infrastructure.
5. Destroy: terraform destroy removes all managed resources.

3. Terraform Code Blocks

Key Terraform configuration blocks:

provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "example" {


ami = "ami-123456"
instance_type = "t2.micro"
}

Other blocks include:

• variable for input values


• output for returning values
• module to include external code

1
4. Terraform Modules

Modules are folders with multiple configuration files grouped together for reuse.

Example structure:

project/
├── main.tf
├── variables.tf
├── outputs.tf
├── modules/
│ └── ec2_instance/
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf

5. Terraform State Files

• Named terraform.tfstate
• Tracks real infrastructure state
• Essential for plan and apply operations
• Should be stored remotely in team environments (e.g., S3)

6. Comparing Terraform Modules with Python Functions

Python Function:

def create_vm(name, type):


return f"VM: {name}, Type: {type}"

Terraform Module:

# modules/ec2_instance/main.tf
resource "aws_instance" "this" {
ami = var.ami
instance_type = var.instance_type
}

Similarities:

• Accept parameters

2
• Produce outputs
• Promote reuse

7. How to Call a Terraform Module

In main.tf :

module "web" {
source = "./modules/ec2_instance"
ami = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"
}

8. What Are Parameters in Modules

Parameters (variables) are declared using:

# modules/ec2_instance/variables.tf
variable "ami" {}
variable "instance_type" {}

Used inside resources as:

resource "aws_instance" "this" {


ami = var.ami
instance_type = var.instance_type
}

9. How to Create terraform.tfvars File

A file to define variable values:

ami = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"

Terraform automatically loads this file or you can apply it with:

3
terraform apply -var-file="terraform.tfvars"

You might also like