【问题标题】:Create HttpTrigger for AzureFunction using Terraform使用 Terraform 为 Azure 函数创建 Http 触发器
【发布时间】:2022-01-25 14:20:09
【问题描述】:

我正在尝试使用 Terraform 部署 Azure Function 基础架构和 HttpTrigger。 我知道使用 Terraform 我将创建基础设施,而触发部分是代码责任。 但是还是不明白如何创建HttpTrigger。你能建议吗?

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "~> 2.26"
    }
  }
}
###############################
# Configure the Azure provider
provider "azurerm" {
  features {}
}
###############################
# Data
data "archive_file" "file_function_app" {
  type        = "zip"
  source_dir  = "./function-app"
  output_path = "./function-app.zip"
}
###############################
# Resource Group
resource "azurerm_resource_group" "rg" {
  name     = "${var.project}-rg"
  location = var.location
}
###############################
# App service
resource "azurerm_app_service_plan" "app_service_plan" {
  name                = "${var.project}-app-service-plan"
  resource_group_name = azurerm_resource_group.rg.name
  location            = var.location
  kind                = "FunctionApp"
  reserved = true
  sku {
    tier = "Dynamic"
    size = "Y1"
  }
}
###############################
# Function
resource "azurerm_function_app" "function_app" {
  name                       = "${var.project}-function-app"
  resource_group_name        = azurerm_resource_group.rg.name
  location                   = var.location
  app_service_plan_id        = azurerm_app_service_plan.app_service_plan.id
  app_settings = {
    "WEBSITE_RUN_FROM_PACKAGE" = "https://${azurerm_storage_account.storage_account.name}.blob.core.windows.net/${azurerm_storage_container.storage_container.name}/${azurerm_storage_blob.storage_blob.name}${data.azurerm_storage_account_blob_container_sas.storage_account_blob_container_sas.sas}",
    "FUNCTIONS_WORKER_RUNTIME" = "python",
    "APPINSIGHTS_INSTRUMENTATIONKEY" = ""
  }

  os_type = "linux"
  site_config {
    linux_fx_version          = "python|3.7"
  }
  storage_account_name       = azurerm_storage_account.storage_account.name
  storage_account_access_key = azurerm_storage_account.storage_account.primary_access_key
  version                    = "~3"

}

###############################
# Storage account
resource "azurerm_storage_account" "storage_account" {
  name = "${var.project}storage"
  resource_group_name = azurerm_resource_group.rg.name
  location = var.location
  account_tier = "Standard"
  account_replication_type = "LRS"
}
resource "azurerm_storage_container" "storage_container" {
  name                  = "function-scm"
  storage_account_name  = azurerm_storage_account.storage_account.name
  container_access_type = "private"
}
resource "azurerm_storage_blob" "storage_blob" {
  name = "${filesha256(data.archive_file.file_function_app.output_path)}.zip"
  storage_account_name = azurerm_storage_account.storage_account.name
  storage_container_name = azurerm_storage_container.storage_container.name
  type = "Block"
  source = data.archive_file.file_function_app.output_path
}
data "azurerm_storage_account_blob_container_sas" "storage_account_blob_container_sas" {
  connection_string = azurerm_storage_account.storage_account.primary_connection_string
  container_name    = azurerm_storage_container.storage_container.name

  start = "2021-01-01T00:00:00Z"
  expiry = "2022-04-04T00:00:00Z"

  permissions {
    read   = true
    add    = false
    create = false
    write  = false
    delete = false
    list   = false
  }

带有python代码的Function-app文件夹有两个文件: 1。 初始化.py

import logging
import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
            "Please pass a name on the query string or in the request body",
            status_code=400
        )

2。函数.json

   {
      "scriptFile": "__init__.py",
      "bindings": [
        {
          "authLevel": "function",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get",
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "$return"
        }
      ]
    }

部署过程通过 Azure 管道成功完成。在 Azure 门户上,当我尝试打开 AzureFunction -> Functions 时,我什么也没看到。

但是,在 App Files 中,我确实看到了以前创建的 python 函数文件

【问题讨论】:

    标签: azure terraform azure-functions terraform-provider-azure


    【解决方案1】:

    使用 terraform 创建 HTTP 触发器函数应用。

    1. 使用创建示例 HTTP 触发器函数
    • 作为 Typescript 的函数项目
    • 作为 HTTP 触发器的功能
    • 函数名称:你自己的
    • 授权级别:匿名
    1. 添加下面的 Terraform 文件
    • 资源组
    • 存储帐户
    • 应用洞察
    • 服务计划

    3.这是在 terraform modules 文件夹中输入 azure 函数资源的示例代码

    variable "LOCATION" {}
    
    variable "RESOURCE_GROUP" {}
    
    variable "STORAGE_ACC_NAME" {}
    
    variable "STORAGE_ACC_KEY" {}
    
    variable "STORAGE_CONNECTION_STRING" {}
    
    resource "azurerm_application_insights" "func_application_insights" {
    
    name = "func-application-insights"
    
    location = var.LOCATION
    
    resource_group_name = var.RESOURCE_GROUP
    
    application_type = "Node.JS"
    
    }
    
    resource "azurerm_app_service_plan" "func_app_service_plan" {
    
    name = "func-app-service-plan"
    
    location = var.LOCATION
    
    resource_group_name = var.RESOURCE_GROUP
    
    kind = "FunctionApp"
    
    reserved = true
    
    sku {
    
    tier = "Dynamic"
    
    size = "Y1"
    
    }
    
    }
    
    resource "azurerm_function_app" "func_function_app" {
    
    name = "func-function-app"
    
    location = var.LOCATION
    
    resource_group_name = var.RESOURCE_GROUP
    
    app_service_plan_id = azurerm_app_service_plan.func_app_service_plan.id
    
    app_settings = {
    
    FUNCTIONS_WORKER_RUNTIME = "node",
    
    AzureWebJobsStorage = var.STORAGE_CONNECTION_STRING,
    
    APPINSIGHTS_INSTRUMENTATIONKEY = azurerm_application_insights.func_application_insights.instrumentation_key,
    
    WEBSITE_RUN_FROM_PACKAGE = "1"
    
    }
    
    os_type = "linux"
    
    storage_account_name = var.STORAGE_ACC_NAME
    
    storage_account_access_key = var.STORAGE_ACC_KEY
    
    version = "~3"
    
    lifecycle {
    
    ignore_changes = [
    
    app_settings["WEBSITE_RUN_FROM_PACKAGE"]
    
    ]
    
    }
    
    # FIXME: Use DNS names instead of enabling CORS
    
    site_config {
    
    cors {
    
    allowed_origins = ["*"]
    
    }
    
    }
    
    }
    

    有关使用 Terraform 创建 azure 函数的完整详细信息,请查看document。这是完整的source code

    【讨论】:

    • 在我的例子中我到底做错了什么?为什么项目应该是 Typescript?
    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多