Shaun Luttin
Shaun Luttin

Reputation: 141452

Set an appsetting value conditionally in an ARM template

We have an Azure ARM template, which is adding appsettings for a Microsoft.Web/site.

"resources": [
    {
        "apiVersion": "2016-03-01",
        "name": "myazurefunction", 
        "type": "Microsoft.Web/sites", 
        "properties": {
            "name": "myazurefunction", 
            "siteConfig": {
                "appSettings": [
{
    "name": "MY_SERVICE_URL", 
    "value": "[concat('https://myservice-', parameters('env'), '.domain.ca')]"
}
                ]    
            }
        }
    }
]

We also have four parameters.environment.json files. For instance, this is the content of parameters.dev.json.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01...",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "env": {
            "value": "dev"
        }
    }
}

The template and its parameters favor convention over configuration. This is working nicely for the most part, and leads to the following MY_SERVICE_URL values.

The problem is that we want to break the convention for the dev environment. That is, we want it to have a MY_SERVICE_URL that looks something like this:

How can we configure the ARM template to break the convention for only one environment?

My first though is to use a conditional like this, but such an ARM function appears not to be available.

"name": "MY_SERVICE_URL", 
"value": "[parameters('env') -eq 'dev' 
    ? 'https://abc123.foo.bar.baz.ca' 
    :  concat('https://myservice-', parameters('env'), '.domain.ca')]"

Upvotes: 1

Views: 2079

Answers (2)

Shaun Luttin
Shaun Luttin

Reputation: 141452

For the example in the question, the answer ended up looking like this:

"variables": {
   "myServiceUrl_default": "[concat('https://myservice-', parameters('env'), '.domain.ca')]",
   "myServiceUrl_dev": "https://abc123.foo.bar.baz.ca",
   "myServiceUrl_ci": "[variables('myServiceUrl_default')]",
   "myServiceUrl_qa": "[variables('myServiceUrl_default')]",
   "myServiceUrl_prod": "[variables('myServiceUrl_default')]",
   "myServiceUrl": "[variables(concat('myServiceUrl_', 'parameters('env')'))]"  
},

...

"appSettings: [
   {
      "name": "MY_SERVICE_URL",
      "value": "[variables('myServiceUrl')]"
   }
]

Upvotes: 0

4c74356b41
4c74356b41

Reputation: 72151

just create a variable that would depend on the parameter:

"parameters": {
...
"DeploymentType": {
    "type": "string",
    "allowedValues": [
        "Dev",
        "Prod"
    ]
}
...
"variables": {
    "Dev": "https://some_service-ci.domain.com",
    "Prod": "https://abc123.foo.bar.baz.com",
    "DeploymentVariable": "[variables(parameters('DeploymentType'))]",
...
"appSettings": [
    "name": "MY_SERVICE_URL", 
    "value": "[variables('DeploymentVariable')]"
]
...

Ok, so how does this work. you pass in the parameter 'DeploymentType', it can be PROD or DEV. If you pass DEV "DeploymentVariable": "[variables(parameters('DeploymentType'))]", - this evaluates to "[variables('Dev')]" and gets the value of "Dev": "https://some_service-ci.domain.com",

Upvotes: 2

Related Questions