From ec36169cab64ef187b4ebe5ec348206b65a7a1f5 Mon Sep 17 00:00:00 2001 From: Daniel Larsen Date: Fri, 19 Mar 2021 13:55:24 +1300 Subject: [PATCH] #7 multi-region support --- deploy/_vars.ps1 | 8 +++++++ deploy/deploy-azure.ps1 | 43 ++++++++------------------------------ deploy/deploy-function.ps1 | 29 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 deploy/deploy-function.ps1 diff --git a/deploy/_vars.ps1 b/deploy/_vars.ps1 index e69de29..b72f949 100644 --- a/deploy/_vars.ps1 +++ b/deploy/_vars.ps1 @@ -0,0 +1,8 @@ +$location = 'australiaeast' +$loc = 'aue' +$rg = 'numberservice-rg' +$tags = 'project=NumberService', 'repo=DanielLarsenNZ/NumberService' +$insights = 'numberservice-insights' +$cosmos = 'numberservice' +$cosmosDB = 'NumberService' +$container='Numbers' diff --git a/deploy/deploy-azure.ps1 b/deploy/deploy-azure.ps1 index d3fc910..d253430 100644 --- a/deploy/deploy-azure.ps1 +++ b/deploy/deploy-azure.ps1 @@ -1,21 +1,11 @@ # Deploy NumberService resources - $ErrorActionPreference = 'Stop' +. ./_vars.ps1 -$location = 'australiaeast' -$loc = 'aue' -$rg = 'numberservice-rg' -$tags = 'project=NumberService', 'repo=DanielLarsenNZ/NumberService' -$cosmos = 'numberservice' -$cosmosDB = 'NumberService' $throughput = 400 -$container='Numbers' $pk = '/id' -$insights = 'numberservice-insights' -$functionApp = "numberservice-$loc" -$repo = 'https://github.com/DanielLarsenNZ/NumberService.git' -$storage = "numservfn$loc" - +$primaryCosmosLocation = 'australiaeast' +$secondaryCosmosLocation = 'australiasoutheast' # RESOURCE GROUP az group create -n $rg --location $location --tags $tags @@ -23,28 +13,13 @@ az group create -n $rg --location $location --tags $tags # COSMOS DB ACCOUNT az cosmosdb create -n $cosmos -g $rg --default-consistency-level Session ` - --locations regionName=$location failoverPriority=0 isZoneRedundant=True + --locations regionName=$primaryCosmosLocation failoverPriority=0 isZoneRedundant=True ` + --locations regionName=$secondaryCosmosLocation failoverPriority=1 isZoneRedundant=False ` + --enable-automatic-failover $true ` + --enable-multiple-write-locations $true az cosmosdb sql database create -a $cosmos -g $rg -n $cosmosDB --throughput $throughput az cosmosdb sql container create -a $cosmos -g $rg -d $cosmosDB -n $container -p $pk -$connString = ( az cosmosdb keys list -n $cosmos -g $rg --type 'connection-strings' | ConvertFrom-Json ).connectionStrings[0].connectionString - - -# STORAGE ACCOUNT -az storage account create -n $storage -g $rg --tags $tags --location $location --sku 'Standard_LRS' - +$env:NUMBERS_COSMOS_CONNSTRING = ( az cosmosdb keys list -n $cosmos -g $rg --type 'connection-strings' | ConvertFrom-Json ).connectionStrings[0].connectionString # APPLICATION INSIGHTS -$instrumentationKey = ( az monitor app-insights component create --app $insights --location $location -g $rg --tags $tags | ConvertFrom-Json ).instrumentationKey - - -# FUNCTION APP -az functionapp create -n $functionApp -g $rg --consumption-plan-location $location --functions-version 3 ` - --app-insights $insights --app-insights-key $instrumentationKey -s $storage -az functionapp config appsettings set -n $functionApp -g $rg --settings ` - "CosmosDbConnectionString=$connString" ` - "CosmosDbDatabaseId=$cosmosDB" ` - "CosmosDbContainerId=$container" -az functionapp deployment source config -n $functionApp -g $rg --repo-url $repo --branch 'main' - -# Tear down -# az group delete -n $rg --yes \ No newline at end of file +$env:NUMBERS_APP_INSIGHTS_IKEY = ( az monitor app-insights component create --app $insights --location $location -g $rg --tags $tags | ConvertFrom-Json ).instrumentationKey diff --git a/deploy/deploy-function.ps1 b/deploy/deploy-function.ps1 new file mode 100644 index 0000000..2cdf047 --- /dev/null +++ b/deploy/deploy-function.ps1 @@ -0,0 +1,29 @@ +[CmdletBinding()] +param ( + [Parameter(Mandatory = $true)] [ValidateSet('australiaeast', 'australiasoutheast')] $FunctionLocation +) + +. ./_vars.ps1 + +$loc = switch ($FunctionLocation) { + 'australiaeast' { 'aue' } + 'australiasoutheast' { 'ase' } + default { throw "$FunctionLocation is not supported" } +} + +$functionApp = "numberservice-$loc" +$repo = 'https://github.com/DanielLarsenNZ/NumberService.git' +$storage = "numservfn$loc" + +# STORAGE ACCOUNT +az storage account create -n $storage -g $rg --tags $tags --location $FunctionLocation --sku 'Standard_LRS' + + +# FUNCTION APP +az functionapp create -n $functionApp -g $rg --consumption-plan-location $FunctionLocation --functions-version 3 ` + --app-insights $insights --app-insights-key $env:NUMBERS_APP_INSIGHTS_IKEY -s $storage +az functionapp config appsettings set -n $functionApp -g $rg --settings ` + "CosmosDbConnectionString=$env:NUMBERS_COSMOS_CONNSTRING" ` + "CosmosDbDatabaseId=$cosmosDB" ` + "CosmosDbContainerId=$container" +az functionapp deployment source config -n $functionApp -g $rg --repo-url $repo --branch 'main'