Terraform - Basics

Manoj MManoj M
2 min read

Github link: https://github.com/Manoj-14/devops-cicd-resources/tree/master/terraform

  1. Tool from Hashicorp

  2. Infrastructure as code (IaC)

  3. Terraform maintains the state of infrastructure and it knows that what is the instance state i.e maintaining the state of infrastructure

  4. To perform actions on AWS make sure you configured access keys in you cmd line using aws configure

Data types in variables

  • number

  • string

  • boolean

  • list

  • map

  • objects

  • tuples -> list of primitive mentioned in type ([number, string, number] = [12,'Terraform',23])

  • set -> no duplicate list

  • can use from terraform.tfvars

  • Var def orders (lower to higher priority):

    1. Env vars

    2. terraform.tfvars

    3. *.auto.tfvars (alphabetical order)

    4. -var or -var-file (cmd line flags)

Provisioners

  • Provisioners in terraform are like terraform will execute it but can't manage the state (can't maintain)

Commands

  • terraform fmt -> format the code

  • terraform init -> it scan all the tf file and if data block exists it will fetch it from the terraform registry

  • terraform validate -> check whether syntactically correct or not

  • terraform plan -> it gives what will be the impact of resource that we execute

  • terraform apply -> to run the code and it will git the plan first and then it will ask us whether it is ok to store the output in terraform state if yes only it will run.

  • terraform destroy -> destroy every resource that have created

Output Attribute

  •     output "instance_ip_addr" { 
            value = aws_instance.server.public_ip 
        }
    
  • Elements => resourceType.resourceName.attributeName resourceType = aws_instance
    resourceName = server
    attributeName = public_ip (this will present in state file)

Backend - s3

  • Don't store the state file in SCM it may have sensitive info's

  • We can store it in s3 and terraform have integration for s3

State file Management

  1. Backend (remote):

    • Apply the configuration and store the state file in remote backend S3 not on Git
  2. State Locking:

    • It will help when 2 are more persons are using the state file at the same time

    • DynomoDB is the one which is used to store the locking information

    • this is to avoid the race condition

    • during state locking mention Partition as LockID with type string if not it will throw error

0
Subscribe to my newsletter

Read articles from Manoj M directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manoj M
Manoj M

Software Engineer with 2 years of experience developing scalable full-stack applications and managing cloud native infrastructure. Proficient in Java, Spring Boot, micro-services architecture, and modern frontend frameworks including React and Angular. Experienced in containerisation with Docker and Kubernetes, implementing robust CI/CD pipelines, and deploying applications on AWS. Strong background in translating business requirements into technical solutions and collaborating with cross-functional teams to deliver enterprise-grade applications