Terraform Commands
Initialize Terraform:
terraform init
- Initializes the working directory containing Terraform configuration files. This command downloads the necessary provider plugins.
Validate Terraform configuration:
terraform validate
- Checks the syntax and validity of the Terraform configuration files.
Create or update infrastructure:
terraform apply
- Applies the changes specified in the Terraform configuration to create or update the infrastructure.
Show Terraform execution plan:
terraform plan
- Generates and shows an execution plan without making any actual changes to the infrastructure. Useful for reviewing changes before applying them.
Destroy infrastructure:
terraform destroy
- Destroys the infrastructure defined in the Terraform configuration files. Use with caution, as it will remove all resources.
Output values:
terraform output
- Displays the output values defined in the Terraform configuration.
Import existing resources into Terraform:
terraform import <resource_type>.<resource_name> <resource_id>
- Imports an existing resource into Terraform state. This helps manage resources that were created outside of Terraform.
Workspace management:
terraform workspace new <workspace_name> terraform workspace select <workspace_name> terraform workspace list terraform workspace delete <workspace_name>
- Manages workspaces, allowing you to have multiple isolated instances of your infrastructure.
State management:
terraform state show terraform state list terraform state pull terraform state push
- Manages Terraform state, which is used to track the current state of your infrastructure.
Format Terraform Files:
To format your Terraform configuration files.
terraform fmt
Variables and Input:
We can use variables to make our configurations dynamic. It's like parameters that we can change depending on the situation.
variable "region" {
default = "us-west-2"
}
Module Usage:
Modules help us organize our code and reuse configurations. Think of them as building blocks for our infrastructure.
module "vpc" {
source = "./modules/vpc"
}
Conditional Resources:
We can create resources conditionally based on certain criteria. It's like saying 'only create this if something else is true.'
"aws_instance" "example" {
count = var.create_instance ? 1 : 0
// Other instance configurations
}
Happy Learning !!
Subscribe to my newsletter
Read articles from Raqeeb Ahmed Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by