Going serverless with custom portal for D365 environments
Tags: javascript, nextjs, react, serverless
Published: 2022-04-22 08:23:21

_In the [first article](https://dev.to/rainforss/test-4h3k) of the series, I "briefly" addressed the pros and cons of Microsoft's Power Apps Portal and the reason why it might not be a cost-efficient product for companies at all scales who need to open a controlled "portal" to a specific piece of their D365 data._

The standalone web server which is supporting the Power Apps Portal plays a major role depicting the product as a non-optimal solution for smaller organizations. Most of the time, a business operating in a single territory (or territories in a close proximity) does not need a web server which continuously listens for incoming requests since the traffic could be very localized and patterned. For example, Calgary Canucks Rugby Union - a non-profit organization operating in Calgary - tries to expose some matches information stored in Dynamics 365 through Power Apps Portal. It is very likely that traffic would hit the portal some time right after the rugby matches and rapidly die down until the next match result is posted. During this quiet period between the matches, utilization rate of the portal web server would be very low so the server will stay in an idle state awaiting requests. Therefore, smaller organizations using Power Apps Portal are essentially paying for a web server that's idling for more than 70% of the time.

One can propose the use of a _lower tier web server_ (like what Heroku offers for the free hosting) which enters a sleep mode if no traffic come in for a period of time, but I am going to take it further by adopting a [**serverless solution**](https://martinfowler.com/articles/serverless.html), removing a traditional server from the scene entirely.

Being a huge fan of [**NextJS**](https://nextjs.org/) (an opinionated framework based on ReactJS) and serverless functions, I decided to use NextJS to power the front-end of my custom portal and [**NextJS API routes**](https://nextjs.org/docs/api-routes/introduction) (together with Vercel's serverless functions) to orchestrate my portal's back-end services.

In short, **serverless functions** are invoked functions running in servers offered by larger organizations (AWS, Azure, Google, Vercel, etc.) whose profession is in cloud computing. Using serverless functions to access other web services (e.g., making HTTP requests to remote servers), the front-end application is enabled to do its job (providing user interactions) without the need of a dedicated traditional server. With the self-owned traditional server out of the solution, smaller organizations can focus on delivering user-centric content instead of sparing resources on maintaining a web server.

Now you might be asking, how do serverless functions communicate with Dynamics 365 environments to retrieve the data for the front-end web application? Making use of Azure App Registration and a application user in Dynamics 365, the serverless functions can obtain access tokens issued by Azure Active Directory OAuth2.0 service. Adding the access token to HTTP requests made by serverless functions, the web application is now authenticated and authorized as the application user to perform CRUD operations allowed by security role configurations. This is just a **high-level** overview of the integration, feel free to ask if you want to know more about the details.

Using the serverless functions, I was able to create a custom D365 portal with even registration and authentication functionalities for Calgary Canucks Rugby: [Calgary Canucks Rugby Union](https://www.canucksrugby.org/)


---


## How does serverless function save companies' money?


Serverless functions are on-demand services provided by cloud computing companies so you only need to pay for the functions being invoked. In the previously illustrated scenario, Calgary Canucks Rugby simply wouldn't have any expenditure when no traffic is hitting (hence no back-end services involved) the public facing website if serverless function was the back-end solution.

To make the serverless solution even better, companies such as Vercel are also offering free serverless function usage per month and it is actually very rare for front-end applications with less traffic to use up the monthly quota.



![Vercel Pricing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0jzb2vtok8dbf9dpacu.PNG)


By adopting the serverless function solution for a custom Dynamics 365 portal, Calgary Canucks Rugby and other similar organizations can save up a good amount of budget for other essential operations simply because they wouldn't be paying for web servers and web server maintenance. Sorry "webmasters" and "IT administrators" :(


---


_In case you are wondering, this article is not sponsored by Vercel or NextJS. I simply love Vercel and NextJS that much :)_