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:

Posted on Leave a comment

Deploy between different environments with variable groups – Azure DevOps

Group variables is a functionality provided by Azure pipelines that let one handle a lot of variables as one entity. It also supports key vault integration but also secrets on comparison with the standard pipeline variables which should not be used as secrets according to Microsoft, as their value can be seen.

https://docs.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=yaml

In this article I will describe how you can use variable groups for different deployments based on the environment you work. For example the company may want to differentiate the variables for a product on the test and production version. This could be injected and handled on the pipeline accordingly with group variables and parameters.

This article refers to a product which has the same variables (version, password, environment) on test and prod, but their values are different.

In order to create a variable group you should go to Pipelines -> Library -> +Variable group.

The values which I provided on prod variable group are below:

Accordingly the same values exist for test.

The pipeline will trigger when a commit is merged on the main branch.

trigger:
- main

parameters:
  - name: environment
    displayName: Where to deploy?
    type: string
    default: test
    values:
    - prod
    - test


pool:
  vmImage: ubuntu-latest

variables:
 - group: ${{parameters.environment}}

steps:

- script: |
    echo $(ENV)
    echo $(VERSION)
  displayName: Step 1 - print version and environment

- script: echo $(PASSWORD)
  displayName: Step 2 - print secret

- script: pwd ENV ${{parameters.environment}}
  displayName: Step 3 - print parameter
  
  

By running the pipeline the type of environment will be asked

Depending on the selected input, the group variables of the specific category will be printed. If I run with test as input then I will get the test version and env variable.