Skip to content

Commit

Permalink
Deploy App Service Plan - MLZ Example (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
ExchMaster committed Nov 24, 2021
1 parent 6b6e740 commit cdd9c3f
Show file tree
Hide file tree
Showing 9 changed files with 629 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ crash.log
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
*plan*
!src/bicep/**
*.plan*

# Mac files
.DS_Store

# Ignore deploymentVariables.json as it is specific to a single instantiation of MLZ
src/bicep/examples/deploymentVariables.json
50 changes: 48 additions & 2 deletions src/bicep/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,57 @@

In this directory are examples of how to add and extend functionality on-top of MissionLZ.

You [must first deploy MissionLZ](../README.md#Deployment), then you can deploy these examples.
You [must first deploy MissionLZ](../README.md#Deployment), then you can deploy these examples. Since most examples re-use outputs from the base deployment of MLZ, we make use of the [shared variable file pattern](https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/patterns-shared-variable-file) to make it easier to share common variables across all of the examples.

## Example Explanations

Example | Description
------- | -----------
[Remote Access](./remoteAccess) | Adds a Bastion Host and a virtual machine to serve as a jumpbox into the network
[New Workload](./newWorkload) | Adds a new Spoke Network and peers it to the Hub Network routing all traffic to the Azure Firewall
[Azure Sentinel](./sentinel) | A Terraform module that adds an Azure Sentinel solution to a Log Analytics Workspace
[Azure Sentinel](./sentinel) | A Terraform module that adds an Azure Sentinel solution to a Log Analytics Workspace. Sentinel can also be deployed via bicep and the base deployment of mlz.bicep by using the boolean param '-deploySentinel'.
[Inherit Tags](./inheritTags) | Adds or replaces a specified tag and value from the parent resource group when any resource is created or updated.
[appServicePlan](./appServicePlan) | Deploys an App Service Plan (AKA: Web Server Cluster) to support simple web accessible linux docker containers with optional dynamic auto scaling.

## Shared Variable File Pattern (deploymentVariables.json)

The shared variable file pattern reduced the repeition of shared values in a library of bicep files. This pattern is utilized for all examples modules though in almost all cases you can over-ride the shared variable value by supplying custom parameter values at run time.

Shown below are two ways by which the shared variable file (deploymentVariables.json) can be generated. The first utilizing PowerShell Core and the second using the Azure CLI. A deployment of mlz.bicep is required, please make note of the name and region of the deployment.

### PowerShell Core

Shown below are step by step instructions for generated the needed deploymentVariables.json file utilizing PowerShell Core and the Auzre PowerShell module. PowerShell and the Azure PowerShell module are open-source and avaliable for all major operating systems.

* [Get PowerShell Core](https://github.com/PowerShell/PowerShell/releases)
* [Get Azure PowerShell](https://docs.microsoft.com/en-us/powershell/azure/install-az-ps)
* [Getting Started with Azure PowerShell](https://docs.microsoft.com/en-us/powershell/azure/get-started-azureps)

Execute the following commands from '.\src\bicep\examples\'

```PowerShell
Connect-AzAccount
(Get-AzSubscriptionDeployment -Name MLZDeploymentName).outputs | ConvertTo-Json | Out-File -FilePath .\deploymentVariables.json
```

Replace "MLZDeploymentName" with your deployment name. If you do not know your deployment name then log into the Azure management portal, browse to 'Subscriptions', select the subscription MLZ was deployed into, and then look at 'Deployments' to obtain the deployment name.

Place the 'deploymentVariables.json' file '.\src\bicep\examples\' folder.

### Azure CLI

Shown below are step by step instructions for generated the needed deploymentVariables.json file utilizing the Azure CLI. The Azure CLI is open-source and avaliable for all major operating systems.

* [Get Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli)
* [Getting started with Azure CLI](https://docs.microsoft.com/en-us/cli/azure/get-started-with-azure-cli)

Execute the following commands from '.\src\bicep\examples\'

```Azure CLI
az login
az deployment sub show -n MLZDeploymentName --query properties.outputs >> ./deploymentVariables.json
```

Replace "MLZDeploymentName" with your deployment name. If you do not know your deployment name then log into the [Azure management portal](https://portal.azure.com), browse to 'Subscriptions', select the subscription MLZ was deployed into, and then look at 'Deployments' to obtain the deployment name.

Place the 'deploymentVariables.json' file '.\src\bicep\examples\' folder. For a specific example of a Bicep template utilizing 'deploymentVariables.json', take a look at [.\appServicePlan\appService.bicep](.\appServicePlan\appService.bicep)
63 changes: 63 additions & 0 deletions src/bicep/examples/appServicePlan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# App Service Plan Example

This example deploys an App Service Plan (AKA: Web Server Cluster) to support simple web accessible linux docker containers. It also optionally supports the use of dynamic (up and down) scale settings based on CPU percentage up to a max of 10 compute instances.

Read on to understand what this example does, and when you're ready, collect all of the pre-requisites, then deploy the example.

## What this example does

### Deploys an Azure App Service Plan

The docs on Azure App Service Plans: <https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans>. This sample shows how to deploy using Bicep and utilizes the shared file variable pattern to support the deployment. By default, this template will deploy resources into standard default MLZ subscriptions and resource groups.

The subscription and resource group can be changed by providing the resource group name (Param: targetResourceGroup) and ensuring that the Azure context is set the proper subscription.

## Pre-requisites

1. A Mission LZ deployment (a deployment of mlz.bicep)
2. The outputs from a deployment of mlz.bicep (./src/bicep/examples/deploymentVariables.json).

See below for information on how to create the appropriate deployment variables file for use with this template.

### Template Parameters

Template Parameters Name | Description
-----------------------| -----------
appServicePlanName | The name of the App Service Plan. If not specified, the name will default to the MLZ default naming pattern.
targetResourceGroup | The name of the resource group where the App Service Plan will be deployed. If not specified, the resource group name will default to the shared services MLZ resource group name and subscription.
enableAutoScale | A true/false value that determines if dynamic auto scale is enabled. If set to "true", dynamic auto scale is enabled up to a maximum of 10 compute instances based on CPU percentage exceeding 70% for 10 minutes. Will also scale down if CPU percentage is below 30% for 10 minutes. If set to "false", the App Service Plan will statically maintain two compute instances indefinitely.

### Generate MLZ Variable File (deploymentVariables.json)

For instructions on generating 'deploymentVariables.json' using both Azure PowerShell and Azure CLI, please see the [README at the root of the examples folder](..\README.md).

Place the resulting 'deploymentVariables.json' file within the ./src/bicep/examples folder.

### Deploying App Service Plan

Connect to the appropriate Azure Environment and set appropriate context, see getting started with Azure PowerShell for help if needed. The commands below assume you are deploying in Azure Commercial and show the entire process from deploying MLZ and then adding an Azure App Service Plan post-deployment.

```PowerShell
cd .\src\bicep
Connect-AzAccount
New-AzSubscriptionDeployment -Name contoso -TemplateFile .\mlz.bicep -resourcePrefix 'contoso' -Location 'eastus'
cd .\examples
(Get-AzSubscriptionDeployment -Name contoso).outputs | ConvertTo-Json | Out-File -FilePath .\deploymentVariables.json
cd .\AppServicePlan
New-AzSubscriptionDeployment -DeploymentName deployAppServicePlan -TemplateFile .\appService.bicep -Location 'eastus'
```

```Azure CLI
az login
cd src/bicep
az deployment sub create -n contoso -f mlz.bicep -l eastus --parameters resourcePrefix=contoso
cd examples
az deployment sub show -n contoso --query properties.outputs > ./deploymentVariables.json
cd appServicePlan
az deployment sub create -n deployAppServicePlan -f appService.bicep -l eastus
```

### References

* <https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans>
* <https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/patterns-shared-variable-file>
52 changes: 52 additions & 0 deletions src/bicep/examples/appServicePlan/appService.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Deployes a web server farm(aka: App Service Plan) to support web container deployments for Linux.
Optionally enable dynamic auto scaling based on CPU Percentages using 'enableAutoScale' true/false
*/
targetScope = 'subscription'

param mlzDeploymentVariables object = json(loadTextContent('../deploymentVariables.json'))

@description('The name of the web server farm which will be created. If unchanged or not specified, the MLZ resource prefix + "--ASP" will be utilized.')
param appServicePlanName string = '${mlzDeploymentVariables.mlzResourcePrefix.Value}-asp'

@description('The name of the resource group in which the app service plan will be deployed. If unchanged or not specified, the MLZ shared services resource group is used.')
param targetResourceGroup string = '${mlzDeploymentVariables.spokes.Value[2].resourceGroupName}'

@description('If true, enables dynamic scale-in & scale-out based on CPU percentages. If false, then compute instances remain static with 2 instances supporting all traffic')
param enableAutoScale bool = true

@description('Defines the performance tier of your web farm. By default the performance scale will be premium 2nd generation version 2 "p2v2". Another value would be standard generation 2 "s2".')
param appServiceSkuName string = 'p2v2'

var targetSubscriptionId_Var = targetResourceGroup == '${mlzDeploymentVariables.spokes.Value[2].resourceGroupName}' ? '${mlzDeploymentVariables.spokes.Value[2].subscriptionId}' : subscription().subscriptionId
var location = deployment().location
var kind = 'linux'
var capacity = 2

resource targetASPResourceGroup 'Microsoft.Resources/resourceGroups@2020-10-01' = {
name: targetResourceGroup
location: location
}

module appServicePlan 'modules/appServicePlan.bicep' = {
name: appServicePlanName
scope: resourceGroup(targetSubscriptionId_Var, targetASPResourceGroup.name)
params: {
location: location
svcPlanName: appServicePlanName
sku: appServiceSkuName
capacity: capacity
kind: kind
}
}
module appServicePlanSettings 'modules/appServiceSettings.bicep' = if (enableAutoScale) {
name: 'appServicePlanSettingsName'
scope: resourceGroup(targetSubscriptionId_Var, targetASPResourceGroup.name)
params: {
location: location
svcPlanName: appServicePlan.outputs.svcPlanName
svcPlanNameID: appServicePlan.outputs.svcPlanID
}
}
output appServicePlanName string = appServicePlanName
output resourceGroupName string = targetResourceGroup
Loading

0 comments on commit cdd9c3f

Please sign in to comment.