gpt4 book ai didi

azure - 使用 Terraform 为 Azure Function 创建 Http 触发器

转载 作者:行者123 更新时间:2023-12-02 23:00:44 32 4
gpt4 key购买 nike

我正在尝试使用 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. init.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。 function.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 时,我看不到任何内容。 enter image description here

但是,在应用程序文件中我确实看到了之前创建的 python 函数文件 enter image description here

最佳答案

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

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

    3.这是在 terraform 模块文件夹中输入 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

    关于azure - 使用 Terraform 为 Azure Function 创建 Http 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70832251/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com