Get started with the new Kotlin Azure Functions Archetype and IntelliJ IDEA
Author | Dave Glover, Microsoft Cloud Developer Advocate |
---|---|
GitHub | Get started with the new Kotlin Azure Functions Archetype and IntelliJ |
Platform | Azure Functions |
Programming Language | Kotlin |
Date | As at June 2019 |
This is an end to end walkthrough for creating Kotlin Azure Functions.
Kotlin is an emerging development language rated as one of the most loved languages in the last Stack Overflow Developer Survey. It is becoming the default language for Android Development and being eyed with interested by those with investments in Java and looking for a more modern JVM language.
1. Reference Documentation
- Create your first Azure function with Java and IntelliJ
- Azure Functions Java developer guide
- Library for Azure Java Functions
- Azure Maven Archetypes
- Register Azure Functions binding extensions
- Maven Plugin for Azure Functions
- Azure Toolkit for IntelliJ
2. Set up your development environment
To develop a function with Java and IntelliJ, install the following software:
- Java Developer Kit (JDK), version 8
- Apache Maven, version 3.0 or higher
- IntelliJ IDEA, Community or Ultimate versions
- Azure Toolkit for IntelliJ (Optional)
- Azure CLI
- Azure Functions Core Tools, version 2
The JAVA_HOME environment variable must be set to the install location of the JDK to complete the steps in this article.
3. Create a Kotlin Azure Function Project
- From IntelliJ IDEA, select Create New Project.
- In the New Project window, select Maven from the left pane.
- Select the Create from archetype check box, and then select Add Archetype for the azure-functions-kotlin-archetype.
-
In the Add Archetype window, complete the fields as follows:
- GroupId: com.microsoft.azure
- ArtifactId: azure-functions-kotlin-archetype
- Version: Use the latest version from the central repository
Enter your details for the current project, and select Next.
Define the GroupId and the ArtifactId for the project This information is added to the project pom.xml. The ArtifactId forms part of the Azure Function name in the pom.xml file. For more information see Creating a new Maven projectConfirm Project Name and Location, and select Finish.
The project structure will be created.
Maven creates the project files in a new folder with the same name as the project ArtifactId value. The project's generated code is a simple HTTP-triggered function that echoes the body of the triggering HTTP request.
4. Enable Auto-Import
5. Azure Configuration
By default, the pom.xml file is opened when the project is created.
You can change the following project properties in the pom.xml file
- Azure Function App Name
- Azure Function App Region
Run the following command for a complete list of regions. Choose the location by the "name" field in the returned JSON array.
az account list-locations
6. Open the default Http Trigger
The Azure Functions Maven Archetype will create an example Http Trigger. You will find this by navigating the src project directory.
7. Clean and Package the Project
From the Maven pop-out tab, expand Lifecycle, then run the clean followed by the package commands. This will build the Kotlin project.
8. Execute Azure Functions Run
From the Maven pop-out tab, expand Plugins, then expand azure-functions, then select azure-functions:run. This will start the Azure Functions Core Tools and bootstrap your Kotlin Azure Function.
To test the function click the http://localhost:7071/api/HttpTrigger-Kotlin link
TIP: The function runs in the context of a local process called func on port 7071. Sometimes this process does not close properly. If this happens, then next time you run the function it will complain that port 7071 is already open. You need to close the func process manually from the task/process manager of your operating system.
9. Pass in a parameter on the Query String
In the browser add a name parameter to the query string. For example, http://localhost:7071/api/HttpTrigger-Kotlin?name=dave and you will see the webpage echos the value passed in for name.
Alternatively, you can trigger the function from the command line using curl in a new terminal window:
curl -w '\n' -d Dave http://localhost:7071/api/HttpTrigger-Kotlin
10. Stop the function
Click the stop icon to stop the function from running.
11. Enable Debugging
Right mouse click on the azure-functions:run Maven Archetype and select Create 'glovebox-function [...
12. Create Run/Debug Configuration
- Rename the configuration to highlight that package will be run before azure-functions:run.
- Add -DenableDebug to the command line.
Add package
Click OK.
Click OK
13. Enable JVM Debugger Attach
From Run menu, select Edit Configuration
14. Add New Configuration
Click the + sign, then select Remote.
15. Name the New Configuration
In this case, the configuration is named Attach Debugger
Click OK
16. Set a breakpoint in the Kotlin Azure Function source
17. Run Debugger Enabled Configuration
From the run/debug configuration selector select the Maven azure-functions:run configuration. and the click the green start icon or press Shift+F10.
18. Attach the Debugger
From the run/debug configuration selector select the Attach Debugger configuration and click the green start debugger icon or press Shift+F9.
19. Initiate the Http Trigger in Debug Mode
Select the Run tab.
Click the http://localhost:7071/api/HttpTrigger-Kotlin link to initiate the Http Trigger.
20. Step through the Http Trigger Azure Function with the Debugger
Using the debugger controls, step through the Azure Function code.
21. Stop the Debugger and Azure Function
Click the Stop icon to detach the debugger and stop the Azure Function
22. Adding new Azure Function Triggers
The azure-functions:add Maven archetype supports the following trigger types.
- HTTP Trigger
- Azure Storage Blob Trigger
- Azure Storage Queue Trigger
- Timer Trigger
- Event Grid Trigger
- Event Hub Trigger
- Cosmos DB Trigger
- Service Bus Queue Trigger
- Service Bus Topic Trigger
From the Maven pop-out, under Plugins, select azure-functions:add
23. Deploying Kotlin Azure Functions to Azure
To deploy the staging directory to target Azure Functions. If target Azure Functions does not exist already, it will be created.
Congratulations
You have created your first Kotlin based Azure Function
Next
Try out the Building a Serverless IoT Solution with Kotlin Azure Functions and SignalR
Top comments (0)