Terraform-a01-Introduction

What is Terraform

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently.
It is open source and declarative

The differences between declarative and imperative:
declarative: define WHAT the result you want
imperative: define exact steps - HOW

The key features:

  • Infrastructure as Code
  • Execution Plan
  • Resource Graph
  • Change Automation

A. Previsioning infrastructure:

  1. private network space
  2. ec2 server instances
  3. install Docker and other tools
  4. security

B. Deploy applications

Difference between Ansible and Terraform

Both: Infrastructure as a Code

Ansible

Better for configuring that infrastructure

Mainly a configuration tool
More mature

Terraform

Better for infrastructure

Mainly infrastructure provisioning tool
More advanced in orchestration

How does Terraform work

  1. create execution plan

  2. execute plan with providers

Configuration

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.5.0"
}
}
}

provider "google" {

credentials = file("<NAME>.json")

project = "<PROJECT_ID>"
region = "us-central1"
zone = "us-central1-c"
}

resource "google_compute_network" "vpc_network" {
name = "terraform-network"
}

Terraform Block

it defines which provider to download from Terraform Registry

Provider

It is used to configure the named provider

Resource

It contains the resource type, and the resource name.

resource ID: resource type and resource name together

Commands

refresh:
query provider to get the current state

plan:
create an execution plan

apply:
execute the plan

destroy:
destroy the resources/infrastructure