Day-11-Terraform-AWS
Dependencies:
- Implicit
- Explicit
1- Implicit: implicit dependency means that Terraform uses the information in your code to understand how resources relate to each other and creates them in the right order automatically.
2-Explicit: Terraform Explicit Dependency is when you tell Terraform directly which resources depend on others, so it knows the order to create or manage them.
we can create 3 backets then we can see how terraform creates the s3 backets without Terraform Explicit Dependency.
first s3 backets will be created then after VPC was created. This is not a good way to create s3 backet then we can use Terraform Explicit Dependency.
Now we can use Terraform Explicit Dependency then create s3 backet.
Now we can see the first VPC will be created after S3 backets was created. This is a good way to create s3 backet with the Terraform Explicit Dependency.
Variables and TFVars for configuration management:
Using terraform.tfvars
allows you to externalize variable values, making your Terraform code more modular and reusable.
We can easily change values in terraform.tfvars
without modifying the main configuration, making your Terraform scripts more reusable and maintainable across different environments.
create_before_destroy :
We can mainly use the create_before_destroy
lifecycle rule in production environments. If the current Terraform infrastructure is running in production and we need to upgrade some services, we use this rule to ensure smooth transitions. By using create_before_destroy
, the new resources are created before the old ones are removed, which helps to avoid downtime during the update process. This way, the production environment remains available while the upgrades are being made.
prevent_destroy:
We mainly use prevent_destroy
to protect important resources from being deleted. This ensures that critical resources, like databases or servers in production, cannot be accidentally removed. It adds an extra layer of safety, making sure that even if a delete operation is attempted, Terraform will prevent it and show an error message. This helps maintain the stability of essential resources in a production environment.