Posted on Leave a comment

Create multiple environments with Terraform modules – App service Azure example

In this article I will demonstrate how one can create different environments for development needs through terraform modules. Modules provide great extensibility and code reuse. In this example I will use an appservice resource on Azure cloud.

The result of the deployment will be two different resource groups with two app services. The code of the demonstration is located at the bottom of the page.

You should first init your module. Navigate to the module folder and perform init

cd modules; terraform init
cd .. ; terraform init

Then validate your terraform code

terraform validate

The last step is to apply your configuration

terraform apply

You may encounter an error during the creation of the resources because of the app service name. It should be globally unique.

Change your name and perform a terraform apply again. Terraform will read your .tfstate file and will only implement the differences on infrastructure.

After the successful run you will see the green result output from terraform cli.

On Azure there should be two resource groups created. One for prod environment and one for test.

Inside each resource group there should be a different app service the one that it is created through the module according with the settings provided.

Production app service plan

GitHub repository:

https://github.com/geralexgr/terraform-module-environments-deploy

Video tutorial on YouTube: