Terraform Notess

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

 Local values - Assigns a name to expression which can be used multiple times by calling the

name

 Var.auto.tfvars is 1st preference, terraform.tfvars - 2nd, variables.tf is 3rd

 Terraform state list

 Terraform validate

 Terraform Drift

Page 1: Introduction to Terraform

What is Terraform?

 Terraform is an open-source infrastructure as code (IaC) tool created by HashiCorp.

 It allows you to define and provision infrastructure using a high-level configuration language
called HashiCorp Configuration Language (HCL).

Key Concepts:

 Provider: A plugin that interacts with APIs of cloud platforms or services (e.g., AWS, Azure,
Google Cloud).

 Resource: A component of your infrastructure (e.g., VM instances, databases).

 State: A file that keeps track of the current state of your infrastructure.

 Plan: An execution plan that shows what changes Terraform will make to your infrastructure.

Basic Commands:

bash

terraform init # Initialize a Terraform working directory

terraform plan # Generate and show an execution plan

terraform apply # Apply the changes required to reach the desired state of the configuration

terraform destroy # Destroy the Terraform-managed infrastructure

Page 2: Setting Up Your Environment

Install Terraform:

 Download Terraform from the official website.

 Unzip the downloaded file and move the Terraform binary to a directory included in your
system's PATH.

Initializing a Terraform Project:

1. Create a directory for your project:

bash

mkdir my-terraform-project
cd my-terraform-project

2. Create a main configuration file:

hcl

# main.tf

provider "aws" {

region = "us-west-2"

3. Initialize the directory:

bash

terraform init

Page 3: Working with Providers and Resources

Providers:

 Providers are responsible for managing the lifecycle of resources. They define how Terraform
interacts with cloud services.

Example - AWS Provider:

hcl

provider "aws" {

region = "us-west-2"

Resources:

 Resources describe the components of your infrastructure.

Example - AWS EC2 Instance:

hcl

resource "aws_instance" "example" {

ami = "ami-0c55b159cbfafe1f0"

instance_type = "t2.micro"

tags = {

Name = "example-instance"

}
Page 4: Variables and Outputs

Variables:

 Variables allow you to parameterize your configuration.

Example - Defining Variables:

hcl

variable "instance_type" {

description = "Type of EC2 instance"

type = string

default = "t2.micro"

Example - Using Variables:

hcl

resource "aws_instance" "example" {

ami = "ami-0c55b159cbfafe1f0"

instance_type = var.instance_type

tags = {

Name = "example-instance"

Outputs:

 Outputs are used to display values after the apply step.

Example - Defining Outputs:

hcl

output "instance_id" {

description = "ID of the AWS EC2 instance"

value = aws_instance.example.id

Page 5: Modules

What are Modules?


 Modules are reusable configurations. A module is a container for multiple resources that are
used together.

Creating a Module:

1. Create a directory for the module:

bash

mkdir -p modules/my-module

2. Define Resources in the Module:

hcl

# modules/my-module/main.tf

resource "aws_instance" "example" {

ami = "ami-0c55b159cbfafe1f0"

instance_type = "t2.micro"

tags = {

Name = "example-instance"

3. Use the Module in Your Configuration:

hcl

module "my_module" {

source = "./modules/my-module"

Page 6: Managing State

State Files:

 Terraform maintains the state of your infrastructure in a file called terraform.tfstate.

 It's crucial to manage state files properly, especially in a team environment.

Remote State:

 Store state files remotely for better collaboration and backup.

Example - Configuring Remote State in S3:

hcl

terraform {
backend "s3" {

bucket = "my-terraform-state"

key = "path/to/my/key"

region = "us-west-2"

Page 7: Provisioners

Provisioners:

 Provisioners execute scripts on a local or remote machine as part of the resource creation or
destruction.

Example - Using a Local Provisioner:

hcl

resource "aws_instance" "example" {

ami = "ami-0c55b159cbfafe1f0"

instance_type = "t2.micro"

provisioner "local-exec" {

command = "echo 'Hello, World!'"

Page 8: Data Sources

Data Sources:

 Data sources allow you to fetch data from external sources for use in your configuration.

Example - Using a Data Source:

hcl

data "aws_ami" "latest_amazon_linux" {

most_recent = true

owners = ["amazon"]

filter {

name = "name"
values = ["amzn-ami-hvm-*"]

Using Data Source in Resource:

hcl

resource "aws_instance" "example" {

ami = data.aws_ami.latest_amazon_linux.id

instance_type = "t2.micro"

Page 9: Managing Workspaces

Workspaces:

 Workspaces allow you to manage multiple environments (e.g., dev, staging, production)
using the same configuration.

Example - Creating and Switching Workspaces:

bash

terraform workspace new dev

terraform workspace select dev

Using Workspaces in Configuration:

hcl

resource "aws_instance" "example" {

count = terraform.workspace == "prod" ? 3 : 1

ami = "ami-0c55b159cbfafe1f0"

instance_type = "t2.micro"

Page 10: Advanced Terraform Features

Terraform Cloud:

 Terraform Cloud provides collaboration, governance, and automation features for teams
using Terraform.

 You can create workspaces, manage state, and apply configurations in a shared environment.

Example - Configuring Terraform Cloud:

hcl

terraform {
cloud {

organization = "my-org"

workspaces {

name = "my-workspace"

Managing Secrets:

 Use tools like HashiCorp Vault or environment variables to manage sensitive information like
API keys or passwords.

Example - Using Environment Variables:

bash

export TF_VAR_db_password="mysecretpassword"

By following these notes and examples, you should have a comprehensive understanding of
Terraform's core features

You might also like