| title | Use an Azure Resource Manager template to deploy a Linux web app to Azure |
|---|---|
| description | Use an Azure Resource Manager template to deploy a Linux web app to Azure |
| ms.topic | quickstart |
| ms.author | jukullam |
| author | JuliaKM |
| ms.date | 02/22/2022 |
| monikerRange | =azure-devops |
| ms.custom | subject-armqs, devx-track-arm-template, arm2024, linux-related-content |
[!INCLUDE version-eq-azure-devops]
Get started with Azure Resource Manager templates (ARM templates) by deploying a Linux web app with MySQL. ARM templates give you a way to save your configuration in code. Using an ARM template is an example of infrastructure as code and a good DevOps practice.
An ARM template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. The template uses declarative syntax. In declarative syntax, you describe your intended deployment without writing the sequence of programming commands to create the deployment.
You can use either JSON or Bicep syntax to deploy Azure resources. Learn more about the difference between JSON and Bicep for templates.
Before you begin, you need:
- An Azure account with an active subscription. Create an account for free.
- An active Azure DevOps organization. Sign up for Azure Pipelines.
- (For Bicep deployments) An existing resource group. Create a resource group with Azure portal, Azure CLI, or Azure PowerShell.
Fork this repository on GitHub:
https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.web/webapp-linux-managed-mysql
The template used in this quickstart is from Azure Quickstart Templates.
The template defines several resources:
- Microsoft.Web/serverfarms
- Microsoft.Web/sites
- Microsoft.DBforMySQL/servers
- Microsoft.DBforMySQL/servers/firewallrules
- Microsoft.DBforMySQL/servers/databases
-
Sign in to your Azure DevOps organization and navigate to your project. Create a project if you do not already have one.
-
Go to Pipelines, and then select Create Pipeline.
-
Select GitHub as the location of your source code.
[!NOTE] You may be redirected to GitHub to sign in. If so, enter your GitHub credentials.
-
When the list of repositories appears, select
yourname/azure-quickstart-templates/.[!NOTE] You may be redirected to GitHub to install the Azure Pipelines app. If so, select Approve and install.
-
When the Configure tab appears, select
Starter pipeline. -
Replace the content of your pipeline with this code:
trigger: - none pool: vmImage: 'ubuntu-latest'
-
Create three variables:
siteName,administratorLogin, andadminPass.adminPassneeds to be a secret variable.- Select Variables.
- Use the
+sign to add three variables. When you createadminPass, select Keep this value secret. - Click Save when you're done.
Variable Value Secret? siteName mytestsiteNo adminUser fabrikamNo adminPass Fqdn:5362!Yes -
Map the secret variable
$(adminPass)so that it is available in your Azure Resource Group Deployment task. At the top of your YAML file, map$(adminPass)to$(ARM_PASS).variables: ARM_PASS: $(adminPass) trigger: - none pool: vmImage: 'ubuntu-latest'
-
Add the Copy Files task to the YAML file. You will use the
101-webapp-linux-managed-mysqlproject. For more information, see Build a Web app on Linux with Azure database for MySQL repo for more details.variables: ARM_PASS: $(adminPass) trigger: - none pool: vmImage: 'ubuntu-latest' steps: - task: CopyFiles@2 inputs: SourceFolder: 'quickstarts/microsoft.web/webapp-linux-managed-mysql/' Contents: '**' TargetFolder: '$(Build.ArtifactStagingDirectory)'
-
Add and configure the Azure Resource Group Deployment task.
The task references both the artifact you built with the Copy Files task and your pipeline variables. Set these values when configuring your task.
- Deployment scope (deploymentScope): Set the deployment scope to
Resource Group. You can target your deployment to a management group, an Azure subscription, or a resource group. - Azure Resource Manager connection (azureResourceManagerConnection): Select your Azure Resource Manager service connection. To configure new service connection, select the Azure subscription from the list and click Authorize. See Connect to Microsoft Azure for more details
- Subscription (subscriptionId): Select the subscription where the deployment should go.
- Action (action): Set to
Create or update resource groupto create a new resource group or to update an existing one. - Resource group: Set to
ARMPipelinesLAMP-rgto name your new resource group. If this is an existing resource group, it will be updated. - Location(location): Location for deploying the resource group. Set to your closest location (for example, West US). If the resource group already exists in your subscription, this value will be ignored.
- Template location (templateLocation): Set to
Linked artifact. This is location of your template and the parameters files. - Template (csmFile): Set to
$(Build.ArtifactStagingDirectory)/azuredeploy.json. This is the path to the ARM template. - Template parameters (csmParametersFile): Set to
$(Build.ArtifactStagingDirectory)/azuredeploy.parameters.json. This is the path to the parameters file for your ARM template. - Override template parameters (overrideParameters): Set to
-siteName $(siteName) -administratorLogin $(adminUser) -administratorLoginPassword $(ARM_PASS)to use the variables you created earlier. These values will replace the parameters set in your template parameters file. - Deployment mode (deploymentMode): The way resources should be deployed. Set to
Incremental. Incremental keeps resources that are not in the ARM template and is faster thanComplete.Validatemode lets you find problems with the template before deploying.
variables: ARM_PASS: $(adminPass) trigger: - none pool: vmImage: 'ubuntu-latest' steps: - task: CopyFiles@2 inputs: SourceFolder: 'quickstarts/microsoft.web/webapp-linux-managed-mysql/' Contents: '**' TargetFolder: '$(Build.ArtifactStagingDirectory)' - task: AzureResourceManagerTemplateDeployment@3 inputs: deploymentScope: 'Resource Group' azureResourceManagerConnection: '<your-resource-manager-connection>' subscriptionId: '<your-subscription-id>' action: 'Create Or Update Resource Group' resourceGroupName: 'ARMPipelinesLAMP-rg' location: '<your-closest-location>' templateLocation: 'Linked artifact' csmFile: '$(Build.ArtifactStagingDirectory)/azuredeploy.json' csmParametersFile: '$(Build.ArtifactStagingDirectory)/azuredeploy.parameters.json' overrideParameters: '-siteName $(siteName) -administratorLogin $(adminUser) -administratorLoginPassword $(ARM_PASS)' deploymentMode: 'Incremental'
- Deployment scope (deploymentScope): Set the deployment scope to
-
Click Save and run to deploy your template. The pipeline job will be launched and after few minutes, depending on your agent, the job status should indicate
Success.
-
Sign in to your Azure DevOps organization and navigate to your project. Create a project if you do not already have one.
-
Go to Pipelines, and then select Create Pipeline.
-
Select GitHub as the location of your source code.
[!NOTE] You may be redirected to GitHub to sign in. If so, enter your GitHub credentials.
-
When the list of repositories appears, select
yourname/azure-quickstart-templates/.[!NOTE] You may be redirected to GitHub to install the Azure Pipelines app. If so, select Approve and install.
-
When the Configure tab appears, select
Starter pipeline. -
Replace the content of your pipeline with this code:
trigger: - none pool: vmImage: $(vmImageName)
-
Create three variables:
siteName,administratorLogin, andadministratorLoginPassword.administratorLoginPasswordneeds to be a secret variable.- Select Variables.
- Use the
+sign to add three variables. When you createadminPass, select Keep this value secret. - Click Save when you're done.
Variable Value Secret? siteName mytestsiteNo administratorLogin fabrikamNo administratorLoginPassword Fqdn:5362!Yes -
At the top of your YAML file, map values for
locationandresourceGroupName. Your location should be the location of the resource group. Your resource group needs to already exist.variables: vmImageName: 'ubuntu-latest' resourceGroupName: '<resource-group-name>' # Needs to already exist location: '<your-closest-location>' templateFile: './main.bicep' sourceFolder: 'quickstarts/microsoft.web/webapp-linux-managed-mysql/'
-
If you do not already have an Azure Resource Manager service connection, create a service connection. Learn more about connecting to Azure.
- The service connection be in the same resource group as your
resourceGroupName.
:::image type="content" source="media/service-connection-arm.png" alt-text="Add a service connection.":::
- The service connection be in the same resource group as your
-
Add the Azure CLI task to deploy with Bicep. The task uses the az deployment group create Azure CLI command. You'll pass the
administratorLoginandadministratorLoginPasswordparameter values as variables.- Set the Azure Resource Manager connection to your service connection.
- The Script Type is Shell.
- The Script Location is Inline script.
variables: vmImageName: 'ubuntu-latest' resourceGroupName: '<resource-group-name>' # Needs to already exist location: '<your-closest-location>' templateFile: './main.bicep' sourceFolder: 'quickstarts/microsoft.web/webapp-linux-managed-mysql/' trigger: - none pool: vmImage: $(vmImageName) name: Bicep deploy template steps: - task: AzureCLI@2 inputs: azureSubscription: '<service-connection-name>' scriptType: bash scriptLocation: inlineScript inlineScript: | az deployment group create --resource-group $(resourceGroupName) --template-file $(sourceFolder)$(templateFile) \ --parameters administratorLogin=$(administratorLogin) administratorLoginPassword=$(administratorLoginPassword)
-
Verify that the resources deployed. Go to the
ARMPipelinesLAMP-rgresource group in the Azure portal and verify that you see App Service, App Service Plan, and Azure Database for MySQL server resources.:::image type="content" source="media/azure-resources-portal.png" alt-text="ARM template resources in the Azure portal":::
You can also verify the resources using Azure CLI.
az resource list --resource-group ARMPipelinesLAMP-rg --output table -
Go to your new site. If you set
siteNametoarmpipelinetestsite, the site is located athttps://armpipelinetestsite.azurewebsites.net/.
-
Verify that the resources deployed. Go to your resource group in the Azure portal and verify that you see App Service, App Service Plan, and Azure Database for MySQL server resources.
:::image type="content" source="media/azure-resources-portal.png" alt-text="ARM template resources in the Azure portal":::
You can also verify the resources using Azure CLI.
az resource list --resource-group <resource-group-name> --output table -
Go to your new site. If you set
siteNametoarmpipelinetestsite, the site is located athttps://armpipelinetestsite.azurewebsites.net/.
You can also use an ARM template to delete resources. Change the action value in your Azure Resource Group Deployment task to DeleteRG. You can also remove the inputs for templateLocation, csmFile, csmParametersFile, overrideParameters, and deploymentMode.
variables:
ARM_PASS: $(adminPass)
trigger:
- none
pool:
vmImage: 'ubuntu-latest'
steps:
- task: CopyFiles@2
inputs:
SourceFolder: 'quickstarts/microsoft.web/webapp-linux-managed-mysql/'
Contents: '**'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: AzureResourceManagerTemplateDeployment@3
inputs:
deploymentScope: 'Resource Group'
azureResourceManagerConnection: '<your-resource-manager-connection>'
subscriptionId: '<your-subscription-id>'
action: 'DeleteRG'
resourceGroupName: 'ARMPipelinesLAMP-rg'
location: ''<your-closest-location>'If you no longer need your deployed resources, delete your resource group.
az group delete -n <resource-group-name>
[!div class="nextstepaction"] Create your first ARM template