Terraform Resource Block
Published: 2023-10-30 15:00:23

---
title: Terraform Resource Block
published: true
date: 2023-10-29 08:46:43 UTC
tags: 
canonical_url: https://venkatesh111.hashnode.dev/terraform-resource-block
---

In this article, we are going to understand

1. What is Terraform resource block

2. Understand Terraform basic commands

3. Understand terraform resource behavior on executing terraform basic commands

## Terraform Resource Block

- A **Terraform Resource** is a fundamental unit used to model and manage infrastructure components.

- Each resource block describes one or more **_infrastructure objects_** that you want to create, modify, or manage.

- [**resource Syntax**](https://developer.hashicorp.com/terraform/language/resources/syntax)

- **Example of a Resource Block:**

## Terraform Resource Behaviors

Terraform resource behaviors refer to,

- How Terraform manages and interacts with resources in your infrastructure.

- These behaviors determine how resources are

1. **_Create_** :

* Terraform attempts to create resources in your target infrastructure based on your configuration.  

* Terraform creates resources that exist in the configuration but are not associated(present) with a real infrastructure object in the state

2. **_Destroy_** :

* Destroys resources that exist in the state/infra but no longer exist in the configuration.  

* Removing a resource from your Terraform configuration leads to the planned destruction of that resource in the infrastructure.


3. **_Update in-place_** :

* update the resources whose arguments have changed  

* Terraform detects differences between the desired state in your configuration and the current state in the infrastructure. It plans and applies changes to update resources accordingly.

4. **_Destroy and re-create_** :

* Terraform will destroy and re-create resources whose arguments have changed but which cannot be updated in-place due to remote API limitations  

* Example: Changing the Availability zone of an AWS EC2 instance

5. **_Dependency Management_** :

* Terraform ensures dependent resources are created or updated before resources that rely on them to avoid issues.  

6. **_Concurrency Control_** :

* Terraform manages resource operation concurrency to prevent conflicts and ensure consistency.

7. **_State Management_** :
* Terraform maintains a state file that records the current state of the infrastructure, which is used to plan and apply updates.  

### Understanding Terraform Resource Behavior with Example

- Let's Create an AWS EC2 instance and understand Terraform Resource(EC2) Behavior

- Let's Execute Terraform commands to understand resource behavior

6. `terraform destroy`: _destroy or delete_ Resources

- `terraform destroy` is like the " **off**" switch for your Terraform-managed infrastructure.

- It tells Terraform to **tear down and delete all the resources** in your infrastructure that were created or managed by Terraform.

- Let's understand `terraform destroy` in more detail:

1. **Execution:** Terraform analyzes your configuration and the current state of your infrastructure, just like `terraform apply`.

2. **Resource Destruction:** However, instead of creating or updating resources, `terraform destroy` focuses on **destroying and deleting** them.

3. **User Confirmation:** Similar to `terraform apply`, it shows you a summary of what it's about to destroy. It can be overridden with _auto-approve_.

4. **User Approval:** You must confirm by typing " **_yes_**" when prompted, ensuring you're aware of the resources that are going to be deleted.

5. **Execution:** Once you confirm, Terraform executes the destruction, and you can see the progress in real-time.

6. **Completion:** After the resources are destroyed, Terraform **provides a summary of what was deleted**.

- Example of `terraform destroy`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698568322166/066add30-a7dc-4dc1-ad14-1c43e8e37fe7.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698568329324/4ac55fde-f09d-41a8-9925-ba99ac88b576.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698568333860/1d7c0bf8-c375-432f-ad6e-49de5edec5ad.png)

- After you type **_yes_** to `terraform destroy` prompt, terraform will start **destroying** resources mentioned in the _plan_

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698568348930/b94fe4a7-9066-40cc-952d-87532b2a3c30.png)

- You should also be able to check on your AWS Console resource (EC2) being shutting down and getting ready for termination

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698568360124/53061bb0-650b-4966-9b1b-5e736a379fd8.png)

- Once terraform completes the execution you should be able to check on your AWS Console resource (EC2) is successfully terminated.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698568376406/36b985a9-1d29-493d-beba-1e3f114facbe.png)


## *terraform apply -auto-approve* and *terraform destroy -auto-approve*

* The ***\-auto-approve*** flag is an option that can be added to the terraform apply command to skip the confirmation step.
    
* When you use `terraform apply -auto-approve` or `terraform destroy -auto-approve`, **Terraform will not ask for your confirmation** and will **immediately apply** the changes described in the execution plan.
    
* This can be **useful for automation, scripting, or CI/CD pipelines** where manual confirmation is not possible
    
* However, **be cautious when using *\-auto-approve* in production environments**, as it can lead to unintended changes if the execution plan is not thoroughly reviewed.
    
* **Always review the execution plan carefully before using *\-auto-approve* command** to ensure that the changes are as expected and won't cause any issues in your infrastructure.
    
* ![tf apply auto-approve](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssjmg5yc2ztk9inj3df0.jpeg)


### Let's connect and explore Terraform and AWS.

- [X: Venkatesh111](https://twitter.com/venkatesh111)

- [LinkedIn: Venkatesh111](https://www.linkedin.com/in/venkatesh111/)

- [YouTube: LetUsCloud](https://www.youtube.com/@letuscloud)

### References :

- [Resource Syntax](https://developer.hashicorp.com/terraform/language/resources/syntax)

- [Resource Behavior](https://developer.hashicorp.com/terraform/language/resources/behavior)

- [Provisioning Infrastructure with Terraform](https://developer.hashicorp.com/terraform/cli/run)