Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimised infrastructure deployment, fixed AppInsights telemetry #3

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

git clone https://github.com/Azure-Samples/nodejs-appsvc-cosmosdb-bottleneck.git

3. Deploy the sample app using the PowerShell script. (Tip: macOS users can install PowerShell [here](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-macos?view=powershell-7.1))
3. Open .\deploymentscript.ps1 and inspect the default resource group and deployment name. This resource group and associated resources will be created in you Azure subscription in the next step.

4. Deploy the sample app using the PowerShell script. (Tip: macOS users can install PowerShell [here](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-macos?view=powershell-7.1))

cd SampleApp
.\deploymentscript.ps1

4. You will be prompted to supply a unique application name and a location (default is `eastus`). A resource group for the resources would be created with the same name.
5. Once deployment is complete, browse to the running sample application with your browser.
5. Once deployment is complete, it will output the domain name of your web app as well as opening a browser and navigating to the app.

https://<app_name>.azurewebsites.net
## **Clean up resources**

You may want to delete the resources to avoid to continue incurring charges. Use the `az group delete` command to remove the resource group and all related resources.
Expand Down
72 changes: 72 additions & 0 deletions cosmos2021.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file uses the newer API's for Cosmos, however the node app doesn't cope well
// Leaving the file here for reference, but know that it's not used by main.bicep

@description('Name of the CosmosDb Account')
param databaseAccountId string

param databaseAccountLocation string =resourceGroup().location

param databaseName string = 'sampledatabase'
param collectionName string = 'samplecollection'

resource cosmosDbAccount 'Microsoft.DocumentDB/databaseAccounts@2021-06-15' = {
kind: 'MongoDB'
name: databaseAccountId
location: databaseAccountLocation
properties: {
databaseAccountOfferType: 'Standard'
locations: [
{
locationName: '${resourceGroup().location}'
failoverPriority:0
isZoneRedundant: false
}
]
}
}
output databaseAccountId string = cosmosDbAccount.id

resource cosmosDbThroughput 'Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections/throughputSettings@2021-06-15' = {
parent: cosmosDbCollection
name: 'default'
properties: {
resource: {
throughput: 400
}
}
}

resource cosmosDbDatabase 'Microsoft.DocumentDB/databaseAccounts/mongodbDatabases@2021-06-15' = {
parent: cosmosDbAccount
name: databaseName
properties: {
resource: {
id: databaseName
}
}
}

resource cosmosDbCollection 'Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections@2021-06-15' = {
parent: cosmosDbDatabase
name: collectionName
properties: {
resource: {
id: collectionName
shardKey: {
user_id: 'Hash'
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
]
}
}
}

output accountId string = cosmosDbDatabase.id
output connstr string = first(listConnectionStrings('Microsoft.DocumentDb/databaseAccounts/${databaseAccountId}', '2015-04-08').connectionStrings).connectionString
54 changes: 54 additions & 0 deletions cosmosRustic.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
param databaseAccountId string
param databaseAccountLocation string =resourceGroup().location

resource databaseAccountId_sampledatabase 'Microsoft.DocumentDB/databaseAccounts/mongodbDatabases@2020-06-01-preview' = {
parent: databaseAccountId_resource
name: 'sampledatabase'
properties: {
resource: {
id: 'sampledatabase'
}
options: {}
}
}

resource databaseAccountId_sampledatabase_samplecollection 'Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections@2020-06-01-preview' = {
parent: databaseAccountId_sampledatabase
name: 'samplecollection'
properties: {
resource: {
id: 'samplecollection'
indexes: []
}
options: {}
}
}

resource databaseAccountId_sampledatabase_samplecollection_default 'Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections/throughputSettings@2020-06-01-preview' = {
parent: databaseAccountId_sampledatabase_samplecollection
name: 'default'
properties: {
resource: {
throughput: 400
}
}
}

resource databaseAccountId_resource 'Microsoft.DocumentDb/databaseAccounts@2015-04-08' = {
kind: 'MongoDB'
name: databaseAccountId
location: databaseAccountLocation
properties: {
databaseAccountOfferType: 'Standard'
locations: [
{
locationName: '${resourceGroup().location}'
failoverPriority:0
isZoneRedundant: false
}
]
}
}

output accountId string = databaseAccountId_resource.id
output connstr string = first(listConnectionStrings('Microsoft.DocumentDb/databaseAccounts/${databaseAccountId}', '2015-04-08').connectionStrings).connectionString
99 changes: 14 additions & 85 deletions deploymentscript.ps1
Original file line number Diff line number Diff line change
@@ -1,92 +1,21 @@
function Get-UrlStatusCode([string] $Url)
{
try
{
[System.Net.WebRequest]::Create($Url).GetResponse()
}
catch [Net.WebException]
{
[int]$_.Exception.Response.StatusCode
}
}
$resourceGroup = "rg-bottleneck"
$deploymentName = "bottleneck"
$location = "eastus"

$ErrorActionPreference = "Stop"
[Console]::ResetColor()
# az login --use-device-code
$output = az account show | ConvertFrom-Json
$subscriptionList = az account list | ConvertFrom-Json
$subscriptionList | Format-Table name, id, tenantId -AutoSize
$selectedSubscription = $output.name
Write-Host "Currently logged in to subscription """$output.name.Trim()""" in tenant " $output.tenantId
$selectedSubscription = Read-Host "Enter subscription Id ("$output.id")"
$selectedSubscription = $selectedSubscription.Trim()
if([string]::IsNullOrWhiteSpace($selectedSubscription)) {
$selectedSubscription = $output.id
} else {
# az account set --subscription $selectedSubscription
Write-Host "Changed to subscription ("$selectedSubscription")"
}

while($true) {
$deploymentName = Read-Host -Prompt "Enter webapp name"
$deploymentName = $deploymentName.Trim()
if($deploymentName.ToLower() -match "xbox") {
Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft"
continue
} elseif ($deploymentName.ToLower() -match "windows") {
Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft"
continue
} elseif ($deploymentName.ToLower() -match "login") {
Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft"
continue
} elseif ($deploymentName.ToLower() -match "microsoft") {
Write-Host "Webapp name cannot have keywords xbox,windows,login,microsoft"
continue
}
# Create the request
$HTTP_Status = Get-UrlStatusCode('http://' + $deploymentName + '.azurewebsites.net')
if($HTTP_Status -eq 0) {
break
} else {
Write-Host "Webapp name taken"
}
}
Write-Output "Creating resource group " $resourceGroup
az group create --location $location --name $resourceGroup

$location = Read-Host -Prompt "Enter location (eastus)"
$location = $location.Trim()
if([string]::IsNullOrWhiteSpace($location)) {
$location = "eastus"
}

$resourceGroup = $deploymentName + "-rg"
Write-Host "Creating resource group " $resourceGroup
az group create --location $location --name $resourceGroup --subscription $selectedSubscription
$databaseName = $deploymentName + "db"

Write-Host "Deploying Sample application.. (this might take a few minutes)"
$deploymentOutputs = az deployment group create --resource-group $resourceGroup --subscription $selectedSubscription --mode Incremental --template-file ./windows-webapp-template.json --parameters "webAppName=$deploymentName" --parameters "hostingPlanName=$deploymentName-host" --parameters "appInsightsLocation=$location" --parameters "databaseAccountId=$databaseName" --parameters "databaseAccountLocation=$location"
Write-Output "Deploying Sample application.. (this might take a few minutes)"
$deploymentOutputs = az deployment group create -g $resourceGroup -f ./main.bicep --parameters "appName=$deploymentName"
$deploymentOutputs = $deploymentOutputs | ConvertFrom-Json
$connectionString = $deploymentOutputs.properties.outputs.azureCosmosDBAccountKeys.value.connectionStrings[0].connectionString

Write-Host "Setting connection string to cosmos db"
$setConnectionString = az webapp config appsettings set --name $deploymentName --resource-group $resourceGroup --subscription $selectedSubscription --settings CONNECTION_STRING="$connectionString"

Write-Host "Setting app setting for App Service"
$setAppSettings = az webapp config appsettings set --name $deploymentName --resource-group $resourceGroup --subscription $selectedSubscription --settings MSDEPLOY_RENAME_LOCKED_FILES=1
Write-Output $deploymentOutputs

$publishConfig = az webapp deployment list-publishing-credentials --name $deploymentName --resource-group $resourceGroup --subscription $selectedSubscription | ConvertFrom-Json
$appName=$deploymentOutputs.properties.outputs.appName.value
$appUrl=$deploymentOutputs.properties.outputs.appUrl.value

Write-Host "Publishing sample app.. (this might take a minute or two)"
git init
git config user.email "[email protected]"
git config user.name "Example man"
git add -A
git commit -m "Initial commit"
$a = git remote add azwebapp $publishConfig.scmUri
git remote rm azwebapp
git remote add azwebapp $publishConfig.scmUri
git push azwebapp main:master
Write-Output "Deployment of $appName Complete"
Write-Output "Domain: $appUrl"
Start-Process("https://$appUrl")

Write-Host "Deployment Complete"
Write-Host "Open url https://$deploymentName.azurewebsites.net in the browser"
Write-Host "To delete the app, run command 'az group delete --name $resourceGroup'"
Write-Output "To delete the app, run command 'az group delete --name $resourceGroup'"
Loading