Azure的Function应用程序无法访问存储帐户

lzfw57am  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(109)

我正在开发一个函数应用程序(Python代码),应该使用存储在存储帐户中的zip文件进行部署。所有所需资源都使用Terraform进行部署。在Terraform执行过程中一切都很顺利。
我可以看到Azure门户中的所有资源。但是,功能应用程序不运行。我可以看到这个zip文件在Terraform执行期间被推送。我可以使用浏览器下载,但Function App无法访问文件。
在Portal中查看Function App,我可以看到消息:Azure Functions Runtime无法访问。它还提供了进一步阅读的链接。
我创建的Terraform:
1.功能应用程序(使用现有的应用程序服务计划)
1.用户管理身份(用于Function App从KeyVault和存储帐户读取值)
1.存储帐户+存储Blob
步骤2中提到的身份被赋予角色“读取器”和“存储Blob数据贡献者”。我已尝试设置功能应用程序,以便使用身份或访问密钥访问存储帐户。我得到了同样的结果。:(
对这种情况有什么建议吗?我是否必须为存储帐户创建专用端点,以便Function App能够对其进行私有访问?

编辑1:

使用SAS令牌从存储帐户检索zip文件解决了问题。代码是这样推送的:

resource "azurerm_storage_blob" "updater" {
  name                   = "func-name.zip"
  storage_account_name   = azurerm_storage_account.updater.name
  storage_container_name = azurerm_storage_container.updater.name
  type                   = "Block"
  content_md5            = local.updater_md5
  source                 = local.updater_file_path
}

Function App是这样设置的:

resource "azurerm_linux_function_app" "updater" {
  name                = "func-app-name"
  // OMITTED CODE
  app_settings = {
    "WEBSITE_RUN_FROM_PACKAGE" = azurerm_storage_blob.updater.url 
    // OMITTED CODE
  }
}

在将WEBSITE_RUN_FROM_PACKAGE更改为"https://${azurerm_storage_account.updater.name}.blob.core.windows.net/${azurerm_storage_container.updater.name}/${azurerm_storage_blob.updater.name}${data.azurerm_storage_account_blob_container_sas.updater.sas}"后,事情开始工作了。
谢谢

mfuanj7w

mfuanj7w1#

当我运行terraform脚本时,在容器级别而不是blob级别创建了不正确的SAS URL,我的Function zip存在,并在整个Functions文件夹上创建了一个zip文件,而不仅仅是触发器,我收到了与你相同的错误代码:-
我压缩了整个文件夹,请参阅下面:-

和不正确的SAS URL创建在容器级别而不是我的zip文件存在的blob级别,我收到了与你相同的错误代码.
然后,我创建了如下的HttpTrigger 1的zip文件:

然后,我上传了上面的HttpTrigger 1文件到我的blob存储,并创建了一个具有读取权限的SAS URL,如下所示:

复制SAS Blob URL并在我的terraform代码中使用它,如下所示:-
我的main.tf代码:-

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.59.0"
    }
  }
}

# Configure the Microsoft Azure Provider
# Configure the Microsoft Azure Provider
provider "azurerm" {
  
subscription_id = "xxxxxxfd-e2b6e97cb2a7"
tenant_id = "xxxxxx99ed-af9038592395"
client_id = "xxxxxd26a31435cb"
client_secret = "xxxxx-CS0ifbLE"
features {
  resource_group {
    prevent_deletion_if_contains_resources = false
  }
}

}


resource "azurerm_resource_group" "example" {
  name     = "azure-functions-example-rgsiddhesh"
  location = "West Europe"
}

resource "azurerm_storage_account" "example" {
  name                     = "examlpesasiliconstrg32"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  depends_on = [ azurerm_resource_group.example ]
}

resource "azurerm_app_service_plan" "example" {
  name                = "azure-functions-example-sp-siliconweb"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  kind                = "Linux"
  reserved            = true
  depends_on = [ azurerm_resource_group.example ]

  sku {
    tier = "Dynamic"
    size = "Y1"
  }

  lifecycle {
    ignore_changes = [
      kind
    ]
  }
}

resource "azurerm_function_app" "example" {
  name                       = "example-azure-function-siliconfunc65"
  location                   = azurerm_resource_group.example.location
  resource_group_name        = azurerm_resource_group.example.name
  app_service_plan_id        = azurerm_app_service_plan.example.id
  storage_account_name       = azurerm_storage_account.example.name
  storage_account_access_key = azurerm_storage_account.example.primary_access_key
  os_type                    = "linux"
  version                    = "~4"
  depends_on = [ azurerm_storage_account.example ]
  app_settings = {
  WEBSITE_RUN_FROM_PACKAGE = "https://siliconrga233.blob.core.windows.net/func/HttpTrigger1.zip?sp<sastoken>"
      "FUNCTIONS_WORKER_RUNTIME" = "python",
    "AzureWebJobsDisableHomepage" = "true",
    "SCM_DO_BUILD_DURING_DEPLOYMENT" =  "true"
  }
  site_config {
    linux_fx_version = "python|3.10"
  }
}

在上面的terraform脚本中,我添加了以下代码以使部署成功:

app_settings = {
  WEBSITE_RUN_FROM_PACKAGE = "https://siliconrga233.blob.core.windows.net/func/HttpTrigger1.zip?sp=r&st=2023-06-08T17:04:38Z&se=2023-06-09T01:04:38Z&sv=2022-11-02&sr=b&sig=JncUfWoHdCzVPQJifdA56f1B4J%2F6WezSBkGyhRBm25g%3D"
      "FUNCTIONS_WORKER_RUNTIME" = "python",
    "AzureWebJobsDisableHomepage" = "true",
    "SCM_DO_BUILD_DURING_DEPLOYMENT" =  "true"
  }

确保您的存储帐户存在于您的门户中,并且您正确地创建了触发器的zip文件,其中SAS URL在触发器的zip文件级别的blob级别创建。

输出:-

HttpTrigger函数应用部署成功,参考如下:-

参考号:-

Publish Azure Functions code with Terraform (maxivanov.io)

相关问题