In this article, I will take you through enabling CI/CD for a dotnetcore app with infrastructure provisioned through Terraform. The hosting resource is on Azure.

The app would be build using a normal standard template for ASP.NET Core. The template is shown below. The only difference is I added a copy task to move the terraform files to the pipeline artifact location since the complete infrastructure would be provisioned through terraform.

After building the artifact would be packaged along with .tf files

Next, we will go through the deployment pipeline and brief each step.

  1. Before we start first we need to create a service principal for accessing the resources on the cloud based on the subscription. This can be achieved by using the service connections options and adding an azure resource manager.
  2. Then we will create the storage account in azure for the terraform to initiate and execute. All the tfstate file will be created on the blob storage container.

The Az CLI code snippet will help to create a resource group and storage account along with a container. This is needed for Terraform task will be tapping on this storage to store the tfstate files.

call az group create –location westus –name $(terraformstoragerg)

call az storage account create –name $(terraformstorageaccount) –resource-group $(terraformstoragerg) –location westus –sku Standard_LRS

call az storage container create –name terraform –account-name $(terraformstorageaccount)

call az storage account keys list -g $(terraformstoragerg) -n $(terraformstorageaccount)

The variables are defined in the variable section

2. The second task is to retrieve the storage key from Azure and set it in the variable.

$key=(Get-AzureRmStorageAccountKey -ResourceGroupName $(terraformstoragerg) -AccountName $(terraformstorageaccount)).Value[0]

Write-Host “##vso[task.setvariable variable=storagekey]$key”

#azure-devops #cloud #terraform #ci/cd

Azure Devops — CI/CD with Terraform
1.95 GEEK