我正在使用 Terraform 将 zip 包部署为 Azure 函数。相关代码片段如下:
resource "azurerm_storage_account" "mtr_storage" {
name = "mtrstorage${random_string.random_storage_account_suffix.result}"
resource_group_name = azurerm_resource_group.mtr_rg.name
location = azurerm_resource_group.mtr_rg.location
account_kind = "BlobStorage"
account_tier = "Standard"
account_replication_type = "LRS"
network_rules {
default_action = "Deny"
ip_rules = ["127.0.0.1", "my.id.addr.here"]
virtual_network_subnet_ids = [azurerm_subnet.mtr_subnet.id]
bypass = ["AzureServices", "Metrics"]
}
tags = {
environment = "local"
}
}
resource "azurerm_storage_blob" "mtr_hello_function_blob" {
name = "MTR.ListBlobsFunction.publish.zip"
storage_account_name = azurerm_storage_account.mtr_storage.name
storage_container_name = azurerm_storage_container.mtr_hello_function_container.name
type = "Block"
source = "./example_code/MTR.ListBlobsFunction/MTR.ListBlobsFunction.publish.zip"
}
resource "azurerm_service_plan" "mtr_hello_function_svc_plan" {
name = "mtr-hello-function-svc-plan"
location = azurerm_resource_group.mtr_rg.location
resource_group_name = azurerm_resource_group.mtr_rg.name
os_type = "Linux"
sku_name = "B1"
tags = {
environment = "local"
}
}
data "azurerm_storage_account_blob_container_sas" "storage_account_blob_container_sas_for_hello" {
connection_string = azurerm_storage_account.mtr_storage.primary_connection_string
container_name = azurerm_storage_container.mtr_hello_function_container.name
start = timeadd(timestamp(), "-5m")
expiry = timeadd(timestamp(), "5m")
permissions {
read = true
add = false
create = false
write = false
delete = false
list = false
}
}
resource "azurerm_linux_function_app" "mtr_hello_function" {
name = "mtr-hello-function"
location = azurerm_resource_group.mtr_rg.location
resource_group_name = azurerm_resource_group.mtr_rg.name
service_plan_id = azurerm_service_plan.mtr_hello_function_svc_plan.id
storage_account_name = azurerm_storage_account.mtr_storage.name
storage_account_access_key = azurerm_storage_account.mtr_storage.primary_access_key
app_settings = {
"FUNCTIONS_WORKER_RUNTIME" = "dotnet"
"WEBSITE_RUN_FROM_PACKAGE" = "https://${azurerm_storage_account.mtr_storage.name}.blob.core.windows.net/${azurerm_storage_container.mtr_hello_function_container.name}/${azurerm_storage_blob.mtr_hello_function_blob.name}${data.azurerm_storage_account_blob_container_sas.storage_account_blob_container_sas_for_hello.sas}"
"AzureWebJobsStorage" = azurerm_storage_account.mtr_storage.primary_connection_string
"AzureWebJobsDisableHomepage" = "true"
}
site_config {
always_on = true
application_insights_connection_string = azurerm_application_insights.mtr_ai.connection_string
application_stack {
dotnet_version = "8.0"
use_dotnet_isolated_runtime = true
}
cors {
allowed_origins = ["*"]
}
}
tags = {
environment = "local"
}
}
zip 文件已上传到存储帐户,它是可下载的并且具有正确的结构(或者我认为是这样)。函数应用程序也已创建,但是当我向下滚动查看函数时,它告诉我加载函数时出错:
Encountered an error (ServiceUnavailable) from host runtime.
这可能是一些配置错误,但我只是看不到它。我还进入了高级工具选项卡,在 wwwroot 中只有一个名为 run_from_package_failure.log
的文件。其内容是:
RunFromPackage> 无法从 https://mtrstorageqbr56n79h4.blob.core.windows.net/hello-function-releases/MTR.ListBlobsFunction.publish.zip?sv=2018-11-09&sr=c&st=2024-03 下载包-31T12:09:08Z&se = 2024-03-31T12:19:08Z&sp = r&spr = https&sig = c6dhOCRPL%2BEQujbuq0589D2MXFN5eXfBmhow1QWSfHU%3D。返回代码:1 -(此 URL 格式错误,我交换了一些字符;))。但是,当我转到日志包含的 url 时,zip 文件已成功下载...
首先,要将所需的代码部署为函数应用程序中的 Azure 函数,您需要使用
azurerm_function_app_function
资源提供程序。这将确保函数在函数应用程序中作为单独的代码变得可见和可访问。
仅按照您尝试的方式添加代码只会使其在
app files
下可见,但不会被识别为不同的函数。
我在我的环境中尝试了与您类似的代码,部署成功,如下所示。
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.97.1"
}
}
}
provider "azurerm" {
features {}
}
data "azurerm_resource_group" "mtr_rg" {
name = "Jahnavi"
}
resource "azurerm_storage_account" "mtr_storage" {
name = "mtrstoragej"
resource_group_name = data.azurerm_resource_group.mtr_rg.name
location = data.azurerm_resource_group.mtr_rg.location
account_kind = "BlobStorage"
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_storage_container" "mtr_hello_function_container" {
name = "vhds"
storage_account_name = azurerm_storage_account.mtr_storage.name
container_access_type = "private"
}
resource "azurerm_storage_blob" "mtr_hello_function_blob" {
name = "MTR.ListBlobsFunction.publish.zip"
storage_account_name = azurerm_storage_account.mtr_storage.name
storage_container_name = azurerm_storage_container.mtr_hello_function_container.name
type = "Block"
source = "/home/xx/Console-Application-Examples-for-.Net-Core-master.zip"
}
resource "azurerm_service_plan" "mtr_hello_function_svc_plan" {
name = "mtr-hello-function-svc-plan"
location = data.azurerm_resource_group.mtr_rg.location
resource_group_name = data.azurerm_resource_group.mtr_rg.name
os_type = "Linux"
sku_name = "B1"
}
data "azurerm_storage_account_blob_container_sas" "storage_account_blob_container_sas_for_hello" {
connection_string = azurerm_storage_account.mtr_storage.primary_connection_string
container_name = azurerm_storage_container.mtr_hello_function_container.name
start = timeadd(timestamp(), "-5m")
expiry = timeadd(timestamp(), "5m")
permissions {
read = true
add = false
create = false
write = false
delete = false
list = false
}
}
resource "azurerm_linux_function_app" "mtr_hello_function" {
name = "jahfuncchoc"
location = data.azurerm_resource_group.mtr_rg.location
resource_group_name = data.azurerm_resource_group.mtr_rg.name
service_plan_id = azurerm_service_plan.mtr_hello_function_svc_plan.id
storage_account_name = azurerm_storage_account.mtr_storage.name
storage_account_access_key = azurerm_storage_account.mtr_storage.primary_access_key
app_settings = {
"FUNCTIONS_WORKER_RUNTIME" = "dotnet"
"WEBSITE_RUN_FROM_PACKAGE" = "https://${azurerm_storage_account.mtr_storage.name}.blob.core.windows.net/${azurerm_storage_container.mtr_hello_function_container.name}/${azurerm_storage_blob.mtr_hello_function_blob.name}${data.azurerm_storage_account_blob_container_sas.storage_account_blob_container_sas_for_hello.sas}"
"AzureWebJobsStorage" = azurerm_storage_account.mtr_storage.primary_connection_string
"AzureWebJobsDisableHomepage" = "true"
"SCM_DO_BUILD_DURING_DEPLOYMENT" = "true"
}
site_config {
always_on = true
#application_insights_connection_string = azurerm_application_insights.mtr_ai.connection_string
application_stack {
dotnet_version = "8.0"
use_dotnet_isolated_runtime = true
}
}
}
输出:
功能触发代码:
resource "azurerm_function_app_function" "example" {
name = "examplejahfunction"
function_app_id = azurerm_linux_function_app.mtr_hello_function.id
language = "CSharp"
file {
name = "Console-Application-Examples-for-.Net-Core-master.zip"
content = file("/home/xx/Console-Application-Examples-for-.Net-Core-master.zip")
}
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"
},
]
})
}
回到你现在面临的问题;这可能是由于
"WEBSITE_RUN_FROM_PACKAGE"
块下的 app_setting
的值所致。修改如下并再次检查 URL。
"WEBSITE_RUN_FROM_PACKAGE" = "${azurerm_storage_blob.mtr_hello_function_blob.url}${data.azurerm_storage_account_blob_container_sas.storage_account_blob_container_sas_for_hello.sas}"