Terraform Commands

Terraform Commands
  1. Initialize Terraform:

     terraform init
    
    • Initializes the working directory containing Terraform configuration files. This command downloads the necessary provider plugins.
  2. Validate Terraform configuration:

     terraform validate
    
    • Checks the syntax and validity of the Terraform configuration files.
  3. Create or update infrastructure:

     terraform apply
    
    • Applies the changes specified in the Terraform configuration to create or update the infrastructure.
  4. 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.
  5. Destroy infrastructure:

     terraform destroy
    
    • Destroys the infrastructure defined in the Terraform configuration files. Use with caution, as it will remove all resources.
  6. Output values:

     terraform output
    
    • Displays the output values defined in the Terraform configuration.
  7. 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.
  8. 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.
  9. 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.
  10. 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 !!