|
| 1 | +--- |
| 2 | +title: "Azure Apps Autopilot #2 - Deployment Script" |
| 3 | +slug: azure-bicep-deployment-script |
| 4 | +description: "In this post, I'm going to introduce Azure Bicep DeploymentScripts so that the autopilot feature is completely available through ARM." |
| 5 | +date: "2022-04-06" |
| 6 | +author: Justin-Yoo |
| 7 | +tags: |
| 8 | +- azure |
| 9 | +- autopilot |
| 10 | +- bicep |
| 11 | +- developer-experience |
| 12 | +cover: https://sa0blogs.blob.core.windows.net/devkimchi/2022/04/azure-bicep-deployment-script-00.png |
| 13 | +fullscreen: true |
| 14 | +--- |
| 15 | + |
| 16 | +In my [previous post][post 1], we built the autopilot feature, using various event triggers from [GitHub Actions][gh actions] and [Azure Bicep][az bicep]. Throughout this post, I'm going to build the revised autopilot feature using [Azure Bicep Deployment Scripts resource][az bicep ds] without GitHub Actions. |
| 17 | + |
| 18 | +> You can download the sample code from [this GitHub repository][gh sample]. |
| 19 | +
|
| 20 | +## Solution Architecture ## |
| 21 | + |
| 22 | +Let's say you're building a microservices architecture. It typically consists of an API gateway and many API apps, which are [Azure API Management (APIM)][az apim] and [Azure Functions][az fncapp] API in this example. The microservices architecture might be more complex depending on the requirements, but it's way too far from our topic. Therefore, let's build a minimal structure that is working. Here's the diagram. |
| 23 | + |
| 24 | +![Microservices Architecture on Azure][image-01] |
| 25 | + |
| 26 | +## Azure Resources Provisioning ## |
| 27 | + |
| 28 | +There are five resources to provision: |
| 29 | + |
| 30 | +* [Azure Storage][az st] 👉 [View Bicep code][gh sample st] |
| 31 | +* [Application Insights][az appins] 👉 [View Bicep code][gh sample appins] |
| 32 | +* [App Service Plan][az csplan] 👉 [View Bicep code][gh sample csplan] |
| 33 | +* [Function App][az fncapp] 👉 [View Bicep code][gh sample fncapp] |
| 34 | +* [Azure API Management][az apim] 👉 [View Bicep code][gh sample apim] |
| 35 | + |
| 36 | +As you can see, each resource has its corresponding Bicep module. For example, it's required to provision Azure Storage, Application Insights and App Service Plan (Consumption) before creating an Azure Functions app. Therefore, the [`provision_functionapp.bicep`][gh sample fncapp provision] file takes care of this orchestration. In addition to that, Azure API Management also needs Application Insights as a dependency, so the [`provision_apimamagement.bicep`][gh sample apim provision] file looks after this orchestration. |
| 37 | + |
| 38 | +After deploying the function app, the [`provision_apimanagementapi.bicep`][gh sample apim api provision] registers the function app to APIM. |
| 39 | + |
| 40 | +> **NOTE**: I used the modularisation approach while writing the Bicep files. But it may differ from your situation, and writing one big Bicep file could be more efficient in some circumstances. |
| 41 | +
|
| 42 | +Once you complete writing the Bicep file, you might expect the following processes in that order. |
| 43 | + |
| 44 | +1. Provision the function related resources 👉 [`provision_functionapp.bicep`][gh sample fncapp provision] |
| 45 | +2. Provision the APIM related resources 👉 [`provision_apimamagement.bicep`][gh sample apim provision] |
| 46 | +3. ➡️ Deploy the function app ⬅️ |
| 47 | +4. Integrate the function app with APIM 👉 [`provision_apimanagementapi.bicep`][gh sample apim api provision] |
| 48 | + |
| 49 | +When #1 and #2 are over, you can see the following resources provisioned. |
| 50 | + |
| 51 | +![Azure Resource Provisioning][image-02] |
| 52 | + |
| 53 | +By the way, #4 can't be provisioned until the function app is deployed at #3. Moreover, #3 is not related to resource provisioning but app deployment. What if we can convert this app deployment experience into resource provisioning? Then, all processes from #1 to #4 can be done within the resource provisioning pipeline, which means the whole "autopilot" feature is set. |
| 54 | + |
| 55 | +## Azure Bicep – Deployment Scripts ## |
| 56 | + |
| 57 | +[Azure Resource Manager (ARM)][az rm] has introduced the concept of [deployment script][az bicep ds]. Through this deployment script, ARM can include PowerShell scripts or bash scripts as a part of the resource provisioning pipeline. In other words, the deployment script resource can run [Azure PowerShell][az pwsh] or [Azure CLI][az cli]. How is that possible? Here's the Bicep file declaring the deployment script. |
| 58 | + |
| 59 | +```javascript |
| 60 | +resource ds 'Microsoft.Resources/deploymentScripts@2020-10-01' = { |
| 61 | + name: 'my-deployment-script' |
| 62 | + location: resourceGroup().location |
| 63 | + kind: 'AzureCLI' |
| 64 | + identity: { |
| 65 | + type: 'UserAssigned' |
| 66 | + userAssignedIdentities: { |
| 67 | + '<user-assigned-identity-id>': {} |
| 68 | + } |
| 69 | + } |
| 70 | + properties: { |
| 71 | + azCliVersion: '2.33.1' |
| 72 | + containerSettings: { |
| 73 | + containerGroupName: 'my-container-group' |
| 74 | + } |
| 75 | + environmentVariables: [ |
| 76 | + { |
| 77 | + name: 'RESOURCE_NAME' |
| 78 | + value: '<resource-name>' |
| 79 | + } |
| 80 | + ] |
| 81 | + primaryScriptUri: '<bash-script-url>' |
| 82 | + retentionInterval: 'P1D' |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +* The `kind` attribute explicitly declares that it uses Azure CLI. You can declare "AzurePowerShell" if you like. |
| 88 | +* The bash script needs the login credentials to run Azure CLI commands. Therefore, it uses the user-assigned identity. |
| 89 | +* The bash script uses the Azure CLI of `v2.33.1` in this example. It's recommended to use a specific version instead of the latest version, discussed later. |
| 90 | +* The bash script uses the environment variable of `RESOURCE_NAME`, which is declared and has the value assigned in the Bicep file. |
| 91 | +* The `primaryScriptUri` attribute declares the publicly accessible bash script URL. |
| 92 | + |
| 93 | +> **NOTE**: The Bicep file only shows the necessary bits and pieces. If you want to know more details, please take a look at [this Bicep file][gh sample ds]. |
| 94 | +
|
| 95 | +So, what does this deployment script resource do? It temporarily provisions both [Azure Container Instance][az aci] and [Storage Account][az st] to handle the script. According to [this document][az bicep ds], using the Azure CLI version older than 30 days from the day running the script is recommended. Therefore, at the time of this writing, [`v2.33.1`][az cli release notes] is the closest one. Of course, you can use the `az upgrade` command within the script to get the latest version, but it's totally up to you. |
| 96 | + |
| 97 | +## Deployment Script – Bash Script ## |
| 98 | + |
| 99 | +Take a look at the bash script that runs the series of [Azure CLI][az cli] commands. Let's say the artifact name is `api.zip`, and the following script gets the artifact URL stored in the GitHub repository at the beginning. |
| 100 | + |
| 101 | +```powershell |
| 102 | +# Get artifacts from GitHub |
| 103 | +urls=$(curl -H "Accept: application/vnd.github.v3+json" \ |
| 104 | + https://api.github.com/repos/devkimchi/APIM-OpenAPI-Sample/releases/latest | \ |
| 105 | + jq '.assets[] | { name: .name, url: .browser_download_url }') |
| 106 | +
|
| 107 | +apizip=$(echo $urls | jq 'select(.name == "api.zip") | .url' -r) |
| 108 | +``` |
| 109 | + |
| 110 | +As the artifact URL is set as the environment variable, `$apizip`, Azure CLI uses this artifact URL to deploy the function app. The environment variable, `$RESOURCE_NAME`, comes from the deployment script Bicep file. |
| 111 | + |
| 112 | +```powershell |
| 113 | +# Deploy function apps |
| 114 | +ipapp=$(az functionapp deploy \ |
| 115 | + -g rg-$RESOURCE_NAME \ |
| 116 | + -n fncapp-$RESOURCE_NAME-api \ |
| 117 | + --src-url $apizip \ |
| 118 | + --type zip) |
| 119 | +``` |
| 120 | + |
| 121 | +Once we complete deployment, it should be registered to APIM through the OpenAPI document. How can we do that? Another Bicep file, `provision-apimanagementapi.bicep`, can also be run within the bash script through Azure CLI. But make sure that, if you want to provision resources through URL, you MUST convert the Bicep file to an ARM template of the JSON type. |
| 122 | + |
| 123 | +```powershell |
| 124 | +# Provision APIs to APIM |
| 125 | +az deployment group create \ |
| 126 | + -n ApiManagement_Api \ |
| 127 | + -g rg-$RESOURCE_NAME \ |
| 128 | + # MUST be ARM template, not Bicep |
| 129 | + -u https://raw.githubusercontent.com/devkimchi/APIM-OpenAPI-Sample/main/Resources/provision-apimanagementapi.json \ |
| 130 | + -p name=$RESOURCE_NAME |
| 131 | +``` |
| 132 | + |
| 133 | +Now, we've got the bash script to run within the deployment script resource. |
| 134 | + |
| 135 | +## Overall Resource Orchestration ## |
| 136 | + |
| 137 | +As the final step, everything written above should be composed into one. Here's the [`main.bicep`][gh sample main] file that orchestrates resources and deployment scripts. The deployment script resource should always come last after all other resources are provisioned by using the `dependsOn` attribute. |
| 138 | + |
| 139 | +```javascript |
| 140 | +// Provision API Management |
| 141 | +module apim './provision-apimanagement.bicep' = { |
| 142 | + name: 'ApiManagement' |
| 143 | + params: { |
| 144 | + ... |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// Provision function app |
| 149 | +module fncapp './provision-functionapp.bicep' = { |
| 150 | + name: 'FunctionApp' |
| 151 | + dependsOn: [ |
| 152 | + apim |
| 153 | + ] |
| 154 | + params: { |
| 155 | + ... |
| 156 | + } |
| 157 | +}] |
| 158 | + |
| 159 | +// Provision deployment script |
| 160 | +module uai './deploymentScript.bicep' = { |
| 161 | + name: 'UserAssignedIdentity' |
| 162 | + dependsOn: [ |
| 163 | + apim |
| 164 | + fncapp |
| 165 | + ] |
| 166 | + params: { |
| 167 | + ... |
| 168 | + } |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +Once everything is done, convert the last Bicep file into the ARM template and link it to the image button below. Then, just click the button and put in the necessary information. It will automatically provision resources, deploy apps and do the rest of the provisioning process within one single pipeline, and the app is ready to use. |
| 173 | + |
| 174 | +[](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fdevkimchi%2FAPIM-OpenAPI-Sample%2Fmain%2FResources%2Fazuredeploy.json) |
| 175 | + |
| 176 | +If you actually click the button above, you will be able to see the Azure Portal screen like below: |
| 177 | + |
| 178 | +![Azure Portal Custom Template Provisioning][image-03] |
| 179 | + |
| 180 | +Did you complete all the steps? Then go to [APIM][az apim] and visit one of the function app's Swagger UI endpoint. |
| 181 | + |
| 182 | +![Azure Functions Swagger UI through APIM][image-04] |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +So far, we've walked through the [Azure Bicep][az bicep]'s [deployment script][az bicep ds] resources and revised the autopilot feature without needing to rely on [GitHub Actions][gh actions], as mentioned in the [previous post][post 1]. Now, you can hand over your repository to your sales representatives or other dev units to take a look with no knowledge of your application set-up. How easy is that? |
| 187 | + |
| 188 | + |
| 189 | +[image-01]: https://sa0blogs.blob.core.windows.net/devkimchi/2022/04/azure-bicep-deployment-script-01-en.png |
| 190 | +[image-02]: https://sa0blogs.blob.core.windows.net/devkimchi/2022/04/azure-bicep-deployment-script-02.png |
| 191 | +[image-03]: https://sa0blogs.blob.core.windows.net/devkimchi/2022/04/azure-bicep-deployment-script-03.png |
| 192 | +[image-04]: https://sa0blogs.blob.core.windows.net/devkimchi/2022/04/azure-bicep-deployment-script-04.png |
| 193 | + |
| 194 | + |
| 195 | +[post 1]: /2022/03/11/azure-apps-autopilot/ |
| 196 | +[post 2]: /2022/04/06/azure-bicep-deployment-script/ |
| 197 | + |
| 198 | +[az aci]: https://docs.microsoft.com/azure/container-instances/container-instances-overview?WT.mc_id=62548-dotnet-juyoo |
| 199 | +[az apim]: https://docs.microsoft.com/azure/api-management/api-management-key-concepts?WT.mc_id=62548-dotnet-juyoo |
| 200 | +[az appins]: https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview?WT.mc_id=62548-dotnet-juyoo |
| 201 | + |
| 202 | +[az bicep]: https://docs.microsoft.com/azure/azure-resource-manager/bicep/overview?tabs=bicep&WT.mc_id=62548-dotnet-juyoo |
| 203 | +[az bicep ds]: https://docs.microsoft.com/azure/azure-resource-manager/bicep/deployment-script-bicep?WT.mc_id=62548-dotnet-juyoo |
| 204 | + |
| 205 | +[az cli]: https://docs.microsoft.com/cli/azure/what-is-azure-cli?WT.mc_id=62548-dotnet-juyoo |
| 206 | +[az cli release notes]: https://docs.microsoft.com/cli/azure/release-notes-azure-cli?WT.mc_id=62548-dotnet-juyoo |
| 207 | + |
| 208 | +[az csplan]: https://docs.microsoft.com/azure/app-service/overview-hosting-plans?WT.mc_id=62548-dotnet-juyoo |
| 209 | +[az fncapp]: https://docs.microsoft.com/azure/azure-functions/functions-overview?WT.mc_id=62548-dotnet-juyoo |
| 210 | +[az rm]: https://docs.microsoft.com/azure/azure-resource-manager/management/overview?WT.mc_id=62548-dotnet-juyoo |
| 211 | +[az st]: https://docs.microsoft.com/azure/storage/common/storage-account-overview?WT.mc_id=62548-dotnet-juyoo |
| 212 | +[az pwsh]: https://docs.microsoft.com/powershell/azure/what-is-azure-powershell?WT.mc_id=62548-dotnet-juyoo |
| 213 | + |
| 214 | +[gh sample]: https://github.com/devkimchi/APIM-OpenAPI-Sample |
| 215 | +[gh sample st]: https://github.com/devkimchi/APIM-OpenAPI-Sample/blob/main/Resources/storageAccount.bicep |
| 216 | +[gh sample appins]: https://github.com/devkimchi/APIM-OpenAPI-Sample/blob/main/Resources/appInsights.bicep |
| 217 | +[gh sample csplan]: https://github.com/devkimchi/APIM-OpenAPI-Sample/blob/main/Resources/consumptionPlan.bicep |
| 218 | +[gh sample fncapp]: https://github.com/devkimchi/APIM-OpenAPI-Sample/blob/main/Resources/functionApp.bicep |
| 219 | +[gh sample apim]: https://github.com/devkimchi/APIM-OpenAPI-Sample/blob/main/Resources/apiManagement.bicep |
| 220 | +[gh sample fncapp provision]: https://github.com/devkimchi/APIM-OpenAPI-Sample/blob/main/Resources/provision-functionapp.bicep |
| 221 | +[gh sample apim provision]: https://github.com/devkimchi/APIM-OpenAPI-Sample/blob/main/Resources/provision-apimanagement.bicep |
| 222 | +[gh sample apim api provision]: https://github.com/devkimchi/APIM-OpenAPI-Sample/blob/main/Resources/provision-apimanagementapi.bicep |
| 223 | +[gh sample ds]: https://github.com/devkimchi/APIM-OpenAPI-Sample/blob/main/Resources/deploymentScript.bicep |
| 224 | +[gh sample main]: https://github.com/devkimchi/APIM-OpenAPI-Sample/blob/main/Resources/main.bicep |
| 225 | + |
| 226 | +[gh actions]: https://github.com/features/actions |
0 commit comments