In Terraform, the init
, plan
, and apply
commands are fundamental to the workflow of managing infrastructure as code. Let me explain each one:
terraform init
:This command is used to initialize a Terraform working directory. When you start working on a new or existing Terraform configuration, you run
terraform init
to set up the environment.During initialization, Terraform downloads the necessary providers and modules, creating a
.terraform
directory to store the configuration and plugin binaries.Example:
terraform init
terraform plan
:After initialization, the
terraform plan
command is used to create an execution plan. This plan describes what Terraform will do to reach the desired state defined in your configuration.It doesn't make any actual changes to your infrastructure. It's a dry run that helps you understand what Terraform will do before applying the changes.
Example:
terraform plan
terraform apply
:Once you've reviewed the plan and are satisfied with the proposed changes, you can apply them to your infrastructure using
terraform apply
.This command will prompt you to confirm that you want to apply the changes. If you provide the
-auto-approve
flag, it will apply the changes without asking for confirmation.Example:
terraform apply
or
terraform apply -auto-approve
These commands together form the basic Terraform workflow:
init
: Initialize your working directory.plan
: Preview changes before applying them.apply
: Apply the changes, making the actual modifications to your infrastructure.
here i am creating a EC2 Instance - by script
provider "aws" {
region = "us-east-1" # Set your desired AWS region
}
resource "aws_instance" "example" {
ami = "ami-0fc5d935ebf8bc3bc" # Specify an appropriate AMI ID
instance_type = "t2.micro"
}
Use Terraform destroy command to delete the created resources directly.
Tfstate file -
It's important to note that Terraform keeps track of the current state of your infrastructure in a state file. The state file is used to determine what changes need to be applied and to track the actual state of your infrastructure. The Terraform state file, often named terraform.tfstate
, is a crucial component of Terraform's functionality. It serves several important purposes in managing infrastructure as code (IaC). Here's an overview of its uses:
State Tracking:
- The primary purpose of the state file is to keep track of the current state of your infrastructure. This includes details about the resources that Terraform manages, their current configuration, and any metadata necessary for tracking changes.
Resource Mapping:
- The state file maintains a mapping between the resources defined in your Terraform configuration and the corresponding resources in your cloud provider or infrastructure platform.
Dependency Resolution:
- Terraform uses the state file to understand the dependencies between resources. This information is crucial when planning and applying changes to ensure that resources are created or modified in the correct order.
Concurrency Control:
- The state file helps prevent conflicts in a multi-user or collaborative environment. It contains a lock mechanism that prevents multiple users from making changes to the same infrastructure simultaneously. This ensures that changes are applied in a controlled and orderly manner.
Output Values:
- The state file also stores the values of output variables defined in your Terraform configuration. Output values can be useful for extracting information from your infrastructure, such as IP addresses or resource identifiers.
Back-End Configuration:
- If you're using a remote back-end (such as AWS S3, Azure Storage, or Terraform Cloud), the state file may include information about the back-end configuration, like the location of the remote state.
Sensitive Data Management:
- The state file can contain sensitive information, such as passwords or private keys. While Terraform is designed to handle sensitive data securely, it's crucial to be aware of what is stored in the state file and take appropriate precautions.
State Manipulation:
- Terraform commands like
terraform apply
andterraform destroy
use the state file to determine the current state of your infrastructure and make the necessary changes. The state file is updated after each successful operation.
- Terraform commands like
happy learning... :) Thanks for reading..