Bicep - Parameter Decorators
In my previous article, I covered Parameters and Variables and how to use them. This article is a continuation of my previous two articles which are found here and here. In this article, I will be taking a look at Parameter Decorators and how you can use them to write clean and reusable Bicep code as well as protect it against unwanted values.
What is a Parameter Decorator?
Parameters use decorators for constraints or metadata. All decorators are in the format @expression() and are placed directly above where you have declared the parameter.
Decorators are in the sys namespace so if you need to differentiate a decorator from another item, you can preface the decorator with sys. to allow Bicep to use the decorator. For example, if you have a parameter called description, whenever you want to use the description decorator, you will have to preface it with the sys namespace.
If you do not preface it, you will see the following:
With the error showing:
So this is something you will need to keep in mind as it does not only apply to description, but to any of the decorators mentioned below.
Different types of Parameter?Decorators
The following table describes the available parameter decorators, what they apply to, and what type their argument is:
How to use Parameter?Decorators
Parameter decorators are extremely easy to use and I will show you how to use each one below. Please note that we can have more than one decorator present at a time to provide more information or further apply constraints.
@allowed
To use this decorator, create your parameter, and then in the line above it, type @allowed([]). Inside the square brackets [], press enter and then define your values to be allowed, with each value on a new line as shown below:
@description
To use this decorator, create your parameter, and then in the line above it, type @description(''). Please note that Bicep uses single quotes, if you try to use double quotes, it will error. In between the two single quotes, you can type your description for the parameter. Try to explain what the parameter is and how to use it.
Markdown-formatted text can also be used for the description text, you can go absolutely nuts with this to provide as much information as possible with whatever structure you choose.
And when you hover the parameter:
领英推荐
@maxLength
To use this decorator, create your parameter, and then in the line above it, type @maxLength() and place your value as an int between the () as shown below:
Can be used in conjunction with @minLength() to provide further constraints.
@minLength
To use this decorator, create your parameter, and then in the line above it, type @minLength() and place your value as an int between the () as shown below:
Can be used in conjunction with @maxLength() to provide further constraints.
@maxValue
To use this decorator, create your parameter, and then in the line above it, type @maxValue() and place your value as an int between the () as shown below:
Can be used in conjunction with @minValue() to provide further constraints.
@minValue
To use this decorator, create your parameter, and then in the line above it, type @minValue() and place your value as an int between the () as shown below:
Can be used in conjunction with @maxValue() to provide further constraints.
@metadata
To use this decorator, create your parameter, and then in the line above it, type @metadata({}). Inside the curly braces {}, press enter, and then define your values as shown below:
This decorator can be used to track additional information that is not relevant to the description or other decorators.
@secure
To use this decorator, create your parameter, and then in the line above it, type @secure() as shown below:
The value for databasePassword will not be exposed in the input section of the deployment section within Azure Portal and if you do not provide a value, you will be asked to input it using the CLI once you have run the deploy command.
That's the end of the article! I hope this has been useful for you and you have either learned something or simply had a refresher! Keep an eye out for my next article!