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

DevOps + Terraform deployment Infra + Azure Functions deployment

  • Resources Introduction
    • Terraform
      • What is Terraform?
      • Terraform run mode?
      • Terraform version control?
    • AzureDevOps
    • AzureFunctions
  • Background introduction
  • Resource preparation
  • code writing
    • Project code
    • Infra code
      • Write infra code
  • Follow-up: [For subsequent deployment, please refer to DevOps deployment of python AzureFunctions (Httptrigger) + DevOps deployment of Terraform to build and deploy Infra resources (2)] (https://blog.csdn.net/lisong315389/article/details/134164741?spm=1001.2014 .3001.5501)

Resource introduction

Terraform

What is Terraform?

Terraform —– is an infrastructure management tool that allows us to build, change and manage infrastructure as code. Terraform is not tied to any specific cloud provider and can work with multiple cloud providers and environments. Although Azure and AWS clearly have solutions for resource management and settings of their own cloud platforms.

Terraform run mode?

The Infra resources we build in Azure will form an ARM template:

Azure: ARM Template (An ARM template is a JavaScript Object Notation (JSON) file that defines your project’s infrastructure and configuration. The template uses a declarative syntax that allows you to specify what you want to deploy without having to write a series of programming commands to Create content. In the template, specify the resources to be deployed and the properties of those resources.)

Terraform version control?

The Terraform CLI provides a simple mechanism for deploying and versioning configuration files to Azure, enabling you to define, provision, and configure Azure resources in a repeatable and predictable manner using Terraform template-based configuration files.

AzureDevOps

Using Azure DevOps to automate compilation and deployment, the development project is divided into the following stages:
Develop>Submit>Compile and integrate>Test>Deploy>Monitor feedback
We have to test and build repeatedly until the project is completed. It is too cumbersome to do it manually. At this time, AzureDevOps plays the role of running this process.

AzureFunctions

Run our program. Here we mainly introduce Terraform. Azure Functions can be regarded as an application project. We choose the application triggered by HTTP Trigger to facilitate testing.

Background introduction

Today’s cloud technology is developing rapidly, and many companies have begun to migrate the work of this project to the cloud. If we move to the cloud, we first need to build the infra environment where our project runs. If we build it manually, it will not only be a heavy workload but also error-prone. At this time we You can use IAC, code is the facility, and we use Terraform declarative code to build the infra environment we need to run our project. Then we use DevOps to deploy Terraform’s Infra environment before deploying the project, and then deploy the project, which is very convenient.

Resource preparation

Local project development: VScode Python 3.10 (anyone is OK, I use 3.10 here) Terraform plug-in
Azure: Azure subscription
AzureDevOps: One organization One empty project

Code writing

Project code

Open VScode:
Shortcut key (Ctrl + Shift + P), find Azure Functions: Create New project. . .
During this period, you will be asked to choose the Python version and verification method (select no verification method), and then we choose HTTP Trigger to trigger

Until the end, we can build a successful project after waiting for a while. We can run it locally, visit the URL and give a parameter, and it will be OK.

Infra Code

ok, our protagonist comes to Terraform. Let’s create the Azure infra environment code. We create a new folder called “terraforminfra” in the parallel folder of our project, and then we create the code for the infra resource in this folder.

Let’s create a few files:
main.tf: The code to create resources, the code for the Azureinfra resources we declared.
outputs.tf: Output file, we can output some parameters, or connection string, etc.
provider.tf: version of required_providers, this thing calls Azure’s API to create resources, and declares what cloud needs to be connected (Azure or AWS…)
variables.tf: Define parameters, where you can define parameters, such as location, resource groupname and other reusable parameters, which can be used by the main.tf file.

Write infra code

Every project migration to the cloud must first have an architecture diagram and infra resource documents. We HttpTrigger
Requires an App Service plan, storage account, and an Azure Function. In this way, you can run an Azure Functions project.
main.tf:

resource "azurerm_resource_group" "name" {<!-- -->
  name = var.azurerm_resource_group
  location = var.location
}

resource "azurerm_storage_account" "example" {<!-- -->
  name = "funcstoracont"
  resource_group_name = azurerm_resource_group.name.name
  location = azurerm_resource_group.name.location
  account_tier = "Standard"
  account_replication_type = "LRS"

    tags = {<!-- -->
      environment = "test"
    }
}

resource "azurerm_service_plan" "example" {<!-- -->
  name = "example-service-plan"
  location = azurerm_resource_group.name.location
  resource_group_name = azurerm_resource_group.name.name
  os_type = "Linux"
  sku_name = "S1"
}

resource "azurerm_linux_function_app" "example" {<!-- -->
  name = "ls-exale-func"
  location = azurerm_resource_group.name.location
  resource_group_name = azurerm_resource_group.name.name
  service_plan_id = azurerm_service_plan.example.id

  storage_account_name = azurerm_storage_account.example.name
  storage_account_access_key = azurerm_storage_account.example.primary_access_key

  site_config {<!-- -->
    application_stack {<!-- -->
      python_version = "3.10"
    }
  }
}

resource "azurerm_function_app_function" "example" {<!-- -->
  name = "example-function-app-function-ls"
  function_app_id = azurerm_linux_function_app.example.id
  language = "Python"
  test_data = jsonencode({<!-- -->
    "name" = "Azure"
  })
  config_json = jsonencode({<!-- -->
    "bindings" = [
      {<!-- -->
        "authLevel" = "function"
        "direction" = "in"
        "methods" = [
          "get",
          "post",
        ]
        "name" = "req"
        "type" = "httpTrigger"
      },
      {<!-- -->
        "direction" = "out"
        "name" = "$return"
        "type" = "http"
      },
    ]
  })
}

outputs.tf:

output "resource_group_name" {<!-- -->
    value = azurerm_resource_group.name.id
  
}

provider.tf:

terraform {<!-- -->
  required_providers {<!-- -->
      azurerm = {<!-- -->
      version = "~>3.47.0"
      source = "hashicorp/azurerm"
    }
  }


    backend "azurerm" {<!-- -->}


}

provider "azurerm" {<!-- -->
    features {<!-- -->
      
    }
  
}

variables.tf:

variable "location" {<!-- -->
    type = string
    default = "east asia"
    description = "resource location default Eastasia"

  
}

variable "azurerm_resource_group" {<!-- -->
    type = string
    default = "lisongterraform"
    description = "resourcegroup name"
  
}

The final code structure is as follows:

If we need to deploy other infra environments, you can check the following link to find out:

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

Follow-up: For details on subsequent deployment, please refer to DevOps deployment of python’s AzureFunctions (Httptrigger) + DevOps deployment of Terraform to build and deploy Infra resources (2)