Bicep - Parameter Decorators
Microsoft - Fundamentals of Bicep

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.

No alt text provided for this image

If you do not preface it, you will see the following:

No alt text provided for this image

With the error showing:

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

@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.

No alt text provided for this image

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.

No alt text provided for this image

And when you hover the parameter:

No alt text provided for this image

@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:

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

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:

No alt text provided for this image

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!

要查看或添加评论,请登录

Declan Taggart的更多文章

  • Bicep - Parameters and Variables

    Bicep - Parameters and Variables

    In my previous article, I covered the creation of a basic Bicep file with the commands that are required and a…

  • Create your first Bicep file

    Create your first Bicep file

    What is Bicep? Bicep is a domain-specific language (DSL) that uses a declarative syntax to deploy Azure Resources. In a…

  • Basic unit testing with xUnit

    Basic unit testing with xUnit

    Unit testing has many benefits and will help you identify bugs and problem areas in your code. If we look at the…

  • Dependency injection in Azure Function v3

    Dependency injection in Azure Function v3

    In this article, I am going to show how DI (Dependency Injection) can easily be achieved within an Azure Function…

    2 条评论
  • My basic wireframe for Azure Function v3

    My basic wireframe for Azure Function v3

    In this article, I will show you the wireframe I use for every Azure Function that I create and use. This demo will be…

    2 条评论

社区洞察

其他会员也浏览了