In this article, we are going to have a look at some of the commonly used file functions. Then we will also have a code example demonstrating usage of these functions.
There are three file functions and all of them are from sys namespace.
- loadFileAsBase64 – Loads the file as a base64 string. Need the File path to be passed as parameter
- loadJsonContent – Loads the specified JSON file as an Any object. We can either load the full file or we can specify the json path of the node which needs to be loaded. We can also specify the encoding.
- loadTextContent – Loads the specified as a string.
The paths are specified with relative to the current bicep file. Also, there are some max limits, indicating how much content can be loaded via each of these APIs. I would suggest to refer the documentation for further details.
Where these functions can be useful ?
I can currently think of two use cases
- If there is some script (PowerShell / Bash) and you want to execute that script as a part of deployment, then you may want to use one of these functions to load the script contents
- If you want to supply some values from a file, then also these functions can be useful. For example, the storage account name and pricing tier can be loaded from a JSON file.
Let’s have a look at these to uses cases in the following section.
Load Inputs From JSON
Let’s create a JSON file in a directory. It should have only two properties as shown below.
Now in the same directory, let’s create a bicep file. This bicep file can use the file function to load the JSON contents. Once the JSON object is loaded, the object can be used to provide the inputs for creating the resource.
You can try to deploy this bicep file and it should appropriately read the inputs from JSON and it should be able to create the contents.
Deployment Script From File
This is another example taken from the documentation. It basically reads the contents of a PowerShell file and then uses it in the deployment script task.
I hope you find this information helpful. Let me know your thoughts.