Terraform by a Movie example🎦
Tags: terraform, devops, cloud, 2023ready
Published: 2023-01-13 19:13:54


![Terraform](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oonio96rhvczygvhwzkf.gif)

Lets decode this highly demanding skill of 2023 in Cloud/Devops world🌎

As always we will take a layman example to understand this concept.
Let’s take an example of Movie Watching 🎦🍿🎥

You can watch a Movie in 2 ways:
1. Open your door, take your car, go to theater, buy a ticket & watch🫡
2. Be at home, watch it on Netflix,Prime Video😎

NOTE: Let’s keep our focus on the end result of you watching a movie.
In both ways, you will be able to achieve the task.

The Second type is what Terraform is.Let me explain how😲

In the cloud, we need to create various infrastructures like servers, DB, Security groups,VPC & 100 like them.

Again, we have 2 ways of creating it, same like watching a movie:

Normal way: Open your respective Cloud Login page, do all the authentications, search which service you want,click click click click and lots of click🥵
Terraform way: Write a script in YAML with all your requirements & RUN…boom Done 🚀
Just like the movie you watched is owned by the Netflix company.
Terraform is owned by HashiCorp company🏭

Also just like on Netflix, you can watch all the movies present in it.
Using Terraform, you can create infra on any Cloud like AWS,GCP + 2780 as they have collaborated with Terraform🥳

By having connection with so many Cloud Providers, terraform helps us to be cloud platform independent & gives us freedom to be multi-cloud☁️☁️

And all this is done, by writting declarative code in a .tf file & when we run it, our Cloud Infrastructure is created🧞‍♂️

That’s why Terraform is called “IaaC tool (Infrastructure as a Code)”⚙️

Note: Terraform is of declarative type, meaning you just write/declare what you want as End result & it will take care of how to do it.
Like create 2 aws server or 3 DB, 2 pods. So it’s not imperative, it’s declarative.

Lets understand now, how this magic happens🪄🔮🧙🏼‍♂️

In any team, a Developer will create a project and a Devops/Cloud guy will create the needed infrastructure on cloud to run that project smoothly on cloud.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tclu59xoulsja1sfhb5j.png)

In above example, we have used “AWS” as “Provider” & its different Resources like ami,instance_type for creation.

So once script is ran, we can login to our AWS account & we will be able to see that a ec2 server instance is running in our AWS account.
To connect your Aws account, just configure AWS CLI access credentials of your AWS account on your local machine.

Architecture of Terraform:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5b8krz55uyuh60ks637p.jpg)

Now Terraform Architecture, can be divided into 2 set:

⭐[SCRIPT,STATE,CORE]
The “script/config file” you write, is the result you desire & “State” is the actual state at present.Core of terraform creates a execution PLAN & takes care that ‘Desired’ state = ‘Actual’ state by creating, updating or deleting things.

⭐[POVIDERS]
The Cloud “Providers” like AWS, Azure have tied up with Terraform are 2nd component of Terraform architecture⚓

“AWS” being a “Provider” for Terraform, we can use all AWS Resources like we can create an AWS EC2 server, then deploy Kubernetes cluster on that server then create services inside that cluster.

Just like AWS, we have 100 more such Cloud Providers, that brings their 1000 resources with them.

For example, below you can see we have used “Kubernetes” as a “Provider” & are using Kubernetes different Resources to create a Namespace.
(My Blog on Namespace using a cupboard example)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2it5pxfnljc585o3mrsq.png)

Terraform Commands for Different Stages:



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tx6fc1dqrey1kjpny70l.png)

terraform refresh: to know the condition of current state, later to be matched to desired state.
terraform init: initialize terraform directory, pull down providers as written by you in .tf file here its AWS.
terraform plan: creates plan for making Desired state == Current state
terraform apply: execute the plan created by above step.
Also as now our Infrastructure is written in a script, we can maintain athe versions of this script by storing it in git just like we do for our project codes. And changes can now be tracked down as who did the change in infra and also will be needed approved by raising PR to a Team Lead , thats why if there is any issue on Infra comes, we can roll back to any paticular earlier version.

So at the end Terraform is just like our Netflix, where we can see any movie without actually going into theaters & we have options for multi-watching.

Same way Terraform helps us to create infra on any cloud without logging into its portal & just by writting a chunk of code and it gives us option to go multi-cloud.

Have given Terraform Certified Associate exam and below is my Cert⭐

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmc28hawi0ogs7qrg57d.png)

We have just scratched the surface, but Thank you for being till here with me, if you feel u have learnt something new, let me know for sure, it will means a world to me 🙌🏻

And if you share same interest as me in decoding these complex topics in an easy way them lets connect🙋🏻‍♂️