I'm currently developing a small game that will rely on a lot Azure App Functions to execute function from time to time. I followed a tutorial on MSDN (https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-vs#configure-the-project-for-local-development) explaining that I had to create a new project to host a function but so far, I already have 6 different functions and I don't really want to create 6 different projects.
Moreover, all these functions (developed in JavaScript) have a lot of code in common so I created a common JavaScript file with some helper function. Now that I have multiple projects, I can't use it anymore without copy/pasting it in all projects.
Finally, to be able to correctly develop the game, all the functions must be running in parallel on my development machine and I don't really want to open 6 (or more in the future) powershell instances to host these functions.
Is there a way to host multiple functions in the same project and deploy them easily on Azure ?
That's what Function Apps are for. Each Function App may contain multiple Functions, which will be deployed together.
You mention Javascript, but the linked tutorial is in C#. Regardless, you can put multiple functions into the same app: subfolders under the same root (where host.json file is), or static methods in the same C# project. Each function will have a separate function.json file. All functions can share the same code.
Related
I have developed multiple event-driven cloud functions using NodeJS in GCP. Each cloud function has it's own package.json and index.js also there are many custom module (or utilities) file I created as part of each cloud functions.
But these custom modules are redundant across cloud functions i.e., they are repeated in every cloud function.
Now my question is, if I want to make an update/change in any one of the utility file then I must do the same change in all cloud functions individually. Also, what I observed is that package.json has many common dependencies but with different versions.
To avoid this, I am thinking to group these individual cloud functions into one with only one package.json and single copy of custom modules that can be referred by all cloud functions.
I tried my best to find some useful resource to implement this but couldn't find one.
I referred this document and here they mentioned that it's possible to have multiple function entry point (defined here)
Can anyone let me know how do I group multiple functions to one?
Current project structure:
Root-directory-
-dir_cloudFunction_A
-index.js
-myModule.js
-package.json
-dir_cloudFunction_B
-index.js
-myModule.js
-package.json
Currently I have two cloud functions under a root directory, cloud function A is in dir_cloudFunction_A directory and cloud function B is in dir_cloudFunction_B directory.
Each cloud function has index.js, myModule.js and a package.json
Now I have myModule.js (custom module) in each directory (same copy). Problem with this approach is that, when I need to make any changes in myModule.js, I must ensure that it is updated in all of it's copy wherever this has been referred.
To avoid this issue, I am suggested to share this myModule.js in one place and use it in 'n' number of cloud functions in root directory.
But I'm not sure how can I do this.
I'm developing my first node.js app deploying to GAE.
It'll be organized as an API service and a front-end web app developed with Next.js
I'm looking at this architecture, and, although I have the app separated in two repositories I could have one merged repo to create two different microservices:
https://medium.com/this-dot-labs/node-js-microservices-on-google-app-engine-b1193497fb4b
For me, it seems overwork creating a new repo to merge them and deploy (doesn't it break one of the basic ideas of microservices to make isolated deploys?)
I have to discourage this because we need SEO in some of the parts, and We should use Next.js (or similar):
https://cloud.google.com/storage/docs/hosting-static-website
Another idea I've been working on is... create different GAE projects for front and API to deploy independently. For me, it seems like the best option, but I would like to know your opinion as GAE experts.
Which one should I use?
Thanks!
GAE doesn't care how is the code to be deployed into the services mapped to one or more VCS repositories (or no repositories at all). That's entirely up to you.
With a single repository you may encounter difficulties deploying from CI/CD pipelines - for example unnecessary deployments to one service when only the other one is changed.
Many examples out there focus on applications rather than services, but those are nothing more than the default services of those applications. Personally I like keeping the code for different services in separate directories, see the image captured in Can a default service/module in a Google App Engine app be a sibling of a non-default one in terms of folder structure? (it's no longer present on the updated documentation page). This also allows for easy mapping to multiple, separate VCS repositories
As for multiple projects vs multiple services, this might be of help: Advantages of implementing CI/CD environments at GAE project/app level vs service/module level?
The static website link you mentioned isn't part of GAE, it's part of GCS - a different GCP product. It's fine to use by itself - for a static website, but it might be difficult/impossible to:
communicate between a service running on it and one running on GAE - if you need that
make the 2 services appear as one (for example serve under the same custom domain name)
As per the new template in Visual Studio 2017 prev 2, all function in a function app do share dependencies. Any custom business code/library added will be available to all other functions in a function app.
There is a problem here that it is will give monolithic design a chance to sneak in. And any change in a function would require entire set of function to be redeployed.
Another design can be to keep the function app as thin as possible (if possible one function per function app in my opinion). This would segregate the code and dependencies of each function, but on the flip side, I would bring a deployment/maintainability nightmare.
Is there any design guideline to be followed which could balance maintainability hassle and help achieving micro service design goal ?
It's up to you how much you want to group together vs be separate on their own. You can also have all your functions in 1 project in Visual Studio then deploy it to N number of function apps, then you can specify which functions a given function app should run in your host.json. Check the functions property in here https://github.com/Azure/azure-webjobs-sdk-script/wiki/host.json
Note that the Portal UI doesn't handle that correctly now, so you may get odd errors if you go in the portal. Tracked here https://github.com/Azure/azure-functions-ux/issues/1428
I have common code which is require in other Azure functions, how can I share the common code among the Azure functions?
If the Functions you are wanting to share code between are under the same Function App you can do the following:
https://stackoverflow.com/a/39541156/2854993
And if they are separate Function Apps, I asked a similar question - see:
Azure Functions - Shared code across Function Apps
I managed to get shared code to work (or rather: compile) using an Azure Function Tools for Visual Studio project. In case of the screenshot, the shared library is actualy another Function project. I also tried to work with class libraries: those seem to work as well.
Haven't gotten around to deploying it yet, by the way. Running (and debugging) it localy is not an issue, so I guess deploying shouldn't be one either.
Is there a way of sharing common code across two different Function Apps in Azure?
I understand it is possible to share code between two functions under the same Function App like so:
#load "../Shared/ServiceLogger.csx"
but I would like to share logging code between two different functions, each under it's own Function App. The reason for the functions being under two different Function Apps is that I need one to run on the Consumption plan and the other run on an App Service plan, unless there is another way of doing this?
There is no straightforward way to share code across app boundary in Azure App Sercice, and this holds true both for Function Apps and Web Apps. You will need to deploy the relevant code into each app that needs it.
What I did to get around this before the fix in VS17 Preview (which treats functions as normal cs files and allows project references) is I had a shared class project that had a post build event that would put the dlls into the Azure function projects in a folder. Then reference them with #r "file path to the dll"
One way to reuse code is to use precompiled assemblies / class libraries with your Functions. Donna Malayeri has published a blog post explaining how to achieve this: https://blogs.msdn.microsoft.com/appserviceteam/2017/03/16/publishing-a-net-class-library-as-a-function-app/
Consequently, you could have 2 or more different Functions, deployed to different AppServices or Function instances sharing the same code through a class library. Precompiled assemblies are supported in .NET C# Functions and you can do something similar using WebPack in Node.js based Functions