DevOps deploys python’s AzureFunctions (Httptrigger) + DevOps deploys Terraform to build and deploy Infra resources (2)

DevOps + Terraform Deployment Infra + Azure Functions Deployment 2

  • Deployment preparation
    • Push the project, create a Self-Host Agent or use Microsoft’s own compiler (here we use Microsoft’s own compiler)
    • Create Service Principal
  • Create a storage account and a container to store files generated by running Terraform
  • Deploy and create code
    • Terraform controls
  • Release code

Deployment preparation

Push the project, create a Self-Host Agent or use Microsoft’s own compiler (here we use Microsoft’s own compiler)

Ok, after the local code is completed, we create an organization on Azure DevOps, then initialize a project and push the local project to AzureRepos. Then we configure a compilation machine. Here we can see the previous article Selfhost Agent configuration.

Create Service Principal

This is used to connect to Azure and give this pipeline permission to operate Azure resources.
We go into the project we created, then click “Project Settings” > Then click “Service Connection” > “New Service Connection”

Then we click on Azure Resource Management,

Then we select the automatic Serviceprincipal (automatic) and click “Next”.
Then we can give subscription permissions.
Just select your subscription and save it.

Create a storage account and a container to store files generated by running Terraform

We need to create a storage account on Azure Portal in advance and then create a container in it. Here we created a storage account named “lslsls”,
The container name is “lsls” and is used to store files generated by running Terraform.

Deployment creation code

When we create a CI in Pipelines, we can create an empty one first.
Then the following code:

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
-master

pool:
  vmImage: ubuntu-latest

variables:
  - group: varls

steps:
- task: TerraformTaskV4@4
  inputs:
    provider: 'azurerm'
    command: 'init'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraforminfra'
    backendAzureRmResourceGroupName: 'RG-LS-TRANING'
    backendAzureRmStorageAccountName: 'lslsls'
    backendAzureRmContainerName: 'lsls'
    backendAzureRmKey: 'tf/terraform.tfstate'
    backendServiceArm: 'll'
    
- task: TerraformTaskV4@4
  inputs:
    provider: 'azurerm'
    command: 'plan'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraforminfra'
    environmentServiceNameAzureRM: 'll'
  
- task: TerraformTaskV4@4
  inputs:
    provider: 'azurerm'
    command: 'apply'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraforminfra'
    environmentServiceNameAzureRM: 'll'
    
    
- task: TerraformTaskV4@4
  inputs:
    provider: 'azurerm'
    command: 'output'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraforminfra'
    environmentServiceNameAzureRM: 'll'

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    replaceExistingArchive: true
# - task: AzureFunctionApp@2
# inputs:
# connectedServiceNameARM: 'll'
# appType: 'functionApp'
# appName: $(funcname)
# package: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
# deploymentMethod: 'auto'

- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Build.ArtifactStagingDirectory)'
    publishLocation: 'pipeline'

- task: AzureFunctionApp@2
  inputs:
    connectedServiceNameARM: 'll'
    appType: 'functionAppLinux'
    appName: 'ls-exale-func'
    package: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    runtimeStack: 'PYTHON|3.10'
    deploymentMethod: 'auto'

There are trigger branches, compilation pools, parameters,
Then there is the deployment of our infra environment. The deployment of the infra environment is divided into the following steps:
Terraform init: Initialization will download some required dependent plug-ins, etc.
Terraform plan: Specify deployment plan
Terraform apply: apply deployment, apply deployment according to our code
If successful, the next step is for us to deploy the project.
Let’s make a statement (here we are using Microsoft’s own compiler, so dependencies include Python, Azure CLI, etc. If it is a self-prepared agent, we need to install the required tools)

Terraform control

We can search for Terraform on the right and click on Terraform,

Provider: Cloud provider, here we choose Azurerm
Command: According to our steps above, the first step should be init
Configuration directory: The working directory where our Infra code is located, the default is the outermost directory

The project and infra code organization are as follows:

Just add /terraforminfra directly at the end according to our directory structure. The next few steps are the same.

Publish code

We can package our project code into Zip files (Archive files control)

Then find the control deploy Azure Functions and publish it.


Then we can deploy it and run the Pipeline;

We go to Azure to find our function, go to the overview, and see the function we deployed:

After entering, we click to get the function URL, and the access is OK.

ok, finished perfectly.