Deploy Logic App Standard with Terraform and Azure DevOps pipelines

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Community Hub.

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:

Screenshot 2023-04-30 103832.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. 

import.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 

create-service-connection.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.

Screenshot 2023-04-30 104506.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 

Screenshot 2023-04-30 104506.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.

Screenshot 2023-04-30 104506.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:

Screenshot 2023-04-30 105047.png

2. Download the artifact

Screenshot 2023-04-30 104506.png

3. A PowerShell script to get the storage key

Screenshot 2023-04-30 104506.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. 

Screenshot 2023-04-30 104506.png

5. Install the latest version of Terraform. 

Screenshot 2023-04-30 104506.png

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

Screenshot 2023-04-30 104506.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. 

Screenshot 2023-04-30 104506.png

 

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

Screenshot 2023-04-30 104506.png

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

huidongliu_16-1682782816360.png

In the terraform apply step the resources will be created. 

huidongliu_1-1682786912672.png

 

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

Screenshot 2023-04-30 104506.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.

huidongliu_0-1682787221155.png

 

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

 

huidongliu_2-1682787356294.png

 

 

 

References:

https://www.azuredevopslabs.com/labs/vstsextend/terraform/#overview

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/logic_app_standard

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.