Jump to content

Featured Replies

Posted

Overview

 

Terraform is an open source infrastructure as code tool to create, change, and improve infrastructures. You can use Terraform to easily deploy Azure resources such as resource group, app service, storage account etc. This blog provides sample code and detailed instructions on the deployment of the standard Logic App infrastructure on Azure using Terraform in an Azure DevOps pipeline.

 

 

 

Terraform

 

 

You can clone this repo LogicApp-Terraform-Deploy from Github. Find the Terraform/LAstandard.tf and check the terraform code.

 

 

 

In this code you can see that we will be creating the following resources on Azure:

 

1. The resource group which your Logic App resources will de deployed to.

 

2. The storage account hosting the Logic App standard.

 

3. The app service plan.

 

4. The application insights and the associated log analytics workspace.

 

5. The standard Logic App

 

Example:

 

428x212vv2.png.1048512608cc20dcb1d67d41b94e621a.png

 

 

 

Azure pipeline

 

 

 

Create a project

 

 

To create a pipeline, first we will need to create a new project in Azure DevOps

 

Once the project is created, we can go to the project-Repos, and import the code from Github or git push code from existing repository.

 

largevv2px999.png.f187a75a6ae82633672af482f74be5bb.png

 

Create a service connection

 

 

Before we create a pipeline, to deploy the resources to Azure, we will need to create a service connection, by clicking the project settings.

 

Then go to the service connections, create service connection. Choose Azure Resource Manager->Service principal

 

largevv2px999.png.f4a12114d7a6c05073fa281ba6ca083a.png

 

Select the subscription and resource group you would like to connect, give the connection a name, and save. Now we are ready to use this service connection in the pipeline.

 

largevv2px999.png.f45b45be6b8c26b221e5278c34df8dc8.png

 

 

Create a pipeline

 

 

Now we can go back to the project and create a pipeline. Select "New pipeline"->"Azure Repos Git"-> "Existing Azure Pipelines YAML file" from the project repo.

 

Choose the "logic-app-pipeline-infra.yml" file in the repository. This pipeline put CI and CD together, including two stages, to create the infrastructures for Logic App:

 

 

 

Stage 1: Build

 

This stage will build and publish the artifact from path "Terraform/LAstandard.tf"

 

439x286vv2.png.a25b1f3edf8f7abf129d2f88848f269f.png

 

Stage 2: Deploy

 

This stage has the following steps:

 

1. An Azure CLI task to create the storage for terraform. This storage account is different than the Logic App hosting storage. By default, Terraform stores state locally in a file named terraform.tfstate. With remote state, Terraform writes the state data to a remote data store. This task is commented out. If you already have a storage account to save the terraform files, it can be skipped.

 

largevv2px999.png.b2df6c4c6e214964f7c905e07b577ba7.png

 

In my case I will use an existing storage account in my subscription for the Terraform data, the values are specified in the variables:

 

408x107vv2.png.7ca50e00fb7a6e83f855a51321f5deec.png

 

2. Download the artifact

 

298x79vv2.png.d6db48e3a8cf88f99948d4fcb60dd018.png

 

3. A PowerShell script to get the storage key

 

582x135vv2.png.366b167bad5ad8c03ee73851ed4b6441.png

 

4. Replace the token in the
LAstandard.tf
file, started with "
__
" in the terraform code. For example
"
__storagekey__
".
This task will replace the token value with the values got from the PowerShell task.

 

410x107vv2.png.cf4a5542b570588288b23f6e720200fc.png

 

5. Install the latest version of Terraform.

 

561x40vv2.png.6b8e810c0e611a386d9448f2d9ba5ede.png

 

6. Terraform init->plan->apply to initialize, plan, and deploy the Logic App using Terraform.

 

520x401vv2.png.e13aa880bfc85149fd240aabbed0a542.png

 

 

Run the pipeline

 

 

Once the yml file is added, you can replace the variable values with your own storage account information, and the environmentServiceNameAzureRM with your service connection name. After the yml file is edited and saved, you can start to run the pipeline.

 

 

 

1. First stage is to build the artifact, the artifact will be published to pipeline.

 

441x324vv2.png.570a271df0a567d59e86df873f9e0d10.png

 

 

 

2. Second stage is to deploy the Logic App infrastructure.

 

371x439vv2.png.5d03edf1af86e82cc26894064103fefd.png

 

In the Terraform
plan
step you can review the output to verify what are the resources going to be added, changed, or destroyed.

 

394x425vv2.png.f1d136c6bd33934db8410e708a945cec.png

 

In the terraform
apply
step the resources will be created.

 

467x395vv2.png.1f8c8bb8b1ce4ec0f54eab6d4b49bf95.png

 

 

 

After the pipeline run successfully, you can view the resources which just deployed from the Azure portal.

 

567x314vv2.png.5f17f103da0050b9bef4b32bba03b373.png

 

 

 

Deploy the workflow

 

 

Standard Logic App allows you to deploy the application code and infrastructure separately. In the Github repo I have also included a sample code for the workflow.

 

201x170vv2.png.1559e04159821c4098dec5eac61f7a6e.png

 

 

You can deploy the workflow separately using the logic-app-pipeline-wf.yml with the zip deploy method.

 

 

 

420x179vv2.png.4c9a2f1facfc91c279ae3eb726cbf09e.png

 

 

 

 

 

 

 

References:

 

Automating infrastructure deployments in the Cloud with Terraform and Azure Pipelines

 

Terraform Registry

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...