azure function on containerapps

Adrian Ruchti 20 Reputation points
2024-12-21T06:42:04.0066667+00:00

Good morning, I have a bicep files azurefunction.bice and env.bicep. the provisioning works fine. once I am trying to deploy the functionapp service (ref in azure.yaml) on the function hosted on containerapp I am getting the following error:
Error: Deploying services (azd deploy)

(x) Failed: Deploying service functionapp

ERROR: error executing step command 'deploy --all': failed deploying service 'functionapp': validating target resource: resource 'func-gfne4osnh4yxi' with type 'Microsoft.Web/sites' does not match expected resource type 'Microsoft.App/containerApps'

azurefunction.bicep
resource`` functionapp 'Microsoft.Web/sites@2023-01-01' = {

``name: 'func-${uniqueString(prefix, subscription().id)}'

location``: location

kind``: 'functionapp,linux,container,azurecontainerapps'

identity``: identityInfo

tags``: tags

properties``: {

managedEnvironmentId``: environmentId

siteConfig``: {

linuxFxVersion``: 'DOCKER|${acrUrl}/azurefunctionsimage:v1.0.0'

acrUseManagedIdentityCreds``: !useSystemIdentity

acrUserManagedIdentityID``: useSystemIdentity ? '': identityResourceId

minimumElasticInstanceCount``: 1

functionAppScaleLimit``: 5

appSettings``: [

{

name``: 'AzureWebJobsStorage'

value``: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${listKeys(storageAccount.id, storageAccount.apiVersion).keys[0].value}'

}

{

name``: 'FUNCTIONS_EXTENSION_VERSION'

value``: '~4'

}

{

name``: 'APPLICATIONINSIGHTS_CONNECTION_STRING'

value``: appInsights.properties.ConnectionString

}

{

name``: 'DOCKER_REGISTRY_SERVER_URL'

value``: acrUrl

}

{

name``: 'WEBSITES_ENABLE_APP_SERVICE_STORAGE'

value``: 'false'

}

{

name``: 'AzureWebJobsStorage__clientId'

value``: identityClientId

}

]

}

}

}

env.bicep
resource`` environment 'Microsoft.App/managedEnvironments@2023-05-01' = {

``name: '${envResourceNamePrefix}-env'

location``: location

properties``: {

daprAIInstrumentationKey``: appInsightsInstrumentationKey

appLogsConfiguration``: {

destination``: 'log-analytics'

logAnalyticsConfiguration``: {

customerId``: customerId

sharedKey``: logAnalyticsSharedKey

}

}

workloadProfiles``: [

{

name``: 'Consumption'

workloadProfileType``: 'consumption'

}

{

name``: 'd4'

workloadProfileType``: 'D4'

minimumCount``: 1

maximumCount``: 3

}

]

}

}

output`` environmentId string = environment.id

azure.yaml
name``: postgres-timescale

metadata``:

template``: postgres-timescale@0.0.2-beta

requiredVersions``:

azd``: ">= 1.9.5"

services``:
functionapp``:

``project: ./src/azure_functions_app

language``: py

module``: functionapp

host``: containerapp

docker``:

path``: ./Dockerfile

context``: .

registry``: ${AZURE_CONTAINER_REGISTRY_ENDPOINT}

image``: azurefunctionsimage

tag``: v1.0.0

hooks``:

predeploy``:

posix``:

shell``: sh

run``: |

echo "Building Docker image for the functionapp service..."

docker build -t ${AZURE_CONTAINER_REGISTRY_ENDPOINT}/azurefunctionsimage:v1.0.0 .

echo "Tagging Docker image for the functionapp service..."

docker tag ${AZURE_CONTAINER_REGISTRY_ENDPOINT}/azurefunctionsimage:v1.0.0 ${AZURE_CONTAINER_REGISTRY_ENDPOINT}/azurefunctionsimage:v1.0.0

echo "Pushing Docker image to Azure Container Registry..."

az acr login --name ${AZURE_CONTAINER_REGISTRY_NAME}

docker push ${AZURE_CONTAINER_REGISTRY_ENDPOINT}/azurefunctionsimage:v1.0.0

postdeploy``:

posix``:

shell``: sh

run``: |

echo "Deploying Event Grid subscription..."

az deployment group create \

--name eventGridSubscriptionDeployment \

--resource-group $(azd env get-values RESOURCE_GROUP_NAME) \

--template-file ./infra/core/eventgrid/eventgrid.bicep \

--parameters \

eventGridTopicName=$(azd env get-values AZURE_EVENTGRID_TOPIC_NAME) \

functionAppName=$(azd env get-values AZURE_FUNCTIONAPP_NAME) \

location=$(azd env get-values AZURE_LOCATION)

I would appreciate some help. I have read the docs, checked azure-samples on github but most of them are using appservice as host. Thank you.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,256 questions
Azure Container Apps
Azure Container Apps
An Azure service that provides a general-purpose, serverless container platform.
487 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Vinodh247 26,371 Reputation points MVP
    2024-12-22T02:43:31.33+00:00

    Hi ,

    Thanks for reaching out to Microsoft Q&A.

    The error indicates a mismatch between the resource type expected (Microsoft.App/containerApps) and the one deployed (Microsoft.Web/sites). This occurs because you are trying to use Azure Container Apps as the hosting environment, but your azurefunction.bicep file is configured for a Function App running on App Service.

    Here’s how you can resolve this issue:

    1. Review and Update azurefunction.bicep

    To use Azure Functions with Azure Container Apps, your resource definition should point to the Microsoft.App/containerApps resource type, not Microsoft.Web/sites.

    Replace your existing functionapp resource definition with one that uses Microsoft.App/containerApps.

    For example:

    resource functionApp 'Microsoft.App/containerApps@2023-05-01' = {
        name: 'func-${uniqueString(prefix, subscription().id)}'
        location: location
        properties: {
            managedEnvironmentId: environmentId
            configuration: {
                secrets: [
                    {
                        name: 'AzureWebJobsStorage'
                        value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${listKeys(storageAccount.id, storageAccount.apiVersion).keys[0].value}'
                    }
                ]
                activeRevisionsMode: 'Single'
                ingress: {
                    external: true
                    targetPort: 80
                    transport: 'auto'
                }
                registries: [
                    {
                        server: acrUrl
                        username: ''
                        passwordSecretRef: 'acr-password'
                    }
                ]
                dapr: {
                    enabled: false
                }
            }
            template: {
                containers: [
                    {
                        name: 'functionapp'
                        image: '${acrUrl}/azurefunctionsimage:v1.0.0'
                        resources: {
                            cpu: 0.25
                            memory: '0.5Gi'
                        }
                        env: [
                            {
                                name: 'AzureWebJobsStorage'
                                value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${listKeys(storageAccount.id, storageAccount.apiVersion).keys[0].value}'
                            }
                            {
                                name: 'FUNCTIONS_EXTENSION_VERSION'
                                value: '~4'
                            }
                        ]
                    }
                ]
            }
        }
    }
    
    
    1. Ensure env.bicep Is Correct

    Ensure the managedEnvironments resource in your env.bicep is correctly configured and that environmentId in azurefunction.bicep references the correct managed environment.

    1. Update azure.yaml

    The host: containerapp configuration is correct. Ensure the docker section aligns with the updated resource definition and paths.

    1. Verify Dependencies
    • Ensure that the azd CLI version is compatible with your configuration (>= 1.9.5 is required).
    • Ensure the container image (azurefunctionsimage:v1.0.0) exists in your Azure Container Registry and is correctly referenced.
    1. Deploy Again

    Run the following command to redeploy your environment -->azd deploy

    1. Debugging Tips
    • If the error persists, validate all resource configurations in the Azure Portal.
    • Use az resource list to confirm that resources are created under the expected types (Microsoft.App/containerApps).

    These steps should align your deployment with the hosting requirements for Azure Container Apps.

    Please feel free to click the 'Upvote' (Thumbs-up) button and 'Accept as Answer'. This helps the community by allowing others with similar queries to easily find the solution.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.