# Terraform initialize 🔥

Today we will learn the purpose of basic Terraform commands which you'll use often

`terraform init`

* Initializes a new or existing Terraform configuration in the current working directory. This command initializes the Terraform environment, downloads provider plugins, and prepares the working directory for configuration.
    

`terraform init -upgrade:`

* Upgrades the provider plugins to their latest compatible versions during initialization.
    

`terraform plan`

* Generates an execution plan that shows what actions Terraform will take to reach the desired infrastructure state.
    

`terraform apply`

* It will apply the changes defined in your Terraform configuration to create, update, or delete resources to achieve the desired state.
    

`terraform validate`

* It will validate the syntax and structure of your Terraform configuration files. It's useful to help catch syntax errors, typos, or configuration issues before attempting to apply changes.
    

`terraform fmt`

* To automatically format your configuration files according to best practices.
    

`terraform destroy`

* It will destroy all resources managed by your Terraform configurations. It's essential to be cautious with this command, as it will permanently delete resources.
    

### **TASK:**

1. Create a file and write content inside using Terraform
    
2. Create a docker container using Terraform
    

<mark>Launch an EC2 instance</mark>

<mark>Create a directory</mark>

`mkdir Terraform_project`

/After creating directory you have to initialize the directory by executing  
`terraform init`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694795881525/71644c32-b2d6-4948-8b93-5d70e8b3b7fd.png align="center")

<mark>Syntax of Terraform</mark>

Resource block "Resource Type" "Resource name"

{

Arguments

}

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694794724234/229b5f69-5d81-424f-a383-35498a79288c.png align="center")

After saving this file execute `terraform apply`

It will give you the following output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694795578285/f20446e0-56bf-442e-a5ff-c9d3f90a0f04.png align="center")

2)Run a Docker container with an Nginx image

* First, need to install Docker
    

`apt install docker.io`

* we'll require a docker provider
    
* `vim provider.tf`  
    terraform { required\_providers { docker = { source = "kreuzwerker/docker" version = "3.0.2" } } }
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694796117433/349a2be3-9bb4-4057-815e-a91cb5b3740f.png align="center")

initialized that config file

`main.tf` with `terraform init`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694794750972/3941cef9-ca8f-4e16-91da-6c3367eb1404.png align="center")

Once you initialize execute `docker ps`

you will see no kind of container running

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694797007206/cdd65138-e878-42c9-8c5d-e970948d0c9a.png align="center")

Now terraform apply for previously configured main.tf

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694797145965/fe7e8149-0608-41d8-87af-9586e27ae8c9.png align="center")

<mark>Docker</mark> container is created

![Backhand index pointing down emoji clipart. Free download transparent .PNG  | Creazilla](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSBHE_9xf_ekBDQta77tCdfnCycDrGNoGQRzQ&usqp=CAU align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694797173891/57d12b41-5b13-45aa-b82d-2dbb840f3edb.png align="center")

<mark>Check Nginx container is working.<br>url:ipv4address:80</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694797191742/61ea4a81-8c6f-4ce7-a309-9b62b283c215.png align="center")

> "Thank you for reading my blog! Happy Learning!!!😊
> 
> Stay tuned for more DevOps articles follow me on **Hashnode** and connect on **LinkedIn** ([https://www.linkedin.com/in/ashish-karad-ba9630235/](https://www.linkedin.com/in/ashish-karad-ba9630235/))  
> *for the latest updates and discussions.*
