Deploy .zip archive (python code) to Azure Function using TerraForm

huangapple go评论133阅读模式
英文:

Deploy .zip archive (python code) to Azure Function using TerraForm

问题

我遇到了将Python代码上传到Function App的问题,问题在于Function App已创建,但代码未上传到“Function”部分,而是上传到“AppFiles”部分(Azure Portal中没有“Functions”中的函数)。

这是我的TerraForm代码:

provider "azurerm" {
  features {}
}

data "archive_file" "function_zip" {
  type        = "zip"
  source_dir  = "../${path.module}/blob_storage_trigger"
  output_path = "../${path.module}/blob_storage_trigger.zip"
  depends_on = [null_resource.pip]
}

resource "null_resource" "pip" {
  triggers = {
    requirements_md5 = "${filemd5("../${path.module}/blob_storage_trigger/requirements.txt")}"
  }
  provisioner "local-exec" {    
    command = "pip install --target='.python_packages/lib/site-packages' -r requirements.txt"
    working_dir = "../${path.module}/blob_storage_trigger"
  }
}

#上传到Blob容器
resource "azurerm_storage_blob" "storage_blob_function" {
  name                   = "my-function.zip"
  storage_account_name   = "***"
  storage_container_name = "***"
  type                   = "Block"
  source                 = "../${path.module}/blob_storage_trigger.zip"
}

# 使用消耗性定价创建Azure App Service计划
resource azurerm_service_plan "app_service_plan" {
  name                   = "app_service_plan"
  location               = "West Europe"
  resource_group_name    = "***"
  os_type                = "Linux"
  sku_name               = "Y1"
}

resource "azurerm_function_app" "blurring_fn_app" {
  name                = "calinfntest-app-terraform"
  location            = "West Europe"
  resource_group_name = "***"

  storage_account_name       = "***"
  storage_account_access_key = "***"
  app_service_plan_id =  azurerm_service_plan.app_service_plan.id

  app_settings = {
    "WEBSITE_RUN_FROM_PACKAGE" = azurerm_storage_blob.storage_blob_function.url
  }
  site_config {}
}

观察:

Function App已成功创建,pip软件包已成功安装在上传到存储帐户内的Blob容器中的.zip包中(我在TerraForm的日志中甚至再次检查了这一步),并且.zip已成功上传到azurerm_function_app资源中指定的Blob容器。

terraform apply运行成功,但当我打开Azure Portal并转到Function应用时,实际上没有部署函数(没有可用的函数)。

但是,我可以在“AppFiles”部分中看到我的代码,如下所示,但不在Function部分中。

Deploy .zip archive (python code) to Azure Function using TerraForm

我该如何解决这个问题?

英文:

I am having issues with uploading Python code to a Function App, in the sense that the function-app is created, but the code is not uploaded in the "Function" section, but rather in the "AppFiles" (there is no function in "Functions" in Azure Portal).

Here is my TerraForm code:

provider "azurerm" {
  features {}
}

data "archive_file" "function_zip" {
  type        = "zip"
  source_dir  = "../${path.module}/blob_storage_trigger"
  output_path = "../${path.module}/blob_storage_trigger.zip"
  depends_on = [null_resource.pip]
}

resource "null_resource" "pip" {
  triggers = {
    requirements_md5 = "${filemd5("../${path.module}/blob_storage_trigger/requirements.txt")}"
  }
  provisioner "local-exec" {    
    command = "pip install --target='.python_packages/lib/site-packages' -r requirements.txt"
    working_dir = "../${path.module}/blob_storage_trigger"
  }
}

#Uploading to the blob container
resource "azurerm_storage_blob" "storage_blob_function" {
  name                   = "my-function.zip"
  storage_account_name   = "***"
  storage_container_name = "***"
  type                   = "Block"
  source                 = "../${path.module}/blob_storage_trigger.zip"
}

# Create Azure App Service Plan using Consumption pricing
resource azurerm_service_plan "app_service_plan" {
  name                   = "app_service_plan"
  location               = "West Europe"
  resource_group_name    = "***"
  os_type                = "Linux"
  sku_name               = "Y1"
}

resource "azurerm_function_app" "blurring_fn_app" {
  name                = "calinfntest-app-terraform"
  location            = "West Europe"
  resource_group_name = "***"


  storage_account_name       = "***"
  storage_account_access_key = "***"
  app_service_plan_id =  azurerm_service_plan.app_service_plan.id

  app_settings = {
    "WEBSITE_RUN_FROM_PACKAGE" = azurerm_storage_blob.storage_blob_function.url
  }
  site_config {}
}

Observations:

The function-app is successfully created, the pip packages are successfully installed inside the .zip package that is uploaded on a blob container within a storage account (I double-checked this step, even in the logs of TerraForm) and the .zip is successfully uploaded to the blob container specified in azurerm_function_app resource.

The terraform apply runs successfully, but when I open the Azure Portal and go the the function app, there is no actual function deployed (no functions available).

However, I can I can see my code appearing in "AppFiles" section as denoted below, but not in the Function section.

Deploy .zip archive (python code) to Azure Function using TerraForm

How can I solve this problem?

答案1

得分: 0

以下是您要翻译的内容:

问题出在这里:

data "archive_file" "function_zip" {
  type        = "zip"
  source_dir  = "../${path.module}/blob_storage_trigger"
  output_path = "../${path.module}/blob_storage_trigger.zip"
  depends_on = [null_resource.pip]
}

.zip 需要一个额外的上级文件夹,以正确创建嵌套文件夹,从而正确部署应用程序。

因此,source_dir 需要像这样:"../${path.module}/upper_level_folder_name",实际上包含了 "blob_storage_trigger" 文件夹。

最终,部署的结构应如下所示:

.zip
   -function_1
           -.zipped_code_for_function_1

   -function_2
           -.zipped_code_for_function_2

我的问题在于 .zip 中我只放了 .zipped_code_for_function_1

因此,通过遵循上述结构,问题得以解决。

英文:

The problem was here :

data "archive_file" "function_zip" {
  type        = "zip"
  source_dir  = "../${path.module}/blob_storage_trigger"
  output_path = "../${path.module}/blob_storage_trigger.zip"
  depends_on = [null_resource.pip]
}

The .zip would need an additional, upper-level folder in order to properly create a nested folder so that the application is correctly deployed.

So the source_dir needs to be like "../${path.module}/upper_level_folder_name", which in essence contains the "blob_storage_trigger" folder.

Eventually, the structure of the deployment must look like:

.zip
   -function_1
           -.zipped_code_for_function_1

   -function_2
           -.zipped_code_for_function_2

My problem was that in .zip I only put .zipped_code_for_function_1.

Consequently, the problem is solved by respecting the structure from above.

huangapple
  • 本文由 发表于 2023年4月10日 20:55:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定