Parameterize deployment scripts
In the previous unit, you added some custom behavior to an Azure Resource Manager (ARM) template to stage starting content in a storage account for a new application environment. This solved a specific problem for one application team.
One way to make deployment scripts more adaptable is to provide data to the script. You have two options, command-line arguments and environment variables.
Note
The commands in this unit are shown to illustrate concepts. Don't run the commands yet. You'll practice what you learn here soon.
Using command-line arguments
The first option for passing data into the deploymentScripts
resources is to customize the arguments
property. The arguments
property takes a string of arguments just like the ones you'd supply at the command line. These arguments are supplied to the command
property of the Azure container instance that will run the script.
Note
Some parsing happens, so test some variations of your arguments
property. It'll be broken up into an array of strings the same way that the Windows shell parses command lines.
"properties": {
"arguments": "-Name Learner",
"azPowerShellVersion": "3.0",
"scriptContent": "
param ([string]$Name)
$output = \"Hello $Name!\"
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
",
"retentionInterval":"P1D"
}
properties: {
arguments: '-Name Learner'
azPowerShellVersion: '3.0'
scriptContent: '''
param ([string]$Name)
$output = "Hello $Name!"
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
'''
retentionInterval: 'P1D'
}
Using environment variables
Your second option is to create environment variables that your scripts can access.
"properties": {
"arguments": "-Name Learner",
"environmentVariables:": [
{
"name": "Subject",
"value": "Deployment Scripts"
}
],
"azPowerShellVersion": "3.0",
"scriptContent": "
param ([string]$Name)
$output = \"Hello $Name!\"
$output += \"Learning about $env:Subject can be very helpful in your deployments.\"
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
",
"retentionInterval":"P1D"
}
properties: {
arguments: '-Name Learner'
environmentVariables: [
{
name: 'Subject'
value: 'Deployment Scripts'
}
]
azPowerShellVersion: '3.0'
scriptContent: '''
param ([string]$Name)
$output = "Hello $Name!"
$output += "Learning about $env:Subject can be very helpful in your deployments."
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
'''
retentionInterval: 'P1D'
}
One benefit of using environment variables is that you can use the secureValue
option for secrets that might need to be passed into deployment scripts.
"properties": {
"arguments": "-Name Learner",
"environmentVariables:": [
{
"name": "Subject",
"value": "Deployment Scripts"
},
{
"name": "MySecretValue",
"secureValue": "PleaseDoNotPrintMeToTheConsole!"
}
],
"azPowerShellVersion": "3.0",
"scriptContent": "
param ([string]$Name)
$output = \"Hello $Name!\"
$output += \"Learning about $env:Subject can be very helpful in your deployments.\"
$output += \"Secure environment variables (like $env:MySecretValue) are only secure if you keep them that way.\"
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
",
"retentionInterval":"P1D"
}
properties: {
arguments: '-Name Learner'
environmentVariables: [
{
name: 'Subject'
value: 'Deployment Scripts'
}
{
name: 'MySecretValue'
secureValue: 'PleaseDoNotPrintMeToTheConsole!'
}
]
azPowerShellVersion: '3.0'
scriptContent: '''
param ([string]$Name)
$output = "Hello $Name!"
$output += "Learning about $env:Subject can be very helpful in your deployments."
$output += "Secure environment variables (like $env:MySecretValue) are only secure if you keep them that way."
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
'''
retentionInterval: 'P1D'
}
Passing through parameters
As you've learned, you can set parameter values directly in the properties of the deployment script. There are several other options for values that can be passed in. You can use dynamic values coming from previously created resources, variables declared in the template, or parameters passed directly in to the template at deployment time.
These scenarios are available through template functions in the arguments
or environmentVariables
property. You can use any of the ARM template functions to access values and pass them in to the template. These functions include reference
, parameters
, or variables
.
These scenarios are available through template functions in the arguments
or environmentVariables
property. You can use any of the Bicep features to access values and pass them in to the template, such as refer to properties from other resources by using their symbolic names, and refer to parameters and variables.