【发布时间】: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 时,我什么也没看到。
【问题讨论】:
标签: azure terraform azure-functions terraform-provider-azure