Azure Function code deleted after a new ARM deployment from Azure DevOps - azure

I am running a custom ARM template to create the infrastructure (Dynamic App Service plan, Storage account etc.) for an Azure function through an Azure DevOps Pipeline.
I am also deploying my function code via Azure DevOps from a different Github repo in a different Azure DevOps pipeline.
After I've successfully deployed the infrastructure and code if the pipeline for the infrastructure runs again it deletes the code, even if the changes I am making to the infrastructure don't effect the function and I run in Incremental mode.

It turns out the issue was a missing setting.
https://learn.microsoft.com/en-ca/azure/azure-functions/run-functions-from-deployment-package
When an ARM template runs it replaces an the app settings for an app service (for Functions too).
I didn't realize that the Azure Dev Ops task for deploying function code is actually using this new run from package deployment method so I should have had the setting in the app settings portion of my ARM template.
When I ran the ARM template a second time after the code had been deployed it was removing the WEBSITE_RUN_FROM_PACKAGE setting so that the function didn't know where the code was.
To fix the issue I simply added { "name": "WEBSITE_RUN_FROM_PACKAGE", "value": "1" } to my ARM template!
Note this is very similar to this question. I wanted to call out that the Azure DevOps task for Functions now uses Run from Package: Azure Functions ARM Template deploy deletes Functions

Related

I've generated resources using Azure Bicep in ADO pipelines. Need to make a new pipeline that will populate the resources with code. Not sure how

I've got a bicep file that has made the following resources: Function app, storage account, app insights, key vault and app service plan, via a build pipeline in Azure Dev Ops Pipelines. I need to populate these resources with artifacts built from source code e.g. adding functions to the function app. I'm not sure how to do this though. Does anyone have any advice or links to tutorials? I'm new to Azure and would appreciate any help. Thanks.
Basically, Azure pipelines working with tasks to deploy artifacts to the resources. These resources can be AKS, IIS or Azure functions as you mentioned. If you particularly looking for Azure function deployment task you can use the AzureFunction#1 task for that.
Here is some additional useful links;
https://www.azuredevopslabs.com/labs/vstsextend/azurefunctions/#:~:text=Azure%20Functions%20is%20an%20event,well%20as%20on%2Dpremises%20systems.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-azure-devops?tabs=dotnet-core%2Cyaml%2Ccsharp

Azure DevOps - Multiple Azure Function Deployments Replaces Each Other in a Single Function App Service

I have 2 .NET 5 Azure Function Visual Studio projects . These 2 Functions to be deployed in a single Azure Function App.
I have 2 separate Build Pipelines for each of the projects and have separate Release Pipelines as well in Azure DevOps. I am not using YAML for release pipelines but depends on DevOps UI to choose a new Release pipeline.
My issue is that whenever running the Release pipeline, the last Release overrides the existing Function in Azure Function App. So, I am not getting 2 Functions as expected under a single Azure Function App. Always the last release deployment exists in Azure. Why one release deployment overrides existing Functions, even though they are from different Release pipelines?
In the MS documentation it states that if you perform a "Zip Deployment" to an Azure Function, it will always overwrite what is running on the Azure Function Service. I presume that the "Classic Pipeline" gives you the "Zip Deployment" option.
Link to MS Documentation: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-function-app?view=azure-devops#deployment-methods
For the moment, I do have a ticket open at Microsoft on how to manage different apps on the same Azure Function service. I only want changes made to the specific app where there are code changes without impacting any other existing apps on the Azure Function. For now, I only had one troubleshooting session with no solution in sight. I will share more information once I have received a solution/workaround.

How to deploy the specific changes of ARM template code into specific azure resources?

I am working on Azure ARM templates. I created the ARM templates for azure web app and azure SQL database, for that I configured the Continuous Integration and Continuous Deployment successfully without any issues.
After one month, I added the one more application settings for web app and added the new firewall rule for azure SQL database and check in the code into VSTS it’ automatically triggered the build and then release.
After every release the ARM template code deployed into azure resources. So, that’s why azure resources recreated once again. But I want to deploy only the changes of ARM template of azure web app and SQL database etc….
I have few doubts in this
I created the azure resources using ARM templates through the CI & CD process from VSTS. After that I deployed the application code into azure web app, after few days I added the some code in web app ARM template and deployed it into azure. After redeployed the ARM templates once again then the application code is also gone. For that what can I follow the better approach?
How to deploy the specific changes of ARM templates code in to azure, once I deployed the primary ARM template first.
You need to make sure the code is also being deployed with the ARM Template, because it adjusts the configuration to what is defined in the template. whats why you see such a behavior.
Just edit the template and redeploy into the same resource group. that's what they are for.

Enterprise standards for deploying the ARM templates along with source code in to azure environment using VSTS?

I working on Azure ARM templates, I developed few ARM templates for production environment it includes vnet, subnet, web app, API app, storage and database etc...
I had the source code contains the MVC application and Azure API Application and check in the code into VSTS team repository.
Source code managed in VS2017:
Currently I created Build and Release for source code along with ARM templates in single build and release by following this link.
I have few questions in my mind after implemented above process in my current environment:
What are best industry practices to manage the source code and ARM templates in VS2017?
How to know which version of ARM template deployed in specified resource group?
For suppose if someone changes the application code only, then also ARM template deployed along with modified source code into azure. is it recommended approach?
Is it recommended approach for deploying the source code along with ARM templates at time by creating single build and release definition In VSTS?
Is it recommended way to give application settings and connection strings in web app and API app arm templates?
What is the enterprise implementations to deploy the ARM templates and source code into azure environment using VSTS?
You can deploy ARM and web application in the same build/release, simple workflow:
Check the template file has been updated or not through VSTS REST API (e.g. Get a single commit with changed items)
Check the related resource is existing in Azure through Azure PowerShell task (e.g. Find-AzureRmResource, Get-AzureRmResource)
Add a new variable per to the results of step1 and step3 through Logging Command (e.g. isUpdated: Write-Host "##vso[task.setvariable variable= isUpdated;]true")
Add Azure Resource Group Deployment task and choose custom conditions in Run this taks box (Custom condition: eq(variables[isUpdated '], 'true')
Add corresponding tasks to deploy your web app.

Is there a way to deploy a VSTS project in an app service using an ARM template?

I'm creating resources on Azure using an ARM template. I have three web apps to deploy. Normally i can deploy them separately using VSTS which is slow. So is there a way to deploy a project on VSTS using an ARM template?
You can deploy a template that uses web deploy to also deploy a web app - you can deploy to slots or deploy to an existing web app (not defined in your ARM template). This sample has an MSDeploy extension that does the work.
https://github.com/Azure/azure-quickstart-templates/tree/master/201-web-app-blob-connection
You can also go this route:
https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started/
Both can be done via VSTS...
You would need to export ARM template from your azure RM portal.
Make sure
you have single resource group where all these apps have been deployed (since you want to deploy them together).
Export the resource management template and attach to your VSTS project as resource.
Add a new release task in your VSTS as Azure Resource Group Deployment.
Add connection details to your resource group.
Specify the previously exported template
Configure the parameters (or use variables).
you should be able to use VSTS build and release management to deploy ARM resources.

Resources