In the previous post, we learned about my proposal of an architecture for Azure API Management for integrations. And I closed the post with a pending thing, which was details on how to deploy and configure all the Azure resources the solution uses.

And in today’s post we’re going to see how all the resources are created, linked together and configured to have a fully working API logging solution, and how we can deploy everything using Bicep.

Nice Bicep...
Nice Bicep…

And the trick to deploying it is to…

Do it manually!

Of course! That’s the logic way of doing it! With our keyboards and mouses, clicking buttons and entering names, then looking in Azure for configurations, connection strings, enabling options, etc.

That’s what I did the first time I set this up, I created all the resources from the Azure portal by hand, changing the configuration of the different elements until I had everything working fine.

But then I thought that there should be a way of deploying this automatically and if, for example, I had to deploy this in different customers or projects, could reduce the chance of having errors, or just avoid repeating work that can be automated.

I knew about the ARM templates, and I also knew that I don’t like them a lot. That’s when I remembered I had heard about this new thing…

Use Bicep!

Bicep is a language used to deploy Azure resources that we’ll use to create infrastructure-as-code (IaC) solutions.

One of the benefits of using IaC is that it helps automate resource deployments, and helps us to make sure that we deploy the same resources each time our code is run. The same code will always be deployed as the same infrastructure.

To deploy all the resources, you need an Azure subscription. Remember, you can register and get one free and with $200 of credit for 30 days, and that several services are always free of charge.

Bicep uses declarative syntax, which means that we describe what we want the code to do, and how the code does it is a problem of the compiler. I personally find it much easier to read than the JSON from the ARM templates.

And starting using it is really easy, you just need to install the Bicep tools, which if you’re using Visual Studio Code is as easy as installing an extension.

Then imagine you want to deploy a new storage account to your Azure subscription. We’ll write the following code:

If you use the VS Code extension it’s almost as easy as writing .NET code, hitting the tab key 🙊 Joking… let’s take a look at the different parts of the code.

All resources are declared using the resource keyword, then aristeinfoStorageAccount would be like the variable name for that resource, and you can use it later in your code to access its members, just like an object in any OOP language. And what comes next, the ‘Microsoft.Storage/storageAccounts@2021-09-01‘ piece, is the type of resource, which the extension helps us look for it, you don’t need to remember how all resources are named.

Then inside the curly brackets we have:

  • The name, which will be the name of the resource in Azure.
  • The location, is the region where the resource is deployed to.
  • kind, is the version of the storage account.
  • sku, the tier of the resource.

Of course, different types of resources have different elements in its setup, and different properties.

Before deploying the file, make sure you have installed both the Bicep and Azure Account extensions for VS Code. And first, connect to your subscription by pressing F1 and looking for “Azure: Sign in”.

Now to deploy it, you just need to right-click on the VS Code tab and select the “Deploy Bicep file…” option:

Deploying a Bicep file from VS Code
Deploying a Bicep file from VS Code

Thanks to the Bicep extension you will see a dialog to select the subscription, the resource group (or create one) and after that it will also ask to select a parameter file, select None.

You will see a message in the output window, and if you go to the Azure portal (or click the link in the output window), navigate to the resource group you’re deploying this to, and select the Deployments option you will see the deployment:

Bicep deployment in Azure portal
Bicep deployment in Azure portal

And when it’s finished, you can go to overview and see your resource:

Storage account deployed using Bicep
Storage account deployed using Bicep

It’s that easy, and if you want to learn more about Bicep, you can check Microsoft Learn learning paths for Bicep.

Deploying the APIM solution

I wanted it to be easy for everyone to try this, so I prepared everything and if you go check this GitHub project I’ve created, you will find the following things:

The X++ folder that contains a model with a custom web service that will create a calculator API in your F&O environment. There’s both a model folder and an axpp file you can import in Visual Studio.

The AzureFunction folder contains the code to create an Azure Function that will create a calculator API.

And finally, and what interests us the most, the Bicep folder that contains the needed code to create all the Azure resources, so you can test what I explained in my previous post without having to create all the resources manually. And you have two options.

Deploy an Azure Function

If you choose to try the solution using an Azure function, you just need to deploy the mainAzureFunction.bicep file.

You don’t need to create and deploy the Azure Function because the Bicep file will download this ZIP file which is only the Azure Function packaged to make your life easier, and this faster to try. If you prefer to deploy the function for security reasons, you’ll need to update the azureFunction.bicep file inside the modules folder.

If you choose this option, the dialogs will ask you for the following parameters: Subscription, Resource Group, location (which is defaulted to that of the resource group), prefix to be used as the name of the resources and, finally, email and organization to deploy the APIM (just your email and tenant but don’t worry no emails will be sent).

Deploy X++ code

If you choose this option, you have to download the code from the X++ folder and add it to your environment. Once you’ve done that, you can deploy the mainDynamics.bicep file.

If you prefer using an X++ custom web service to try this, you’ll have to provide additional information. In addition to the same parameters as the ones of the Azure Function solution, you’ll need to provide the environment URL (without the final /), the environment URL with the API information (service group and service name, also without the final /), tenant, AAD App Id and secret.

This way of deploying the code will create a policy in your APIM that will handle the token authentication, that’s why it’s asking for the tenant, App Id and secret.

The Bicep files

In the Bicep folder you will find these files:

Bicep folder
Bicep folder

The mainAzureFunction.bicep and mainDynamics.bicep files are what needs to be executed/deployed, depending on which way you choose to go.

Let’s take a look at the mainAzureFunction.bicep file:

I’ve put the parameters and variables at the beginning of the file and the resources after that.

When you use a parameter, that’s declared using the parm keyword, you need to provide that parameter each time the Bicep file is run.

Variables are declared with the var keyword and… you remember about resources, right? But there are no resources in this file, instead there are…

Modules

In Bicep, modules are files that are deployed from other files. Think of them as other classes which we can use in the main file. It’s something like this.

In modules, you usually group resources that need to be deployed together or that can be reused in other files.

The modules’ folder contains other Bicep files that help deploy all the infrastructure.

For example, let’s take a look at the azureFunction.bicep file:

First, we see parameters too. These parameters will be required when using the module in another file.

And then we see the resources. You can see I’m deploying a storage account, an app service plan and the function. These go in this module because those are things needed when you deploy a function.

And at the end we see output variables, that can be accessed from the file that uses the module, like a return in a method.

Deploy

I’m going to show this using the Azure function, I open the mainAzureFunction.bicep file and right click in the tab and select “Deploy Bicep file…”:

Deploy Bicep file
Deploy Bicep file

You will see a list of the subscription the user you’ve authenticated with has access to:

Azure subscriptions
Azure subscriptions

Then you’ll get asked which resource group you want to deploy it to, or if you want to create a new one:

Create new resource group
Create new resource group

I’ll create a new resource group. When you do this, you will get an additional dialog asking in which region you want to create it from a list.

Then you can select a parameter file, but we’re not using it, so select “None“.

And now we have to select the region where the resources are going to be deployed. Because in the code we’re using the resourceGroup().location value, we see this option in the dialog, but we could use a different one.

Resource group selection
Resource group selection

Next we enter the prefix, keep in mind it has to be a unique name:

Prefix
Prefix

And finally enter the email and publisher name, your name or domain.

And, finally, a dialog will ask if we want to create a parameter file, select “No”:

Parameter file from values
Parameter file from values

After that we’ll see the message in the output window, and if we want to see the operation status we go to the resource group and “Deployments” option:

Deployments in resource group
Deployments in resource group

It will take a few minutes to deploy everything, and when all it’s done we can go to the overview and see all the resources there:

APIM architecture resources
APIM architecture resources

And just like that, everything is ready to try!

One last cool trick

You may already have noticed this while testing the bicep solution, but when you right-click on the VS Code tab for your main Bicep file, you can select any one of these two options:

Bicep Visualizer
Bicep Visualizer

The Bicep Visualizer will open a new tab and show a diagram of the resources, with their icons, and arrows pointing to the relationships between resources.

Bicep diagram
Bicep diagram

Maybe it’s not that important, but I really like it 😛

Let me insist: if you want to learn more about Bicep, go right now to Microsoft Learn learning paths for Bicep, you don’t even need an Azure subscription because the Learn modules deploy a free sandbox account for you to do the exercises.

And that’s all, I hope this helps you a bit as an introduction to Bicep, and also to deploy the APIM architecture I proposed and try it, and see if it can be helpful for your projects.

Subscribe!

Receive an email when a new post is published

2 Comments

  1. Thanks Adria for introducing Bicep, definitely will give it a try.

    • Adrià Ariste Santacreu Reply

      Thanks for reading it Andrew. I hope it’s useful!

Write A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

ariste.info