21 - Terraform Lab - By Houssem Dellai

 TERRAFORM - BY HOUSSEM DELLAI

  •  Install Terraform - Chocolatey on Windows
    • https://learn.hashicorp.com/tutorials/terraform/install-cli
    • https://terraform.io
  • Download Azure CLI
    • https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli
  • Create an Azure Account
  • Download Visual Studio Code 
    • https://code.visualstudio.com/download
    • Install Terraform Extension
  • Download Source Code/ Template 
    • https://github.com/HoussemDellai/terraform-course



Deploy Infra in the Cloud using Terraform

01 - Resource Group

vi main.tf
# valid for terraform version 0.13
# provider "azurerm" {
#   version = "=2.40.0"
#   features {}
# }

# valid for terraform version 0.14
provider "azurerm" {
  features {}
}

terraform {
  required_providers {
azurerm = {
  source  = "hashicorp/azurerm"
  version = "2.40.0"
}
  }
}

resource "azurerm_resource_group" "rg" {
  name     = "myFirstResourceGroup"
  location = "westeurope"
}


   Commands 
   # Login to Azure Cloud
   $ az login --use-device-code

   # get terraform version
  terraform version

   # get terraform commands
   terraform

   # init terraform's Azure provider (main.tf)
   terraform init

   # plan and preview terraform changes
   terraform plan

   # deploy terraform infra
   terraform apply

   # destroy infra
   terraform destroy

   # validate syntax
   terraform validate

   # Formatting codes
   terraform fmt


02 - Web App
       ** tranaform upgrade from 2.36 to 2.40
terraform init -upgrade

** create variable file 
=> variables.tf
variable "resource_group_name" {
default = "example-resource"
type = string
description = "RG name in Azure"
}
variable "resource_group_location" {
default = "West Europe"
type = string
description = "RG location in Azure"
}
variable "app_serevice_plan_name" {
default = "example-appserviceplan"
type = string
description = "Ap Service in Azure"
}
** modify main.tf
resource "azurerm_resource_group" "example" {
  name     = var.resource_group_name
  location = var.resource_group_location
.....
.....
- Overriding variable with .tfvars
  
  ** Another way to define variables
  => terraform.tfvars
resource_group_name     = "example-resource"
resource_group_location = "West Europe"
app_serevice_plan_name  = "example-appserviceplan"
app_service_name        = "terraform-demo"


- Output variables
  
  => output.tf
  output "webapp_url" {
    value = azurerm_app_service.example.default_site_hostname
  }

  output "webapp_ips" {
    value = azurerm_app_service.example.outbound_ip_addresses
  }

03 - Execution Plan

# plan and save the infra changes into tfplan file
terraform plan -out tfplan

# show the tfplan file
terraform show -json tfplan
terraform show -json tfplan >> tfplan.json

# apply the infra changes
terraform apply tfplan

# delete the infra
terraform destroy


  terraform init  ---->   terraform plan  ----> teraform apply
     main.tf                      .tfplan                     .tfstate
                            (changes)              (current state)
 
Terraform State -> when working as a team and every developer working on local machines and own tfstate file. it will start conflict if working same project with own tfstate file.
   
Solution => Centralized location of tfstate file and every developer access same file.
Lock this file during implementation apply
   How => 1. Create Resource Group
          2. Create Storage Account 
- RG name = terraform-rg
- Storage name = terraformrg
- location = Central US
- Peformance = standard 
Storage V2 -> Locally-redundant
  3. Go to Storage Account
Add - Containers
-> tfstate
 
4. create backend.tf file
terraform {
  backend "azurerm" {
resource_group_name   = "terraform-rg"
storage_account_name  = "terraformrg"
container_name  = "tfstate"
key  = "terraform.tfstate"
  }
}

5. terraform apply
    Acquiring state lock. This may take a few moments

- Deploy container service  ( AKS )

# plan and save the infra changes into tfplan file
terraform plan -out tfplan

# show the tfplan file
terraform show -json tfplan
terraform show -json tfplan >> tfplan.json
# show only the changes
cat tfplan.json | jq -r '(.resource_changes[] | [.change.actions[], .type, .change.after.name]) | @tsv'
cat tfplan.json | jq '[.resource_changes[] | {type: .type, name: .change.after.name, actions: .change.actions[]}]' 

# apply the infra changes
terraform apply tfplan

# delete the infra
terraform destroy

# cleanup files
rm terraform.tfstate
rm terraform.tfstate.backup
rm .terraform.lock.hcl
rm tfplan
rm tfplan.json
rm -r .terraform/



Comments

Popular posts from this blog

05 - Docker - Containers

08 - PUPPET - Configuration Management

06 - Docker Swarm - Container Orchestration