AzureWebJobsScriptRoot variable is not defined on Azure Functions, but returns correct value locally - azure

AzureWebJobsScriptRoot variable is not defined on Azure Functions. The code below returns no value.
System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process)["AzureWebJobsScriptRoot"];
However, %HOME%\site\wwwroot will be returned based on below:
AzureWebJobsScriptRoot
AzureWebJobsScriptRoot
The path to the root directory where the host.json file and function folders are located. In a function app, the default is %HOME%\site\wwwroot.
Key Sample value
AzureWebJobsScriptRoot %HOME%\site\wwwroot
It returns correct value locally, not %HOME%\site\wwwroot
Update
Is this a bug with Azure Functions?
If so, what is an alternative solution?
Before the issue is fixed by Microsoft, can this variable, AzureWebJobsScriptRoot, be defined myself to "%HOME%\site\wwwroot" on Azure?
https://github.com/Azure/Azure-Functions/issues/1146
https://github.com/MicrosoftDocs/azure-docs/issues/26761

I test in my site and get the same problem with you. As the article said, when running in Azure, will default to %HOME%\site\wwwroot and it set the function folder under home\site\wwwroot.
However, when I set AzureWebJobsScriptRoot in Azure funtion Application Settings, it will show in the output like below:

If you configure a function app with a different value for AzureWebJobsScriptRoot, then the functions host should honor that new value. For example, if you set AzureWebJobsScriptRoot = D:\home\site\wwwroot\foo then the functions host would look for a host.json file and function directories in the D:\home\site\wwwroot\foo location.
By default, this environment variable is not set. So it is expected that if you did not set it yourself, then System.Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot") will return null.
Be aware that if you modify this setting, other components like the portal, visual studio, visual studio code, etc will not be aware of setting and will deploy your code to the normal default location. If you want to customize this setting, its up to you to make sure the application code is deployed to the right location.
Please refer the full details here

Related

Terraform Inconsistent Azure VM Names

I have the following terraform code that creates multiple instances of a Azure VM based on a variable called nb_instance
module "create-servers" {
  source              = "../../Modules/Create-Vms"
  resource_group_name = azurerm_resource_group.rg.name
  vm_hostname         = "testvm"
  nb_instances = 2
On my create-servers module, I have the following code
resource "azurerm_virtual_machine" "vm-windows" {
  count                         = var.nb_instances 
  name                          = "${var.vm_hostname}${format("%02d",count.index+1)}"
  resource_group_name           = data.azurerm_resource_group.vm.name
Which works great, however I'm getting inconsitent naming convention between the Azure resource and the actual server name of the VM.
Azure displays the server as testvm01 and testvm02 however the server's actual OS name is testvm-1 and testvm-2 . My desired name is testvm02 which is what I expected within the module. Is there a workaround for this to keep both names consistent?
You also need to set the property os_profile.computer_name
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine#os_profile

Substitute Service Fabric application parameters during deployment

I'm setting up my production environment and would like to secure my environment-related variables.
For the moment, every environment has its own application parameters file, which works well, but I don't want every dev in my team knowing the production connection strings and other sensitive stuffs that could appear in there.
So I'm looking for every possibility available.
I've seen that in Azure DevOps, which I'm using at the moment for my CI/CD, there is some possible variable substitution (xml transformation). Is it usable in a SF project?
I've seen in another project something similar through Octopus.
Are there any other tools that would help me manage my variables by environment safely (and easily)?
Can I do that with my KeyVault eventually?
Any recommendations?
Thanks
EDIT: an example of how I'd like to manage those values; this is a screenshot from octopus :
so something similar to this that separates and injects the values is what I'm looking for.
You can do XML transformation to the ApplicationParameter file to update the values in there before you deploy it.
The other option is use Powershell to update the application and pass the parameters as argument to the script.
The Start-ServiceFabricApplicationUpgrade command accept as parameter a hashtable with the parameters, technically, the builtin task in VSTS\DevOps transform the application parameters in a hashtable, the script would be something like this:
#Get the existing parameters
$app = Get-ServiceFabricApplication -ApplicationName "fabric:/AzureFilesVolumePlugin"
#Create a temp hashtable and populate with existing values
$parameters = #{ }
$app.ApplicationParameters | ForEach-Object { $parameters.Add($_.Name, $_.Value) }
#Replace the desired parameters
$parameters["test"] = "123test" #Here you would replace with your variable, like $env:username
#Upgrade the application
Start-ServiceFabricApplicationUpgrade -ApplicationName "fabric:/AzureFilesVolumePlugin" -ApplicationParameter $parameters -ApplicationTypeVersion "6.4.617.9590" -UnmonitoredAuto
Keep in mind that the existing VSTS Task also has other operations, like copy the package to SF and register the application version in the image store, you will need to replicate it. You can copy the full script from Deploy-FabricApplication.ps1 file in the service fabric project and replace it with your changes. The other approach is get the source for the VSTS Task here and add your changes.
If you are planning to use KeyVault, I would recommend the application access the values direct on KeyVault instead of passing it to SF, this way, you can change the values in KeyVault without redeploying the application. In the deployment, you would only pass the KeyVault credentials\configuration.

Get app service name in azure programmatically

Simple question, hopefully a simple answer :)
How to I get the app service name, displayed as "test-webapp" in the picture below, from code (C#)? (any other identifier of a specific app service also works).
I have multiple app services, running same code. So I want to be able to express
if(appServiceName == "test-webapp")
{
//take a specific value from web.config and run with it
}
You can get it from the WEBSITE_SITE_NAME environment variable.
To get the variable, use something like:
string siteName = System.Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")
You can see a list of environment variables available here: https://github.com/projectkudu/kudu/wiki/Azure-runtime-environment.

Azure function published but not running, "no data available"

I can publish a Azure function from Visual Studio without an error.
This funtion is set to run every 4 seconds ("*/4 * * * * *") but it is not running at all. Even if I try to run it manually it do not run and show the following error:
Status: 404 Not FoundThe resource you are looking for has been
removed, had its name changed, or is temporarily unavailable.
Under monitoring it do not shows data, under success or error count it says no data available :(
Nothing is working please help
This is a pretty old thread but in case anyone is facing the same issue after migrating their Function App to .NET Core 3.1, check that you have also updated the Function Runtime Version to 3. Update the Function App SDK and in Azure portal check that the function runtime settings is 3. Without updating this setting the same 404 error appears whenever you try to call your function app.
For changing the Function Runtime Version open the Function App in Azure Portal then go to Configuration -> Function runtime settings. From the Runtime version dropdown choose ~3.
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
According to your 404 error message, it means your function source couldn’t be found. Such as wrong resource path , function name has been changed, wrong function name or the function has been deleted.You could check whether your class name and FunctionName attribute name are consistant. If you have changed code, remeber to rebuild the project.
And please make sure you could run the Azure function successfully in Visual studio before published to Azure. In debug mode, check whether output logs are correct.
Under monitoring it do not shows data, under success or error count it says no data available
This info usually means function has never been triggered before. If you create a new function in Azure and click Monitor directly, you could also see this info. To solve this problem, unless you could trigger this Azure function successfully.
In my case I was deploying the azure function using the Azure Resource Manager (ARM) template. I created it manually and was missing some of the properties for the storage account:
For anyone deploying an Azure Function using an ARM template, I would highly recommend taking a template from the GitHub quickstart ARM templates: https://github.com/Azure/azure-quickstart-templates
It provides the minimum template to get your function (and other resource) up and running.
The issue with your function was that GetFTPData.cs is not a valid function name. VS build doesn't validate the function name and the portal isn't displaying these errors.
This issue is tracking the portal error display https://github.com/Azure/azure-functions-ux/issues/2316
and this is for VS build to validate functionName attribute https://github.com/Azure/azure-functions-vs-build-sdk/issues/174

MSdeploy deploys an MVC 2 application with wrong virtual directory name

I'm using MSbuild(v4.0.30319.1) and MSdeploy(v7.1.618.0) to deploy my ASP MVC 2 application on IIS(v7.5).
Here are the commands I run to do it:
msbuild.exe <path to my csproj>/MyMvcApp.csproj /t:Package /p:configuration=release;outDir=<my output dir>
and msdeploy:
msdeploy.exe -verb:sync -source:package='<MSBuildOutputDir>\_PublishedWebsites\Webui_Package\MyMVCApp.zip' -dest:auto
After build and deploy the application is deployed by address http://localhost/MyMVCApp_deploy and not by address http://localhost/MyMVCApp.
I did not expect that the _deploy will be in the address.
How can I fix this?
As Portalus commented you can control the name of the app in the properties page. I'll expand a bit on that answer here.
Configure the default value on PP/Web tab
By default when you package/publish your web project we will create an Web Deploy parameter named IIS Web Application Name which controls this value. The default value for this is
ProjectName_deploy. The reason why we put the _deploy suffix there is for IIS scenarios. So you may already have an IIS app with the name of ProjectName but its much less likely that you will have one named ProjectName_deploy. You can customize this value on the Package/Publish Web tab of the project properties. One thing to keep in mind if you go this route is that all of these settings are tied to a specif build configuration. So if you configure the settings on Debug and the create your package using Release those settings will not apply. See image below.
When you set this value it sets the MSBuild property, DeployIisAppPath, and you can use that if you want to have some logic relating to the value it gets.
Pass the parameter value on publish
If you want you can also just specify the value of this parameter when you are publishing. You have two primary approaches here.
Specify the value for the individual property
Specify the value for this and other properties in a file
1. Specify the value for the individual property:
You can use the -setParam parameter when calling msdeploy.exe to give a new value for that parameter. For example:
%msdeploy% -verb:sync -source:package=WebApplication3.zip -dest:auto -setParam:name="IIS Web Application Name",value="Default Web Site/FooBar"
2. Specify the value for this and other properties in a file
When you create a package in VS we automatically create for you a file named {ProjectName}.SetParameters.xml. This file is a simple XML file and it will contain all the parameters, along with their default values. You can update that file to include the correct parameter values and then pass it into msdeploy.exe (note: the file doesn't have to be named ...SetParameters.xml you can rename it to whatever you want). If you want to use this approach then just use the -setParamFile parameter when calling msdeploy.exe. Here is an example of the command line syntax for this:
%msdeploy% -verb:sync -source:package=WebApplication3.zip -dest:auto -setParamFile=WebApplication3.SetParameters.xml
Change the application name in the settings.
Right click the web project hit properties. Go to package/publish web, change the application name to use on the destiantion server from "default web site/mymvcapp_deploy" to "default website/mymvcapp"

Resources