Terraform Command Examples (with examples)
1. Initializing a new or existing Terraform configuration
Command:
terraform init
Motivation: Initializing a Terraform configuration is the first step before using any other Terraform command. It downloads the necessary provider plugins and sets up the backend for storing the Terraform state.
Explanation:
The terraform init
command initializes the current working directory as a Terraform configuration. It ensures that all the required provider plugins are available and downloads them if necessary. It also initializes the backend to store the Terraform state, which tracks the infrastructure’s current state.
Example Output:
Initializing the backend...
Initializing provider plugins...
- Using terraform-provider-aws v3.56.0
- Using terraform-provider-null v2.1.2
- Using terraform-provider-random v3.1.0
Terraform has been successfully initialized!
2. Verifying the syntactical validity of configuration files
Command:
terraform validate
Motivation: It is essential to validate Terraform configuration files to identify any syntax errors or configuration issues before deploying the infrastructure. This allows catching errors early in the development process.
Explanation:
The terraform validate
command verifies the syntax and configuration of Terraform files in the current directory. It checks for any syntax errors, required variable definitions, missing resources, etc. It helps ensure that the configuration files are valid and ready for deployment.
Example Output:
Success! The configuration is valid.
3. Formatting configuration files according to Terraform conventions
Command:
terraform fmt
Motivation:
Maintaining a consistent and readable code style is crucial for collaboration and code maintainability. The terraform fmt
command automatically formats the Terraform configuration files based on Terraform’s language style conventions.
Explanation:
The terraform fmt
command formats the Terraform configuration files by applying standard formatting rules. It adjusts indentation, spacing, and line breaks to make the code more readable and consistent. It helps improve code quality and ensures that the configuration files follow best practices.
Example Output:
Formatted main.tf
4. Generating and showing an execution plan
Command:
terraform plan
Motivation:
Before making any changes to the infrastructure, it is essential to understand the impact of those changes. The terraform plan
command helps generate an execution plan that shows the resources to be created, changed, or destroyed.
Explanation:
The terraform plan
command analyzes the current state of the infrastructure and the desired state defined in the configuration files. It calculates the changes required to move from the current state to the desired state. The command generates an execution plan and displays the planned actions without making any changes to the infrastructure.
Example Output:
Terraform will perform the following actions:
# aws_instance.example will be created
+ resource "aws_instance" "example" {
...
}
Plan: 1 to add, 0 to change, 0 to destroy.
5. Building or changing infrastructure
Command:
terraform apply
Motivation:
The terraform apply
command is used to create or modify infrastructure resources based on the defined Terraform configuration. It allows deploying the desired state configuration and provisioning infrastructure resources.
Explanation:
The terraform apply
command reads the Terraform configuration files, compares the desired state with the current state, and determines which resources need to be created, modified, or destroyed. It then prompts for confirmation before proceeding with the changes and deploys the infrastructure accordingly.
Example Output:
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.example will be created
+ resource "aws_instance" "example" {
...
}
Plan: 1 to add, 0 to change, 0 to destroy.
...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
6. Destroying Terraform-managed infrastructure
Command:
terraform destroy
Motivation:
There may be instances when the infrastructure resources are no longer needed or the environment needs to be cleaned up. The terraform destroy
command allows destroying all the resources managed by Terraform.
Explanation:
The terraform destroy
command reads the Terraform state and analyzes the resources currently managed by Terraform. It generates a plan to destroy all the resources and prompts for confirmation before proceeding. It then removes the infrastructure resources that were created using Terraform.
Example Output:
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# aws_instance.example will be destroyed
- resource "aws_instance" "example" {
...
}
Plan: 0 to add, 0 to change, 1 to destroy.
...
Destroy complete! Resources: 1 destroyed.
By using these Terraform command examples, you can efficiently create, manage, and destroy infrastructure as code. Whether it’s initializing a new configuration, validating the syntax, or generating an execution plan, Terraform provides a robust set of commands to streamline the deployment and management of your infrastructure.