Colin Laws
Colin Laws

Reputation: 680

How do you access environment variables in an Azure function app written in powershell?

I am writing an Azure function app in powershell (runtime 2.0.12507.0).

To perform an operation in my function app, it must authenticate with Azure using the Connect-AzAccount function. I store the sensitive credentials used to connect in 'Manage > Function keys', but I cannot access these keys programmatically.

Here I have the function keys set under the manage section of my function.

I have already tried using $Env:NAME_OF_MY_KEY to access the key value, but the value is coming up as null or empty when I try this. From what I understand, 'Function keys' are simply environment variables, and should be accessible this way.

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Get the service principal secret as a secure string
$secpasswd = ConvertTo-SecureString $env:SP_Secret -AsPlainText -Force 

# Create a new PSCredential using the principal secret and app id
$mycreds = New-Object System.Management.Automation.PSCredential ($env:SP_AppID, $secpasswd) 

# Connect to the Azure account using the powershell credentials and tenant id
$result = Connect-AzAccount -ServicePrincipal -Tenant $env:SP_Tenant -Credential $mycreds 

I am expecting to be able to access these function keys; however, even if I echo out the value of $env:SP_Secret, I still get an empty string. Is this the correct way to access function keys in an Azure Function App?

Upvotes: 3

Views: 7211

Answers (2)

HariHaran
HariHaran

Reputation: 4099

With the help of Key Management API you can GET PUT POST keys using http requests. You don't have to re-add the keys as environment variables.

You can find the KEY MANAGEMENT API Docs here on Github

Upvotes: 0

Colin Laws
Colin Laws

Reputation: 680

Function keys are not exposed as environment variables. To set environment variables, you must set them in application settings.

Here, you can see the 'Manage application settings' link when you navigate to your function app.

Go to your application settings

Then, you may add a key here.

Add environment variables in your application settings.

Upvotes: 2

Related Questions