Publishing OpenAPI Document from Azure Functions to Azure API Management within CI/CD Pipeline

Published Mar 04 2022 04:00 PM 1,725 Views
Microsoft

In my previous post, we've walked through how to run the Azure Functions app as a background process and locally generate the OpenAPI document from it within the GitHub Actions workflow. The OpenAPI document can be stored as an artifact and integrated with Power Platform or Azure API Management (APIM) later on.

 

 

Throughout this post, I'm going to discuss how to publish the OpenAPI document to APIM, after locally running the Azure Functions app as a background process and generating the OpenAPI document within the GitHub Actions workflow.

 

NOTE: Let's use the sample app provided by the Azure Functions OpenAPI Extension repository.

 

Update GitHub Actions Workflow

 

The GitHub Actions workflow built in my previous post looks like the following (line #21-35). Let's assume that we use the Ubuntu runner and PowerShell.

 

name: Build

on:
  push:

jobs:
  build_and_test:
    name: Build
    runs-on: 'ubuntu-latest'

    steps:
    - name: Build solution
      shell: pwsh
      run: |
        pushd MyFunctionApp

        dotnet build . -c Release -v minimal

        popd

    - name: Generate OpenAPI document
      shell: pwsh
      run: |
        cd MyFunctionApp

        Start-Process -NoNewWindow func @("start","--verbose","false")
        Start-Sleep -s 60

        Invoke-RestMethod -Method Get -Uri http://localhost:7071/api/swagger.json | ConvertTo-Json -Depth 100 | Out-File -FilePath outputs/swagger.json -Force

        Get-Content -Path outputs/swagger.json

        cd ..

 

The OpenAPI document generated from the pipeline above has https://localhost:7071/api as the default server URL. However, the actual server URL must be the format of https://<azure-functions-app>.azurewebsites.net/api after deployment. Therefore, although you haven't deployed the function app yet, the generated OpenAPI document must have the deployed app URL. According to the doc, the extension offers the feature to apply the server URL.

 

  • Set the environment variable, OpenApi__HostNames, so the actual server URL is added on top of localhost (line #4).
  • Set the environment variable, AZURE_FUNCTIONS_ENVIRONMENT, to Production so that localhost is omitted and only the server URL is rendered as if the function app is running on Azure (line #5).
  • Generally speaking, local.settings.json is excluded from the repository because it's supposed to use for local development. But, it's required to run the function app locally as a background process. Therefore, it's always a good idea to create one. The revised action below copies the local.settings.sample.json to local.settings.json (line #12).

 

With these three points in mind, let's update the GitHub Action (line #4,5,12).

 

    - name: Generate OpenAPI document
      shell: pwsh
      env:
        OpenApi__HostNames: 'https://<azure-functions-app>.azurewebsites.net/api'
        AZURE_FUNCTIONS_ENVIRONMENT: 'Production'
      run: |
        cd MyFunctionApp

        mkdir outputs

        # Create local.settings.json
        cp ./local.settings.sample.json ./local.settings.json

        Start-Process -NoNewWindow func @("start","--verbose","false")
        Start-Sleep -s 60

        Invoke-RestMethod -Method Get -Uri http://localhost:7071/api/swagger.json | ConvertTo-Json -Depth 100 | Out-File -FilePath outputs/swagger.json -Force

        Get-Content -Path outputs/swagger.json -Raw

        cd ..

 

As mentioned above, this action just copies the existing local.settings.sample.json file to local.settings.json, but it's not common. Therefore, in most cases, you should create the local.settings.json file by yourself, and it MUST include the environment variable, FUNCTIONS_WORKER_RUNTIME (line #3). In other words, the minimal structure of local.settings.json looks like below (assuming you use the in-proc worker):

 

{
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

 

Finally, you will see the server URL applied from the OpenAPI document (line #4,16).

 

// OpenAPI v2
{
  "swagger": "2.0",
  "host": "<azure-functions-app>.azurewebsites.net",
  "basePath": "/api",
  "schemes": [
    "https"
  ]
}

// OpenAPI v3
{
  "openapi": "3.0.1",
  "servers": [
    {
      "url": "https://<azure-functions-app>.azurewebsites.net/api"
    }
  ]
}

 

Now, we've got the OpenAPI document from the CI/CD pipeline, with the actual server URL, without deploying the function app.

 

Publish to Azure API Management

 

This time, let's publish the OpenAPI document to APIM. Although there are many ways to do it, let's use Azure Bicep and deploy it through Azure CLI in this post. Here's the first part of the bicep file. Use the existing keyword to get the existing APIM resource details.

 

// azuredeploy.bicep
param servicename string

resource apim 'Microsoft.ApiManagement/service@2021-08-01' existing = {
    name: servicename
    scope: resourceGroup(apiManagement.groupName)
}

 

Then, with these APIM details, declare the API resource.

 

// azuredeploy.bicep
param openapidoc string

resource apimapi 'Microsoft.ApiManagement/service/apis@2021-08-01' = {
    name: '${apim.name}/my-api'
    properties: {
        type: 'http'
        displayName: 'My API'
        description: 'This is my API.'
        path: 'myapi'
        subscriptionRequired: true
        format: 'openapi+json-link'
        value: openapidoc
    }
}

 

It looks simple, except those two attributes – format and value (line #12-13). Therefore, it's better to understand what those two attributes are. For more comprehensive details, you can visit the Azure Bicep Template Reference.

 

  1. TO use the JSON string parsed from the OpenAPI document:
    • OpenAPI v2: Set the format value to swagger-json.
    • OpenAPI v3: Set the format value to openapi+json.
    • Assign the JSON string to the value attribute.
  2. TO use the publicly accessible URL for the OpenAPI document:
    • OpenAPI v2: Set the format value to swagger-link-json.
    • OpenAPI v3: Set the format value to openapi+json-link.
    • Assign the publicly accessible URL to the value attribute.

 

Even if the first approach is doable, I wouldn't recommend it because it's too painful to parse the OpenAPI document to the JSON string that the bicep file can understand. Therefore, I would suggest using the second approach by storing the OpenAPI document to Azure Blob Storage right after it's generated from the pipeline.

 

Once you complete authoring the bicep file, run the Azure CLI command to deploy the API to APIM.

 

$servicename = "<my_apim_service_name>"
$openapidoc = https://<my_blob_storage_name>.blob.core.windows.net/<container>/openapi.json

az deployment group create `
    -g <resource_group_name> `
    -n <deployment_name> `
    -f ./azuredeploy.bicep `
    -p servicename=$servicename `
    -p openapidoc=$openapidoc

 

You can confirm that the API is correctly deployed to APIM on Azure Portal.


So far, we've walked through how:

 

  1. To run Azure Functions app as a background process,
  2. To generate the OpenAPI document from the app by applying the deployed server URL without actually deploying it, and
  3. To publish the OpenAPI document to APIM within GitHub Actions workflow.

 

By doing so, you will be able to reduce some maintenance overheads by automating the integration part.

%3CLINGO-SUB%20id%3D%22lingo-sub-3239611%22%20slang%3D%22en-US%22%3EPublishing%20OpenAPI%20Document%20from%20Azure%20Functions%20to%20Azure%20API%20Management%20within%20CI%2FCD%20Pipeline%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3239611%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20my%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fapps-on-azure-blog%2Fgenerating-openapi-document-from-azure-functions-within-ci-cd%2Fba-p%2F3193584%22%20target%3D%22_blank%22%3Eprevious%20post%3C%2FA%3E%2C%20we've%20walked%20through%20how%20to%20run%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-functions%2Ffunctions-overview%3FWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Functions%20app%3C%2FA%3E%20as%20a%20background%20process%20and%20locally%20generate%20the%20OpenAPI%20document%20from%20it%20within%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ffeatures%2Factions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EGitHub%20Actions%20workflow%3C%2FA%3E.%20The%20OpenAPI%20document%20can%20be%20stored%20as%20an%20artifact%20and%20integrated%20with%20%3CA%20href%3D%22https%3A%2F%2Fpowerplatform.microsoft.com%2F%3FWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EPower%20Platform%3C%2FA%3E%20or%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fapi-management%2Fapi-management-key-concepts%3FWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20API%20Management%20(APIM)%3C%2FA%3E%20later%20on.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fapps-on-azure-blog%2Fgenerating-openapi-document-from-azure-functions-within-ci-cd%2Fba-p%2F3193584%22%20target%3D%22_blank%22%3EGenerating%20OpenAPI%20Document%20from%20Azure%20Functions%20within%20CI%2FCD%20Pipeline%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CEM%3E%3CSTRONG%3EPublishing%20OpenAPI%20Document%20from%20Azure%20Functions%20to%20Azure%20API%20Management%20within%20CI%2FCD%20Pipeline%3C%2FSTRONG%3E%3C%2FEM%3E%20%3Abackhand_index_pointing_left%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThroughout%20this%20post%2C%20I'm%20going%20to%20discuss%20how%20to%20publish%20the%20OpenAPI%20document%20to%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fapi-management%2Fapi-management-key-concepts%3FWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAPIM%3C%2FA%3E%2C%20after%20locally%20running%20the%20Azure%20Functions%20app%20as%20a%20background%20process%20and%20generating%20the%20OpenAPI%20document%20within%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ffeatures%2Factions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EGitHub%20Actions%20workflow%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%3CSTRONG%3ENOTE%3C%2FSTRONG%3E%3A%20Let's%20use%20the%20sample%20app%20provided%20by%20the%20%3CA%20href%3D%22https%3A%2F%2Faka.ms%2Fazfunc-openapi%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Functions%20OpenAPI%20Extension%3C%2FA%3E%20repository.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22update-github-actions-workflow%22%20id%3D%22toc-hId-390214609%22%20id%3D%22toc-hId-415896474%22%3EUpdate%20GitHub%20Actions%20Workflow%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20GitHub%20Actions%20workflow%20built%20in%20my%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fapps-on-azure-blog%2Fgenerating-openapi-document-from-azure-functions-within-ci-cd%2Fba-p%2F3193584%22%20target%3D%22_blank%22%3Eprevious%20post%3C%2FA%3E%20looks%20like%20the%20following%20(line%20%2321-35).%20Let's%20assume%20that%20we%20use%20the%20Ubuntu%20runner%20and%20PowerShell.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-yaml%22%3E%3CCODE%3Ename%3A%20Build%0A%0Aon%3A%0A%20%20push%3A%0A%0Ajobs%3A%0A%20%20build_and_test%3A%0A%20%20%20%20name%3A%20Build%0A%20%20%20%20runs-on%3A%20'ubuntu-latest'%0A%0A%20%20%20%20steps%3A%0A%20%20%20%20-%20name%3A%20Build%20solution%0A%20%20%20%20%20%20shell%3A%20pwsh%0A%20%20%20%20%20%20run%3A%20%7C%0A%20%20%20%20%20%20%20%20pushd%20MyFunctionApp%0A%0A%20%20%20%20%20%20%20%20dotnet%20build%20.%20-c%20Release%20-v%20minimal%0A%0A%20%20%20%20%20%20%20%20popd%0A%0A%20%20%20%20-%20name%3A%20Generate%20OpenAPI%20document%0A%20%20%20%20%20%20shell%3A%20pwsh%0A%20%20%20%20%20%20run%3A%20%7C%0A%20%20%20%20%20%20%20%20cd%20MyFunctionApp%0A%0A%20%20%20%20%20%20%20%20Start-Process%20-NoNewWindow%20func%20%40(%22start%22%2C%22--verbose%22%2C%22false%22)%0A%20%20%20%20%20%20%20%20Start-Sleep%20-s%2060%0A%0A%20%20%20%20%20%20%20%20Invoke-RestMethod%20-Method%20Get%20-Uri%20http%3A%2F%2Flocalhost%3A7071%2Fapi%2Fswagger.json%20%7C%20ConvertTo-Json%20-Depth%20100%20%7C%20Out-File%20-FilePath%20outputs%2Fswagger.json%20-Force%0A%0A%20%20%20%20%20%20%20%20Get-Content%20-Path%20outputs%2Fswagger.json%0A%0A%20%20%20%20%20%20%20%20cd%20..%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20OpenAPI%20document%20generated%20from%20the%20pipeline%20above%20has%3CCODE%3E%3CA%20href%3D%22https%3A%2F%2Flocalhost%3A7071%2Fapi%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flocalhost%3A7071%2Fapi%3C%2FA%3E%3C%2FCODE%3Eas%20the%20default%20server%20URL.%20However%2C%20the%20actual%20server%20URL%20must%20be%20the%20format%20of%3CCODE%3Ehttps%3A%2F%2F%3CAZURE-FUNCTIONS-APP%3E.azurewebsites.net%2Fapi%3C%2FAZURE-FUNCTIONS-APP%3E%3C%2FCODE%3Eafter%20deployment.%20Therefore%2C%20although%20you%20haven't%20deployed%20the%20function%20app%20yet%2C%20the%20generated%20OpenAPI%20document%20must%20have%20the%20deployed%20app%20URL.%20According%20to%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-functions-openapi-extension%2Fblob%2Fmain%2Fdocs%2Fopenapi.md%23configure-custom-base-urls%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Edoc%3C%2FA%3E%2C%20the%20extension%20offers%20the%20feature%20to%20apply%20the%20server%20URL.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ESet%20the%20environment%20variable%2C%3CCODE%3EOpenApi__HostNames%3C%2FCODE%3E%2C%20so%20the%20actual%20server%20URL%20is%20added%20on%20top%20of%3CCODE%3Elocalhost%3C%2FCODE%3E(line%20%234).%3C%2FLI%3E%0A%3CLI%3ESet%20the%20environment%20variable%2C%3CCODE%3EAZURE_FUNCTIONS_ENVIRONMENT%3C%2FCODE%3E%2C%20to%3CCODE%3EProduction%3C%2FCODE%3Eso%20that%3CCODE%3Elocalhost%3C%2FCODE%3Eis%20omitted%20and%20only%20the%20server%20URL%20is%20rendered%20as%20if%20the%20function%20app%20is%20running%20on%20Azure%20(line%20%235).%3C%2FLI%3E%0A%3CLI%3EGenerally%20speaking%2C%3CCODE%3Elocal.settings.json%3C%2FCODE%3Eis%20excluded%20from%20the%20repository%20because%20it's%20supposed%20to%20use%20for%20local%20development.%20But%2C%20it's%20required%20to%20run%20the%20function%20app%20locally%20as%20a%20background%20process.%20Therefore%2C%20it's%20always%20a%20good%20idea%20to%20create%20one.%20The%20revised%20action%20below%20copies%20the%3CCODE%3Elocal.settings.sample.json%3C%2FCODE%3Eto%3CCODE%3Elocal.settings.json%3C%2FCODE%3E(line%20%2312).%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20these%20three%20points%20in%20mind%2C%20let's%20update%20the%20GitHub%20Action%20(line%20%234%2C5%2C12).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-yaml%22%3E%3CCODE%3E%20%20%20%20-%20name%3A%20Generate%20OpenAPI%20document%0A%20%20%20%20%20%20shell%3A%20pwsh%0A%20%20%20%20%20%20env%3A%0A%20%20%20%20%20%20%20%20OpenApi__HostNames%3A%20'https%3A%2F%2F%3CAZURE-FUNCTIONS-APP%3E.azurewebsites.net%2Fapi'%0A%20%20%20%20%20%20%20%20AZURE_FUNCTIONS_ENVIRONMENT%3A%20'Production'%0A%20%20%20%20%20%20run%3A%20%7C%0A%20%20%20%20%20%20%20%20cd%20MyFunctionApp%0A%0A%20%20%20%20%20%20%20%20mkdir%20outputs%0A%0A%20%20%20%20%20%20%20%20%23%20Create%20local.settings.json%0A%20%20%20%20%20%20%20%20cp%20.%2Flocal.settings.sample.json%20.%2Flocal.settings.json%0A%0A%20%20%20%20%20%20%20%20Start-Process%20-NoNewWindow%20func%20%40(%22start%22%2C%22--verbose%22%2C%22false%22)%0A%20%20%20%20%20%20%20%20Start-Sleep%20-s%2060%0A%0A%20%20%20%20%20%20%20%20Invoke-RestMethod%20-Method%20Get%20-Uri%20http%3A%2F%2Flocalhost%3A7071%2Fapi%2Fswagger.json%20%7C%20ConvertTo-Json%20-Depth%20100%20%7C%20Out-File%20-FilePath%20outputs%2Fswagger.json%20-Force%0A%0A%20%20%20%20%20%20%20%20Get-Content%20-Path%20outputs%2Fswagger.json%20-Raw%0A%0A%20%20%20%20%20%20%20%20cd%20..%0A%3C%2FAZURE-FUNCTIONS-APP%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20mentioned%20above%2C%20this%20action%20just%20copies%20the%20existing%3CCODE%3Elocal.settings.sample.json%3C%2FCODE%3Efile%20to%3CCODE%3Elocal.settings.json%3C%2FCODE%3E%2C%20but%20it's%20not%20common.%20Therefore%2C%20in%20most%20cases%2C%20you%20should%20create%20the%3CCODE%3Elocal.settings.json%3C%2FCODE%3Efile%20by%20yourself%2C%20and%20it%20MUST%20include%20the%20environment%20variable%2C%3CCODE%3EFUNCTIONS_WORKER_RUNTIME%3C%2FCODE%3E(line%20%233).%20In%20other%20words%2C%20the%20minimal%20structure%20of%3CCODE%3Elocal.settings.json%3C%2FCODE%3Elooks%20like%20below%20(assuming%20you%20use%20the%20in-proc%20worker)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%20%20%22Values%22%3A%20%7B%0A%20%20%20%20%22FUNCTIONS_WORKER_RUNTIME%22%3A%20%22dotnet%22%0A%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFinally%2C%20you%20will%20see%20the%20server%20URL%20applied%20from%20the%20OpenAPI%20document%20(line%20%234%2C16).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%2F%2F%20OpenAPI%20v2%0A%7B%0A%20%20%22swagger%22%3A%20%222.0%22%2C%0A%20%20%22host%22%3A%20%22%3CAZURE-FUNCTIONS-APP%3E.azurewebsites.net%22%2C%0A%20%20%22basePath%22%3A%20%22%2Fapi%22%2C%0A%20%20%22schemes%22%3A%20%5B%0A%20%20%20%20%22https%22%0A%20%20%5D%0A%7D%0A%0A%2F%2F%20OpenAPI%20v3%0A%7B%0A%20%20%22openapi%22%3A%20%223.0.1%22%2C%0A%20%20%22servers%22%3A%20%5B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%22url%22%3A%20%22https%3A%2F%2F%3CAZURE-FUNCTIONS-APP%3E.azurewebsites.net%2Fapi%22%0A%20%20%20%20%7D%0A%20%20%5D%0A%7D%0A%3C%2FAZURE-FUNCTIONS-APP%3E%3C%2FAZURE-FUNCTIONS-APP%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%2C%20we've%20got%20the%20OpenAPI%20document%20from%20the%20CI%2FCD%20pipeline%2C%20with%20the%20actual%20server%20URL%2C%20without%20deploying%20the%20function%20app.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22publish-to-azure-api-management%22%20id%3D%22toc-hId--1417239854%22%20id%3D%22toc-hId--1391557989%22%3EPublish%20to%20Azure%20API%20Management%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20time%2C%20let's%20publish%20the%20OpenAPI%20document%20to%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fapi-management%2Fapi-management-key-concepts%3FWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAPIM%3C%2FA%3E.%20Although%20there%20are%20many%20ways%20to%20do%20it%2C%20let's%20use%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-resource-manager%2Fbicep%2Foverview%3Ftabs%3Dbicep%26amp%3BWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Bicep%3C%2FA%3E%20and%20deploy%20it%20through%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fcli%2Fazure%2Fwhat-is-azure-cli%3FWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20CLI%3C%2FA%3E%20in%20this%20post.%20Here's%20the%20first%20part%20of%20the%20bicep%20file.%20Use%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-resource-manager%2Fbicep%2Fexisting-resource%3FWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CCODE%3Eexisting%3C%2FCODE%3Ekeyword%3C%2FA%3E%20to%20get%20the%20existing%20APIM%20resource%20details.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bicep%22%3E%3CCODE%3E%2F%2F%20azuredeploy.bicep%0Aparam%20servicename%20string%0A%0Aresource%20apim%20'Microsoft.ApiManagement%2Fservice%402021-08-01'%20existing%20%3D%20%7B%0A%20%20%20%20name%3A%20servicename%0A%20%20%20%20scope%3A%20resourceGroup(apiManagement.groupName)%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThen%2C%20with%20these%20APIM%20details%2C%20declare%20the%20API%20resource.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bicep%22%3E%3CCODE%3E%2F%2F%20azuredeploy.bicep%0Aparam%20openapidoc%20string%0A%0Aresource%20apimapi%20'Microsoft.ApiManagement%2Fservice%2Fapis%402021-08-01'%20%3D%20%7B%0A%20%20%20%20name%3A%20'%24%7Bapim.name%7D%2Fmy-api'%0A%20%20%20%20properties%3A%20%7B%0A%20%20%20%20%20%20%20%20type%3A%20'http'%0A%20%20%20%20%20%20%20%20displayName%3A%20'My%20API'%0A%20%20%20%20%20%20%20%20description%3A%20'This%20is%20my%20API.'%0A%20%20%20%20%20%20%20%20path%3A%20'myapi'%0A%20%20%20%20%20%20%20%20subscriptionRequired%3A%20true%0A%20%20%20%20%20%20%20%20format%3A%20'openapi%2Bjson-link'%0A%20%20%20%20%20%20%20%20value%3A%20openapidoc%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20looks%20simple%2C%20except%20those%20two%20attributes%20%E2%80%93%3CCODE%3Eformat%3C%2FCODE%3Eand%3CCODE%3Evalue%3C%2FCODE%3E(line%20%2312-13).%20Therefore%2C%20it's%20better%20to%20understand%20what%20those%20two%20attributes%20are.%20For%20more%20comprehensive%20details%2C%20you%20can%20visit%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Ftemplates%2Fmicrosoft.apimanagement%2Fservice%2Fapis%3FWT.mc_id%3Ddotnet-59289-juyoo%26amp%3Btabs%3Dbicep%23apicreateorupdateproperties%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Bicep%20Template%20Reference%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ETO%20use%20the%20JSON%20string%20parsed%20from%20the%20OpenAPI%20document%3A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EOpenAPI%20v2%3C%2FSTRONG%3E%3A%20Set%20the%3CCODE%3Eformat%3C%2FCODE%3Evalue%20to%3CCODE%3Eswagger-json%3C%2FCODE%3E.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EOpenAPI%20v3%3C%2FSTRONG%3E%3A%20Set%20the%3CCODE%3Eformat%3C%2FCODE%3Evalue%20to%3CCODE%3Eopenapi%2Bjson%3C%2FCODE%3E.%3C%2FLI%3E%0A%3CLI%3EAssign%20the%20JSON%20string%20to%20the%3CCODE%3Evalue%3C%2FCODE%3Eattribute.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3CLI%3ETO%20use%20the%20publicly%20accessible%20URL%20for%20the%20OpenAPI%20document%3A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EOpenAPI%20v2%3C%2FSTRONG%3E%3A%20Set%20the%3CCODE%3Eformat%3C%2FCODE%3Evalue%20to%3CCODE%3Eswagger-link-json%3C%2FCODE%3E.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EOpenAPI%20v3%3C%2FSTRONG%3E%3A%20Set%20the%3CCODE%3Eformat%3C%2FCODE%3Evalue%20to%3CCODE%3Eopenapi%2Bjson-link%3C%2FCODE%3E.%3C%2FLI%3E%0A%3CLI%3EAssign%20the%20publicly%20accessible%20URL%20to%20the%3CCODE%3Evalue%3C%2FCODE%3Eattribute.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEven%20if%20the%20first%20approach%20is%20doable%2C%20I%20wouldn't%20recommend%20it%20because%20it's%20too%20painful%20to%20parse%20the%20OpenAPI%20document%20to%20the%20JSON%20string%20that%20the%20bicep%20file%20can%20understand.%20Therefore%2C%20I%20would%20suggest%20using%20the%20second%20approach%20by%20storing%20the%20OpenAPI%20document%20to%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fstorage%2Fblobs%2Fstorage-blobs-introduction%3FWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Blob%20Storage%3C%2FA%3E%20right%20after%20it's%20generated%20from%20the%20pipeline.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20you%20complete%20authoring%20the%20bicep%20file%2C%20run%20the%20Azure%20CLI%20command%20to%20deploy%20the%20API%20to%20APIM.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24servicename%20%3D%20%22%3CMY_APIM_SERVICE_NAME%3E%22%0A%24openapidoc%20%3D%20https%3A%2F%2F%3CMY_BLOB_STORAGE_NAME%3E.blob.core.windows.net%2F%3CCONTAINER%3E%2Fopenapi.json%0A%0Aaz%20deployment%20group%20create%20%60%0A%20%20%20%20-g%20%3CRESOURCE_GROUP_NAME%3E%20%60%0A%20%20%20%20-n%20%3CDEPLOYMENT_NAME%3E%20%60%0A%20%20%20%20-f%20.%2Fazuredeploy.bicep%20%60%0A%20%20%20%20-p%20servicename%3D%24servicename%20%60%0A%20%20%20%20-p%20openapidoc%3D%24openapidoc%0A%3C%2FDEPLOYMENT_NAME%3E%3C%2FRESOURCE_GROUP_NAME%3E%3C%2FCONTAINER%3E%3C%2FMY_BLOB_STORAGE_NAME%3E%3C%2FMY_APIM_SERVICE_NAME%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20confirm%20that%20the%20API%20is%20correctly%20deployed%20to%20APIM%20on%20Azure%20Portal.%3C%2FP%3E%0A%3CHR%20%2F%3E%0A%3CP%3ESo%20far%2C%20we've%20walked%20through%20how%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ETo%20run%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-functions%2Ffunctions-overview%3FWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Functions%20app%3C%2FA%3E%20as%20a%20background%20process%2C%3C%2FLI%3E%0A%3CLI%3ETo%20generate%20the%20OpenAPI%20document%20from%20the%20app%20by%20applying%20the%20deployed%20server%20URL%20without%20actually%20deploying%20it%2C%20and%3C%2FLI%3E%0A%3CLI%3ETo%20publish%20the%20OpenAPI%20document%20to%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fapi-management%2Fapi-management-key-concepts%3FWT.mc_id%3Ddotnet-59289-juyoo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAPIM%3C%2FA%3E%20within%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ffeatures%2Factions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EGitHub%20Actions%20workflow%3C%2FA%3E.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBy%20doing%20so%2C%20you%20will%20be%20able%20to%20reduce%20some%20maintenance%20overheads%20by%20automating%20the%20integration%20part.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-3239611%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22publishing-openapi-doc-from-azfunc-to-apim-within-cicd-pipeline-00.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F352283iB70F182CC2446421%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22publishing-openapi-doc-from-azfunc-to-apim-within-cicd-pipeline-00.png%22%20alt%3D%22publishing-openapi-doc-from-azfunc-to-apim-within-cicd-pipeline-00.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3E%0A%3CDIV%3E%3CSPAN%3EIn%20this%20post%2C%20I'm%20going%20to%20discuss%20how%20to%20publish%20the%20OpenAPI%20doc%20automatically%20generated%20from%20the%20Azure%20Function%20app%2C%20within%20the%20CI%2FCD%20pipeline.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3239611%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3E.NET%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Resource%20Manager%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDevOps%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EServerless%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Mar 04 2022 04:00 PM
Updated by: