Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle. Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features.
Terraform creates and manages resources on cloud platforms and other services through their application programming interfaces (APIs). Providers enable Terraform to work with virtually any platform or service with an accessible API. HashiCorp and the Terraform community have already written more than 1700 providers to manage thousands of different types of resources and services, and this number continues to grow. You can find all publicly available providers on the Terraform Registry, including Amazon Web Services (AWS), Azure, Google Cloud Platform (GCP), Kubernetes, Helm, GitHub, Splunk, DataDog, and many more.
The core Terraform workflow consists of three stages:
Write: You define resources, which may be across multiple cloud providers and services. For example, you might create a configuration to deploy an application on virtual machines in a Virtual Private Cloud (VPC) network with security groups and a load balancer.
Plan: Terraform creates an execution plan describing the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration.
Apply: On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies. For example, if you update the properties of a VPC and change the number of virtual machines in that VPC, Terraform will recreate the VPC before scaling the virtual machines.
Usage: terraform [global options] "subcommand" [args]
The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
Main commands:
init Prepare your working directory for other commands
validate Check whether the configuration is valid
plan Show changes required by the current configuration
apply Create or update infrastructure
destroy Destroy previously-created infrastructure
All other commands:
console Try Terraform expressions at an interactive command prompt
fmt Reformat your configuration in the standard style
force-unlock Release a stuck lock on the current workspace
get Install or upgrade remote Terraform modules
graph Generate a Graphviz graph of the steps in an operation
import Associate existing infrastructure with a Terraform resource
login Obtain and save credentials for a remote host
logout Remove locally-stored credentials for a remote host
output Show output values from your root module
providers Show the providers required for this configuration
refresh Update the state to match remote systems
show Show the current state or a saved plan
state Advanced state management
taint Mark a resource instance as not fully functional
test Experimental support for module integration testing
untaint Remove the 'tainted' state from a resource instance
version Show the current Terraform version
workspace Workspace management
Global options (use these before the subcommand, if any):
-chdir=DIR Switch to a different working directory before executing the
given subcommand.
-help Show this help output, or the help for a specified subcommand.
-version An alias for the "version" sub-command.
##### Installation of Terraform
```
# install hashicorp tap
brew tap hashicorp/tap
# install terraform
brew install hashicorp/tap/terraform
# to upgrade to the latest version
brew update
brew upgrade hashicorp/tap/terraform
```
##### Install autocomplete on zshell
```
terraform -install-autocomplete
```
##### Installing Azure Cli
```
brew update && brew install azure-cli
```
##### Azure cli setup
```
# login to azure and find all the subscriptions to which you have access.
az login
az account list
"""
[
{
"cloudName": "AzureCloud",
"homeTenantId": "0envbwi39-home-Tenant-Id",
"id": "35akss-subscription-id",
"isDefault": true,
"managedByTenants": [],
"name": "Subscription-Name",
"state": "Enabled",
"tenantId": "0envbwi39-TenantId",
"user": {
"name": "your-username@domain.com",
"type": "user"
}
}
]
"""
# Find the id column for the subscription account you want to use.
# Once you have chosen the account subscription ID, set the account with the Azure CLI
az account set --subscription "35akss-subscription-id"
az account set --subscription "1392852c-9a42-4a73-a639-8bf403d32008"
```
##### Terraform Commands
##### To get help with terraform
```
terraform -help
# add any subcommands to terraform -help for example:
terraform -help plan
terraform -help init
terraform -help apply
```
##### Initialize Terraform Configuration
```
terraform init
```
##### Format and validate the Configuration
```
terraform fmt
terraform validate
```
##### Plan
```
terraform plan
```
##### Apply Terraform Configuration
```
terraform apply
terraform apply -auto-approve
```
##### Inspect Terraform State
```
terraform show
```
##### Destroy resources managed by Terraform project
```
terraform destroy
```
##### Passing variables via command line
```
terraform apply -var 'resource_group_name=Some_RG_Name' -var 'vm_size=Standard_B2s'
```
##### References
```
https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/azure-get-started
https://docs.microsoft.com/en-us/azure/developer/terraform/create-linux-virtual-machine-with-infrastructure
```