In this article, we are going to understand
What is Terraform resource block
Understand Terraform basic commands
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.
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
- 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
- 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.
- 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.
- 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
- Dependency Management :
- Terraform ensures dependent resources are created or updated before resources that rely on them to avoid issues.
- Concurrency Control :
- Terraform manages resource operation concurrency to prevent conflicts and ensure consistency.
- 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
-
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:
Execution: Terraform analyzes your configuration and the current state of your infrastructure, just like
terraform apply
.Resource Destruction: However, instead of creating or updating resources,
terraform destroy
focuses on destroying and deleting them.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.User Approval: You must confirm by typing " yes" when prompted, ensuring you're aware of the resources that are going to be deleted.
Execution: Once you confirm, Terraform executes the destruction, and you can see the progress in real-time.
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
orterraform 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.
Top comments (0)