# Learning Terraform -IAC Tool

#### **Infrastructure as Code (IaC)**

Before IaC, infrastructure management was manual, time-consuming, and error-prone. System admins had to configure servers manually, leading to inconsistencies, lack of version control, and minimal automation.

IaC solves these challenges by introducing a systematic, automated, and code-driven approach to managing infrastructure. Popular IaC tools include Terraform, AWS CloudFormation, and Azure Resource Manager. These tools ensure consistency, automation, and efficiency in infrastructure management.

#### **Why Terraform?**

* **Multi-Cloud Support** – Write once, deploy anywhere (AWS, Azure, GCP, or on-premises).
    
* **Vast Ecosystem** – Pre-built modules and providers from HashiCorp and the community.
    
* **Declarative Syntax** – Define the desired infrastructure state, making it easier to manage.
    
* **Seamless Integration** – Works with Docker, Kubernetes, Ansible, Jenkins, and other DevOps tools.
    

#### **Prerequisites**

Before using Terraform, ensure you understand the cloud provider (AWS/Azure) where you'll deploy resources. Without this, managing infrastructure with Terraform will be challenging.

#### **Hands-on: Deploying an EC2 Instance**

To start, set up Terraform and AWS CLI on your local machine. Alternatively, use **GitHub Codespaces** (free for 60 hours) to configure and install Terraform, AWS CLI, and other required plugins.

Next, we'll create a basic **EC2 instance** using Terraform.\\.

Once you set up all these things create a directory and create a ‘main.tf’ file this is the file where we write our terraform code .

In the main.tf file the first thing we do we explain the terraform where you going to automate infrastructure its on AWS or Azure or any other cloud .

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739945536194/fd589982-fe90-4cbe-b455-3e3508293017.png align="center")

After this you need to add the resource block which tells what resource you need to create

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739945725570/696bcdb4-7fb0-4614-aafb-fc8cbee4e754.png align="center")

So now you got the question how do we remember this script so no need to worry just google

‘terraform aws’ there you find Hashicorp has given the brilliant examples for every resource you create on AWS or any other cloud you can refer those examples and start practicing the templates

Now how to initialize terraform and create the infrastrucuture on AWS. Before that you have to authenticate terraform to connect your AWS account.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739946081742/d5a50724-b9d8-457a-893c-addb0740eb32.png align="center")

Terraform has these four lifecycles:

a) Terraform Init : it will initializes the terraform configuration ,authenticate with AWS do the provider configuration.

b) Terraform Plan : it will show you the dry-run before creating the infrastructure what it will going to create

c) Terraform Apply : It will apply the configuration.

d) Terraform destroy: Deletes the configuration.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739946767035/cbe34af9-76a2-4c39-94f3-07916e090d02.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739946801067/22ccece2-24fd-4685-9eac-95b899fd0000.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739946853018/b3b8bdda-4d1d-4b50-a330-aed0b4f81a8c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739946880996/1efbf168-d1fb-4946-8f9a-bd04e53dc21c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739946939292/59521940-c193-4a8b-aff6-80e23f9834b5.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739946992877/28380e09-53f9-4d4e-88c1-74819c1c3eb2.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1739947068926/4243016b-77a9-4e47-b5a7-ff12fe7aeff4.png align="center")

**Whats Next ?**

We’ll learn about providers,multi region/cloud variables and conditions. Stay Tuned..

#LearnIacwithMe#terraform#infrastructureAutomation#devops#cloudcomputing
