DEV Community

Venkatesh K
Venkatesh K

Posted on • Originally published at venkatesh111.hashnode.dev on

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

  • 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

  1. 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.

  1. 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.

  1. 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

  1. Dependency Management :
  • Terraform ensures dependent resources are created or updated before resources that rely on them to avoid issues.
  1. Concurrency Control :
  • Terraform manages resource operation concurrency to prevent conflicts and ensure consistency.
  1. State Management :
  2. 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

  1. 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

  • After you type yes to terraform destroy prompt, terraform will start destroying resources mentioned in the plan

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

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

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

Let's connect and explore Terraform and AWS.

References :

Top comments (0)