Setting up Terraform Remote State
--
Introduction:
When working with Terraform as a team, it is always ideal to set up a remote state as multiple people want to update the same state file and it is not really easy to work with multiple copies of divergent state files. One of the best features of setting up Remote state, along with the fact that a single state file is accessed and used by all the team members is that the state file comes with a locking mechanism. The remote state setup can be achieved by setting up backends specific to the cloud. For example, s3 is the backend used for AWS whereas storage accounts are used in Azure
Creating the resources needed to setup Remote State:
The first and foremost thing that most first timers miss (including myself when I started working on a remote state initially) is that, the resources which will be used as the backend, for instance, the s3 bucket and the dynamoDB or the Azure storage container, have to be created initially either with terraform (recommended) or manually in order to set up a remote backend in AWS or Azure
Requirements for setting up a remote state in Azure:
Azure uses storage accounts and stores the state as a blob with a key within the container of the storage account. This blob container also supports state locking and consistency checking with the help of native blob storage capabilities.
The following three resources have to be created initially in order to set up a remote state for Terraform in Azure
- Resource group which contains the storage account. It is ideal to be different than the resource group in which all the needful resources are created
- Storage account which will contain the blob storage container within which the remote state file will be located
- Blob storage container which stores the remote file along with providing the locking facility so that only one person can access the state file (run terraform plan or apply) at the same time thus ensuring the state file is not corrupted.
resource "azurerm_resource_group" "resource_group" {
name = "backend_rg"
location = "us-west-2"
}resource "azurerm_storage_account" "az_backend" {
name = "azbackend"…