2

I'm working on building Azure Resource Manager (ARM) templates and I'm having an issue that I can't work out. We have several development environments (sandbox, development, production, etc.) and I'm trying to wrap my head around how best to accomplish what I'm wanting.

I want to feed a parameters file that defines an environment parameter to identify which environment we're building. For example, here's one for the "dev" environment (there are more parameters, this is just minimal to be relevant).

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

I currently have inside the ARM template several variables that I want to be able to pull values out based on the environment, but I'm having trouble getting my ARM templates to build and the error messages are terrible ('malformed string' when validating using Azure CLI, and "BadRequest" is all I get when a build agent tries to deploy the resources).

If I have a variable defined as:

"sampleObject": {
            "sbx": {
                "value1": true,
                "value2": true
            },
            "dev": {
                "value1": true,
                "value2": true
            },
            "prd": {
                "value1": false,
                "value2": true
            }
}

I want to use these fields to further define variables "envValue1" and "envValue2" for use in defining resources later in the template, so I was trying something like this:

"environmentParams": "[variables('sampleObject')[parameters('environment')]]",
"envValue1": "[variables('environmentParams').value1]",
"envValue2": "[variables('environmentParams').value2]"

However the above doesn't appear to be working, and while I've used everything else in my templates before this is causing issues. Specifically I think it's the line

"environmentParams": "[variables('sampleObject')[parameters('environment')]]"

I can find several examples of people using "[variables('environmentParams').value1]" to grab pieces of an object variable before, but I'm uncertain if I can pass in the parameters('environment)' piece like I have done before and I can't find anything using it this way.

What am I doing wrong? Is it possible to pass in the environment parameter to get a piece of an object? I have tried the following and nothing has worked thus far (testing creating the resource using Azure CLI).

"[variables('sampleObject')[parameters('environment')]]"
"[variables('sampleObject').[parameters('environment')]]"

If what I'm trying to do is not possible, how do people manage multiple environments? Do they just shove everything into a parameter file, making it difficult to see the differences between environments by looking at the ARM template?

1
  • this looks perfectly fine: "environmentParams": "[variables('sampleObject')[parameters('environment')]]", Commented May 12, 2017 at 6:37

1 Answer 1

7
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "environment": {
            "type": "String"
        }
    },
    "variables": {
        "sampleObject": {
            "sbx": {
                "value1": true,
                "value2": true
            },
            "dev": {
                "value1": true,
                "value2": true
            },
            "prd": {
                "value1": false,
                "value2": true
            }
        },
        "value": "[variables('sampleObject')[parameters('environment')]]",
        "othervar": "[variables('value').value1]"
    },
    "resources": [ ],
    "outputs": {
        "we": {
            "type": "Bool",
            "value": "[variables('value').value1]"
        }
    }
}

this works perfectly fine, you can use dot notation to access object properties easily, you can use this sample.

But you cannot do:

"[variables('sampleObject').[parameters('environment')]]"

So no dot if you are using variable to access property.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for confirming the proper syntax. I was certain this was the issue I was having but it's clear it's something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.