Terraform - Basics

Github link: https://github.com/Manoj-14/devops-cicd-resources/tree/master/terraform
Tool from Hashicorp
Infrastructure as code (IaC)
Terraform maintains the state of infrastructure and it knows that what is the instance state i.e maintaining the state of infrastructure
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):
Env vars
terraform.tfvars
*.auto.tfvars (alphabetical order)
-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 codeterraform init
-> it scan all the tf file and if data block exists it will fetch it from the terraform registryterraform validate
-> check whether syntactically correct or notterraform plan
-> it gives what will be the impact of resource that we executeterraform 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
Backend (remote):
- Apply the configuration and store the state file in remote backend
S3
not on Git
- Apply the configuration and store the state file in remote backend
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 informationthis is to avoid the
race condition
during state locking mention Partition as
LockID
with typestring
if not it will throw error
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