Linking App Service backend to APIM with Bicep

Dmitry Nechaev 20 Reputation points
2024-12-04T05:19:47.4933333+00:00

I got a simple task that is killing me as information is almost impossible to find.

There is an API created manually in APIM, that I try coding in Bicep

I need to set the backend, and I cannot.User's image

The backend is an app service.

clicking on policies gives this XML, in which "WebApp_myjax_marlin_api_dev" is nowhere to be found in the APIM, it is something internal and not exposed.User's image

Following whichever samples I can find, I'm creating following Bicep for the APIM

// APIM
resource apiManagementService 'Microsoft.ApiManagement/service@2024-05-01' = {
  location: resourceGroup().location
  name: 'myjax-api-apim${suffix}'
  sku: {
    name: isTestEnv ? 'Developer' : 'Basic'
    capacity: 1
  }
  properties: {
    publisherEmail: ''
    publisherName: ''
  }

  
  @description('Create the backend for the app service')
  resource marlinApiBackend 'backends@2022-08-01' = {
    name: 'marlin-api-backend'
    properties: {
      protocol: 'http'
      url: 'https://${webApp.properties.defaultHostName}'
      resourceId: uri(environment().resourceManager, webApp.id)
    }
  }

  @description('create APIM API for app service')
  resource marlinApi 'apis@2022-08-01' = {
    name: 'marlin-api'
    properties: {
      path: ''
      displayName: 'myjax-marlin-api-dev'
      subscriptionRequired: false
      protocols: ['https']
    }

    @description('Create a policy for the Marlin API  - linking the app service backend')
    resource marlinApiBackendPolicy 'policies@2022-08-01' = {
      name: 'policy'
      properties: {
        value: '''<policies> 
            <inbound>
                <base />
                <set-backend-service id="webapp-backend-policy" backend-id="marlin-api" />
            </inbound>
            <backend>
                <base />
            </backend>
            <outbound>
                <base />
            </outbound>
            <on-error>
                <base />
            </on-error>
        </policies>'''
        format: 'xml'
      }
    }  

the problem is with the "backend-id"

it neither wants seeing name of the api, not of the backend.

[{"code":"ValidationError","target":"set-backend-service","message":"Error in element 'set-backend-service' on line 4, column 18: Backend with id 'marlin-api-backend' could not be found."}]

or

[{"code":"ValidationError","target":"set-backend-service","message":"Error in element 'set-backend-service' on line 4, column 18: Backend with id 'marlin-api' could not be found."}]

Can someone please help!!!

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,239 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
340 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,097 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Dmitry Nechaev 20 Reputation points
    2024-12-04T05:45:17.51+00:00

    Posting this forum somehow helps to find the answer

    Backends can be found on the APIM "backends" section, and the problem i had was probably a combination of typo and timing.

    As marlinApiBackendPolicy does not contain direct references to the backend object, it requires "dependsOn" flag or string replacement in the XML.

          dependsOn: [marlinApiBackend]
    
    0 comments No comments

  2. VenkateshDodda-MSFT 23,141 Reputation points Microsoft Employee
    2024-12-04T11:35:37.06+00:00

    @Dmitry Nechaev

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to accept the answer .

    Error Message:

    • [{"code":"ValidationError","target":"set-backend-service","message":"Error in element 'set-backend-service' on line 4, column 18: Backend with id 'marlin-api-backend' could not be found."}]

    Issue:

    • Unable to link App Service backend to APIM with Bicep

    Solution:

    Backends can be found on the APIM "backends" section, and the problem i had was probably a combination of typo and timing.

    • As marlinApiBackendPolicy does not contain direct references to the backend object, it requires "dependsOn" flag or string replacement in the XML.
        dependsOn: [marlinApiBackend]
      

    If I missed anything please let me know and I'd be happy to add it to my answer, or feel free to comment below with any additional information.

    I hope this helps!

    If you have any other questions, please let me know.

    Please accept as Yes if the answer is helpful so that it can help others in the community.


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.