As I use Terraform more my love for it grows. In this
So enough talk lets get to it.
Configure the Storage Account
First, we need to create and configure the storage account. Bellow is a simple script that will do this for us. Just make sure you have logged in to Azure first using the az login command.
Copy the output at the end to something like the below, we will need this information later.
Configure the Terraform state backend
Now that we have created an Azure storage account and Container for our terraform states we need to create a file called backend.tfvars to store the backend config in.
In visual studio code copy the bellow.
Now under resource_group_name enter the name from the script. Do the same for storage_account_name, container_name and access_key.
For the Key value this will be the name of the terraform state file. For this example I am going to use tst.tfstate
Your backend.tfvars file will now look something like this.
In your Windows subsystem for Linux window or a bash prompt from within VS Code navigate to your terraform project folder that has the backend.tfvars file in and initiate Terraform using the following.
You will need to have your main.tf file
You can create multiple backend.tfvar files for each of your environments. So one for Test, Dev, and Prod.
If you are happy with them being in the same storage account and container you can just copy the backend
This will actually create you a folder under your Azure Storage container.
Then save it as a different file, such as prodbackend.tfvar. Then when you need to initialise Terraform for the different environments just change the file name to which one you are working with.
If you would like a separate storage container, storage account , etc then just use the above script and change the variables.
Thanks for reading and I hope you found this article helpful. If you have any questions reach out.