This document describes how to set up a development environment to support the development of a server-less Azure function in Kotlin.
This document DOES NOT describes an application nor it contains specifications about an application.
It only describes the building/preparation/set up of the local development for Azure environment on Linux
Pre-requisite
Before to start is necessary to have some tools/libraries/setting done in the local development machine.
IntelliJ Idea
node.js ( 1.8 )
At the moment the best JDK to be used is the 1.8. Newer ones can have problems.
The development machine is a Linux based one (Ubuntu 18.04 LTS).
Is necessary to have installed on the local development machine based on Linux :
After installing Azure CLI and Azure functions core tools, Azure Functions core tools 3 , Microsoft .NET and Maven Apache is possible to start to create the basic project using IntelliJ.
Also be sure to have installed on IntelliJ the Azure Toolkit for IntelliJ and Azure DevOps plugins.
IntelliJ Idea
For all the environments.
Maven Settings
Be sure to have some specific settings for Maven.
From :
File/Settings → Build, Execution, Deployment
Build Tools → Maven
Be sure to have the Use Plugin registry box unchecked and the box Work offline uncheckedBuild Tools → Maven → Importing
Check the line JDK for importer. Set it with the correct JDK selection, should be 1.8 for this specific projectBuild Tools → Maven → Repositories
Verify if the local repository is updated if not hit the button Update
Plugins
Is better to have some plugins installed in IntelliJ Idea before to start to work.
Azure Toolkit
Azure DevOps
Maven helper
Maven dependency
IMPORTANT !
After modifying the above settings, invalidate the cache and restart everything (will take a while since it has to re-index everything)
File → Invalidate Caches/Restart
Project creation
Create a new project based on Maven and select to install using the archetype.
It is necessary to use the archetype : com.microsoft.azure:azure-functions-kotlin-archetype
Very probably will not be in the list, to add it click on the button Add Archetype and :
GroupId : com.microsoft.azure
ArtifactId : azure-functions-kotlin-archetype
Version : 1.23
Repository (optional) : leave empty
Click OK. The archetype will be in the list.
Select it.
Important ! Currently Azure server-less support only JDK 1.8 ! It could work also with more recent ones however warnings will pop up to notify the presence of a wrong JDK.
If everything is OK the project will be created to support a JVM code as server-less function.
Since the archetype is Kotlin based there is not necessary to add the Kotlin support since is already in.
To manually add Kotlin support to a project
use the Tools | Kotlin | Configure Kotlin in Project… menu.
A dialog will ask to specify which modules to enable it in - keep the defaults (All modules).
In the project at this point there are two source code :
src/main/java/org.example/Function.kt
src/test/java/org.example/FunctionTest.kt
At this point the skeleton project for a server-less function in Kotlin is ready.
To build the code is necessary to “package” it.
To do so, on the right of the IntelliJ Idea editor open the Maven tab :
Under LifeCycle select Clean and then Package
To execute the function, open the Plugins/Azure-functions and select azure-functions:run
To test the Http trigger is possible to do two things.
From a browser, use this URL :
1http://localhost:7071/api/HttpTrigger-Java?name=Steve
The result in the page will be :
Or is possible to open a terminal and execute :
1curl -d "Steve" localhost:7071/api/HttpTrigger-Java
The Http trigger will return the input string (Steve) with “Hello”.
1steve@steve-Precision-T3600:~$ curl -d "Steve" localhost:7071/api/HttpTrigger-Java
2Hello, Steve!steve@steve-Precision-T3600:~$
Login to the Azure CLI
Go on Tools/Azure and select Azure Sign in.
Note ! It is implied the Azure CLI is installed on the machine and already logged in manually. Otherwise is necessary to select the Device Login.
Select the Azure connection to log in.
Deploy the function on Azure
To deploy the function on Azure, from IntelliJ project, Maven tab (on the right) open the Plugins/Azure-functions and select azure-functions:deploy
After the upload the function automatically start to run on the Azure environment.
To see it login in Azure web console and select Function App.
Using the function From Azure
In order to use it, the new URL assigned by the system need to be used.
To retrieve the URL, from the Function App click on the app
From the menu on the left, select Functions
Clicking Functions change the screen.
Select the function (the first time there is only one function, the Http trigger example.
Click on it.
On the new window on the top there is a button : Get Function Url
Click on it and retrieve the URL.To test the function is necessary to use that URL.
So curl will become :
1curl -d "Steve" <url retrieved>.azurewebsites.net/api/HttpTrigger-Java?code=0kOymUA2lfyTtnnajFTYCKTDHPSyvUZnG2J64NYqRZlyRdGYoEyNOQ==
Using a browser the URL to use will be :
1https://<url retrieved>.azurewebsites.net/api/HttpTrigger-Java?code=0kOymUA2lfyTtnnajFTYCKTDHPSyvUZnG2J64NYqRZlyRdGYoEyNOQ==&name=<put your name>
Expanding the project
The default project has an Http trigger. The goal is create a timed trigger in order to call the server-less function at specific time intervals.
To add a new trigger is necessary to use the procedure from the Maven tab for Azure : azure-functions:add
A menu will appear in the run area with the choice of trigger to be added :
Note that the generated code is in Java and NOT Kotlin, so is necessary to convert it.
If everything is set correctly will be enough to open the java code and press Ctrl-Shift-Alt-K to automatically convert it in Kotlin.
Depending the type of trigger to add is necessary to have also other libraries installed, like the Microsoft .NET one.
Troubleshooting
Missing information on local.settings.json
With any trigger other than the Http is necessary to populate the file local.settings.json file with information about the server-less function on Azure.
If trying to run the code in local ends up with an error and the error indicates missing values in the local.settings.json file, it is necessary to update it with some info from the Azure account.
The best way to do so is :
open the terminal at the bottom of IntelliJ
login to the Azure environment :
1az login
after the login execute the command :
1func azure functionapp fetch-app-settings <application name>
To retrieve the application name go on the Azure account via browser and go on the Function App.
Note ! This will work ONLY if at least one deployment was done !!After this call the local.setting.json file will be updated.
From the Maven tab :
Clean the project (Azure Kotlin Function → Lifecycle → Clean)
Package the project (Azure Kotlin Function → Lifecycle → Package)
Run the project (Azure Kotlin Function → Plugins → azure-functions:run)
References
Microsoft - How add Microsoft Maven archetype in IntelliJ
Microsoft - Setting up a Kotlin app project in Azure
Apache - Install Apache Maven
GitHub - Azure Functions Core Tools
Microsoft - Azure Functions Core Tools 3.0
Microsoft - Install .NET on Linux
Blog - Get started with the new Kotlin Azure Functions Archetype and IntelliJ IDEA
No comments:
Post a Comment