SpyglassMTG Blog

  • Blog
  • Centralize Common Configuration Within IaC

Centralize Common Configuration Within IaC

Centralize Common Configuration Within IaC

As you grow your maturity with Infrastructure as Code (IaC) you will find that there are common things that are the same across your deployment templates. These are things that help you become more consistent and standardized through your approach to workload deployments.

These are things like naming conventions/formats along with abbreviations for regions, resource types, etc. You want to make sure that different people that are writing deployment templates are using the same abbreviations to ensure consistency across deployments. For example, if you are deploying to a production environment, you use ‘prd’ versus ‘prod’ as the abbreviation. Within Bicep you can keep this configuration within a json file that is loaded at deployment time using the loadJsonContent() function.

Sample common configuration json file

MH1

As you can see from the above json format, there are keys for the following information:

  • format that has the format for which the name of the resource will be built within the resource deployment that contains placeholders for each value that will be replaced at deployment time.
  • locations that has an object of regions along with their abbreviations.
  • resourceTypes that has an object of resource types along with their abbreviations.
  • environments that has an object of environments along with their abbreviations.

In the bicep code below you will see how that json file is loaded and then used to build the name of the resource group as it is deployed.

Sample bicep file

MH2

As you see new variables are created for each key within the json file and then used in a replace() command to replace the placeholder value. For example, resourceTypes.resourceGroup gets the value for the resource group abbreviation. Location is handled a little bit different as the location that the resources will be deployed to is a parameter for the template, locations[location] gets the abbreviation for the region where the location passed in the parameter is used as the index to return the correct abbreviation.

By using a common configuration like the one shown here, you ensure that you are always using the same abbreviations on all of your resources and as you need to add a new abbreviations for a new region or a new resource type, all of your code will automatically have that available since it is stored within a single common file.

You can also do this within Terraform code by using the same formatted json file and similar replace() commands within the resource deployment, but you will need to add the following example for locals that will load the json file and set the variables that are used in the replace() commands.

Sample terraform file

MH3

If you have questions about centralizing your common configurations within IaC, contact us today!