I'd like to use code like this in my Azure Function:
var key = ConfigurationManager.AppSettings["SubscriptionKey"];
Given that there's no UI for Azure Functions in the Azure Government portal yet, is there a way to add this via the Azure CLI or is there another way to make this happen?
Update
I figured it out:
az functionapp config appsettings set --name appname --settings SubscriptionKey=1234 --resource-group resourcegroupname
As you've already found out, you can do this via Azure CLI with the following command:
az functionapp config appsettings set --name appname --settings SubscriptionKey=1234 --resource-group resourcegroupname
Related
Recently my function apps have stopped swapping slots, they worked fine for months but suddenly they have all stopped swapping by taking a long time then crashing. I have recreated the function apps using the same scripts and have replicated the issue but still can't fix.
Is there anything I am setting to stop the slots from switching?
Here is the fundamental build script I am using. I am able to run this but when trying to swap slots in Azure it freezes then crashes with a vague error message:
# Azure Login
az login
# Set deployment Environment
$environment = "stage"
# Resource Group Variables
$subscriptionId = ""
$domain = "test4"
$resourceGroup = "xx-platform-$domain-$environment"
$region = "northEurope"
# Functions app variables
$storageName = "xxfn$domain$environment" # must be less than 24 chars and all lower case
$functionAppDeploymentSlotName = "test"
$functionAppName = "xx-platform-$domain-fn-$environment"
$functionAppEnvironment = "AZURE_FUNCTIONS_ENVIRONMENT=Development"
$websiteRunFromPackage = "WEBSITE_RUN_FROM_PACKAGE=1"
#########################################################################################################################################################################
## Resource Group
Write-output "Creating resource group";
# Set subscription
az account set --subscription $subscriptionId
# Create resource group
az group create -l $region -n $resourceGroup
#########################################################################################################################################################################
## Functions App
Write-output "Creating Functions App";
# Create storage account
az storage account create --name $storageName --location $region --resource-group $resourceGroup --sku Standard_LRS
# Create functions app - using consumption plan
az functionapp create --name $functionAppName --storage-account $storageName --consumption-plan-location northEurope --resource-group $resourceGroup --functions-version 4
# Set functions app configuration settings
# Environment
az functionapp config appsettings set --name $functionAppName --resource-group $resourceGroup --settings $functionAppEnvironment
# WEBSITE_RUN_FROM_PACKAGE
az functionapp config appsettings set --name $functionAppName --resource-group $resourceGroup --settings $websiteRunFromPackage
# SET DOT NET FRAMEWORK ERSION
az functionapp config set --net-framework-version v6.0 -g $resourceGroup -n $functionAppName
# Create functions app deployment slot
az functionapp deployment slot create --name $functionAppName --resource-group $resourceGroup --slot $functionAppDeploymentSlotName
I have modified your script in initializing the variable values directly to the cmdlets:
# Azure Login
az login
az account set --subscription "<Ur_Azure_Subscription_Id>"
# Function App Create
az functionapp create --name dt-platform-fn-stage --storage-account dtfnstageenvironment --consumption-plan-location northEurope --resource-group HariTestRG --runtime dotnet --functions-version 4
# Environment
az functionapp config appsettings set --name dt-platform-fn-stage --resource-group HariTestRG --settings AZURE_FUNCTIONS_ENVIRONMENT=Development
# WEBSITE_RUN_FROM_PACKAGE
az functionapp config appsettings set --name dt-platform-fn-stage --resource-group HariTestRG --settings WEBSITE_RUN_FROM_PACKAGE=1
# SET DOT NET FRAMEWORK ERSION
az functionapp config set --net-framework-version v6.0 -g HariTestRG -n dt-platform-fn-stage
# Create functions app deployment slot
az functionapp deployment slot create --name dt-platform-fn-stage --resource-group HariTestRG --slot test
Created the Http Trigger Function using the cmdlets given in the below given MS Document.
For Continuous deployments, this app setting needs to be configured:
az functionapp config appsettings set --name dt-platform-fn-stage --resource-group HariTestRG --settings "SCM_DO_BUILD_DURING_DEPLOYMENT=true"
Deployed the Function to the Azure Functions using az cli cmdlet given in one of my workarounds.
By default, Auto Swap on Slots will be disabled:
It is working fine during the slot swapping and make sure you have followed the considerations mentioned in this MS Doc that also provides the list of az cli cmdlets for managing the slots like creating, listing, deleting, swapping and auto swapping configuration.
Above Azure CLI Commands have been taken from this MS Doc references.
Our goal is publishing our offer to Azure MarketPlace. We created an ARM template for our infrastructure and had no issues with that.
Our main issue is how to deploy our code and DB schema through the purchase process.
Microsoft recommends Deployment Scripts so we made ours which contains, CLI commands to restore a .BACPAC sql file, app settings configurations and a CURL command for zip deployment via KUDU for the web application.
.BACPAC and zip package for code are in blob storage.
If we run our scripts in a custom ARM template deployment in Azure, they work fine !!
But when we run them through the marketplace purchase, they fail with no error logs.
Down below you can find our script commands.
"scriptContent": "
az sql db import -s $sqlserver -n $sqldatabase -g $rg -p $sqlpassword -u $sqlusername --storage-key $sqlstoragekey --storage-key-type $storagekeytype --storage-uri $storageuri \r\n
az webapp deployment user set --user-name $deploymentUser --password $deploymentPass \r\n
az webapp config connection-string set --name $applicationName --resource-group $rg --settings DefaultConnection=\"Server=tcp:$sqlserver.database.windows.net,1433;Initial Catalog=$sqldatabase;Persist Security Info=False;User ID=$sqlusername;Password=$sqlpassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;\" --connection-string-type SQLAzure \r\n
az webapp config appsettings set --name $applicationName --resource-group $rg --settings PortalUrl=\https://$applicationName.azurewebsites.net/\ \r\n
az webapp config appsettings set --name $applicationName --resource-group $rg --settings AzureAd:Domain=\"$AADDomain\" \r\n
az webapp config appsettings set --name $applicationName --resource-group $rg --settings AzureAd:ClientId=\"$AADClientId\" \r\n
az webapp config appsettings set --name $applicationName --resource-group $rg --settings AzureAd:TenantId=\"$AADTenantId\" \r\n
az webapp config appsettings set --name $applicationName --resource-group $rg --settings AzureAd:ClientSecret=\"$AADClientSecret\" \r\n
az webapp restart --name $applicationName --resource-group $rg \r\n
curl -X PUT -u $deploymentUser:$deploymentPass https://$applicationName.scm.azurewebsites.net/api/zipdeploy -H \"Content-Type: application/json\" -d \"{'packageUri':'$applicationUri$applicationStorageKey'}\" \r\n
",
In case our approach is wrong, any example, documentation and suggestions will be super helpful.
Feel free to ask for more information if any.
Thank you
I have deployed a "Container Image" on to Web App Service with the following command.
az webapp create --name Dev-App --plan Dev-AppServicePlan \
--resource-group Dev \
--deployment-container-image-name ...devcontainerregistry.azurecr.io/app-node:latest
After the app was successfully deploment I need to change some Environment variables via the following commands.
az webapp config appsettings set --name Dev-App --resource-group Dev \
--settings MONGO_DSN='mongodb://cosmosdb...'
az webapp config appsettings set --name Dev-App --resource-group Dev \
--settings REDIS_URI='...dev.redis.cache.windows.net:6380...'
az webapp log config --name Dev-App --resource-group Dev \
--docker-container-logging filesystem
Now I have updated and restarted the Web App but it looks like the Image is not redeployed or at least the Environment variables are not there.
az webapp update --name Dev-App --resource-group Dev
az webapp restart --name Dev-App --resource-group Dev
I looked with log tail to the logs but the logs are also not changed. What I mean with "not changed" is that the log output is stucked to the Timestamp "2021-01-13T13:21:31.100Z" even when I restart the Web App.
az webapp log tail --name Dev-App --resource-group Dev
In this Articles from MS is described that a restart should be enough but then should also the log output should be changed.
2: Redeploy a container to Azure App Service after making changes
Configure a custom container for Azure App Service
Any hint how to activate the new settings in the custom image setup or trigger a redeployment?
FYI: I have also created a question in the azure community.
https://learn.microsoft.com/en-us/answers/questions/230584/how-to-activate-settings-in-the-web-app-when-deplo.html
It seems you want to updates the environment variables just like add them in the command:
docker run --name container_name image -e env_var1 -e env_var2
The Azure CLI command just sets the environment variables for the Web App service, you can see them in the Kudu, but it's not the same result as the docker run command.
I recommend you use the docker-compose to deploy your image to the Web App service. Here is the example for the docker-compose. You can update the environment variables in the environment option of the docker-compose file in the portal. Then it will restart again and refresh the logs. And the variables you added will also take effect.
Well the solution for my problem was that I change the registry from "Private Registry" to "Azure Container Registry" in the "container settings" page after running the following command
az webapp create --name Dev-App \
--plan Dev-AppServicePlan \
--resource-group chat-Dev \
--deployment-container-image-name devcontainerregistry.azurecr.io/chat-node:latest
I have put the acr data into the fields and activated the "Continous Deployment"
After this change every settings change triggers a restart and the ENV Variables was in the new instance.
Is there a way we can swap only app settings/connectionstrings but not code from Azure App Service slot using azure DevOps pipeline or Azure CLI?
We are trying to get connectionstring directly from the slot and swap it to prod but not code.
As far as I know, you can use Azure CLI to achieve it.
Appsettings: az webapp config appsettings
ConnectString: az webapp config connection-string
You could use Azure CLI to list appsettings/connectionstring. Then you could use the set command to pass the values to the slot.
Here is my sample(In Azure Devops Azure CLI Task):
$appsettings=az webapp config appsettings list --name webappname -g ResourcesGroupName --slot slotname
$appsettings| Out-File "$(System.DefaultWorkingDirectory)\appsettings.json"
az webapp config appsettings set -g ResourcesGroupName -n webappname --settings -slot slotname "#$(System.DefaultWorkingDirectory)\appsettings.json"
$connectstring=az webapp config connection-string list --name webappname -g ResourcesGroupName --slot slotname
$test = $connectstring | ConvertFrom-Json
$test | Select-Object -Property Name,slotSetting ,type,Value | ForEach-Object {
Write-Host $_.Name $_.Value
$msg = $_.Name , $_.Value -join '='
echo $msg
az webapp config connection-string set --name webappname -g ResourcesGroupName -slot slotname -t mysql --settings $msg
}
Note: Here is an known issue about the az webapp config connection-string set. It couldn't support to set files in settings parameter for the time being.
You can try this,
Step 1 : Create two separated environments in a release as per your needs!
Step 2 : Create a variable with the same name as your connection string name in your web.config, one scoped for staging environment and the other for production.
Step 3 : Add a Deploy Azure App Service task to each environment.
Then you could use the File transformation and substitution options and mark to make the transformation. Take a look at XML Transformation and JSON transformation
I have written an Azure cli task in Azure Devops as below
$vnetid = $(az network vnet show -n $vnet -g $(rg) --query id --output tsv)
I want to output the $vnetid to the Azure Devops Variables so it can be used in another task how can i do this?
I guess you can simply do
Write-Output("##vso[task.setvariable variable=vnetid;]$$vnetid")