简体   繁体   English

如何使用 Terraform 完全部署容器化 Azure Function 应用程序

[英]How do I fully deploy Containerized Azure Function App with Terraform

Attempting to deploy a Function App on a Premium plan that serves the functions from a container.尝试在提供容器功能的高级计划上部署 Function 应用程序。 The HOWTO for this works well enough: https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image?tabs=nodejs#create-an-app-from-the-image用于此的 HOWTO 效果很好: https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image?tabs=nodejs#create-an-app-从图像

However, when I try to deploy it using Terraform, no sale.但是,当我尝试使用 Terraform 部署它时,没有销售。 Everything looks right but the function does not show up in the side menu (it does for the one deployed with the az CLI), nor can I hit it with Postman.一切看起来都正确,但 function 没有出现在侧边菜单中(它适用于使用 az CLI 部署的那个),我也不能用 Postman 击中它。

Via Resource Explorer I can see that the Functions are not being populated.通过资源浏览器,我可以看到函数没有被填充。 Here is the HCL that I am using这是我正在使用的 HCL

resource "azurerm_app_service_plan" "plan" {
  name                         = "${var.app_name}-Premium-ConsumptionPlan"
  location                     = "WestUS"
  resource_group_name          = "${data.azurerm_resource_group.rg.name}"
  kind                         = "Elastic"
  reserved                     = true

  sku {
    tier = "ElasticPremium"
    size = "EP1"
  }
}

data "azurerm_container_registry" "registry" {
  name = "${var.app_name}registry"
  resource_group_name = "${data.azurerm_resource_group.rg.name}"
}

resource "azurerm_function_app" "funcApp" {
    name                       = "${var.app_name}-userapi-${var.env_name}-funcapp"
    location                   = "WestUS"
    resource_group_name        = "${data.azurerm_resource_group.rg.name}"
    app_service_plan_id        = "${azurerm_app_service_plan.plan.id}"
    storage_connection_string  = "${azurerm_storage_account.storage.primary_connection_string}"
    version                    = "~2"

    app_settings = {
        FUNCTIONS_EXTENSION_VERSION               = "~2"
        FUNCTIONS_WORKER_RUNTIME                  = "dotnet"
        DOCKER_REGISTRY_SERVER_URL                = "${data.azurerm_container_registry.registry.login_server}"
        DOCKER_REGISTRY_SERVER_USERNAME           = "${data.azurerm_container_registry.registry.admin_username}"
        DOCKER_REGISTRY_SERVER_PASSWORD           = "${data.azurerm_container_registry.registry.admin_password}"
        WEBSITE_CONTENTAZUREFILECONNECTIONSTRING  = "${azurerm_storage_account.storage.primary_connection_string}"
        DOCKER_CUSTOM_IMAGE_NAME                  = "${data.azurerm_container_registry.registry.login_server}/pingtrigger:test"
        WEBSITE_CONTENTSHARE                      = "${azurerm_storage_account.storage.name}"
        FUNCTION_APP_EDIT_MODE                    = "readOnly"
    }

    site_config {
      always_on                 = true
      linux_fx_version          = "DOCKER|${data.azurerm_container_registry.registry.login_server}/pingtrigger:test"
    }
}

----- Updated based on answer ---- The solution was to instruct Function App to NOT use storage to discover metadata about available functions - this involves setting WEBSITES_ENABLE_APP_SERVICE_STORAGE to false. ----- 根据答案更新---- 解决方案是指示 Function 应用程序不要使用存储来发现有关可用功能的元数据 - 这涉及将WEBSITES_ENABLE_APP_SERVICE_STORAGE设置为 false。 Here is my updated script这是我更新的脚本

resource "azurerm_app_service_plan" "plan" {
  name                    = "${var.app_name}-premiumPlan"
  resource_group_name     = "${data.azurerm_resource_group.rg.name}"
  location                = "${data.azurerm_resource_group.rg.location}"
  kind                    = "Linux"
  reserved                = true

  sku {
    tier = "Premium"
    size = "P1V2"
  }
}

data "azurerm_container_registry" "registry" {
  name                  = "${var.app_name}registry"
  resource_group_name   = "${data.azurerm_resource_group.rg.name}"
}

resource "azurerm_function_app" "funcApp" {
    name                       = "userapi-${var.app_name}fa-${var.env_name}"
    location                   = "${data.azurerm_resource_group.rg.location}"
    resource_group_name        = "${data.azurerm_resource_group.rg.name}"
    app_service_plan_id        = "${azurerm_app_service_plan.plan.id}"
    storage_connection_string  = "${azurerm_storage_account.storage.primary_connection_string}"
    version                    = "~2"

    app_settings = {
        FUNCTION_APP_EDIT_MODE                    = "readOnly"
        https_only                                = true
        DOCKER_REGISTRY_SERVER_URL                = "${data.azurerm_container_registry.registry.login_server}"
        DOCKER_REGISTRY_SERVER_USERNAME           = "${data.azurerm_container_registry.registry.admin_username}"
        DOCKER_REGISTRY_SERVER_PASSWORD           = "${data.azurerm_container_registry.registry.admin_password}"
        WEBSITES_ENABLE_APP_SERVICE_STORAGE       = false
    }

    site_config {
      always_on         = true
      linux_fx_version  = "DOCKER|${data.azurerm_container_registry.registry.login_server}/testimage:v1.0.1"
    }
}

To create the Azure Function with your custom Docker image, I think your problem is that you set the environment variable FUNCTIONS_WORKER_RUNTIME , it means you use the built-in runtime, but you want to use your custom image.要使用您的自定义 Docker 映像创建 Azure Function,我认为您的问题是您设置环境变量意味着您要使用您的自定义运行时FUNCTIONS_WORKER_RUNTIME With my test, you only need to configure the function app like this:通过我的测试,您只需要像这样配置 function 应用程序:

resource "azurerm_function_app" "funcApp" {
    name                       = "${var.app_name}-userapi-${var.env_name}-funcapp"
    location                   = "${azurerm_resource_group.main.location}"
    resource_group_name        = "${azurerm_resource_group.main.name}"
    app_service_plan_id        = "${azurerm_app_service_plan.plan.id}"
    storage_connection_string  = "${azurerm_storage_account.storage.primary_connection_string}"
    version                    = "~2"

    app_settings = {
        FUNCTIONS_EXTENSION_VERSION               = "~2"
        DOCKER_REGISTRY_SERVER_URL                = "${data.azurerm_container_registry.registry.login_server}"
        DOCKER_REGISTRY_SERVER_USERNAME           = "${data.azurerm_container_registry.registry.admin_username}"
        DOCKER_REGISTRY_SERVER_PASSWORD           = "${data.azurerm_container_registry.registry.admin_password}"
        WEBSITE_CONTENTAZUREFILECONNECTIONSTRING  = "${azurerm_storage_account.storage.primary_connection_string}"
        WEBSITE_CONTENTSHARE                      = "${azurerm_storage_account.storage.name}"
        DOCKER_CUSTOM_IMAGE_NAME                  = "${data.azurerm_container_registry.registry.login_server}/pingtrigger:test"
    }

    site_config {
      always_on                 = true
      linux_fx_version          = "DOCKER|${data.azurerm_container_registry.registry.login_server}/pingtrigger:test"
    }
}

Then you only need to wait a while for the creation.然后您只需要等待一段时间即可创建。

I believe you have the wrong worker runtime being used.我相信您使用了错误的工作程序运行时。 I think you need to set that to docker.我认为您需要将其设置为 docker。

app_settings = {
        FUNCTIONS_EXTENSION_VERSION               = "~2"
        FUNCTIONS_WORKER_RUNTIME                  = "docker"
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM