loading...
Microsoft Azure

Creating Kotlin based Azure Function with IntelliJ - Part 1

gloveboxes profile image Dave Glover Updated on ・8 min read

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

2. Set up your development environment

To develop a function with Java and IntelliJ, install the following software:

The JAVA_HOME environment variable must be set to the install location of the JDK to complete the steps in this article.

3. Create an Azure Function project

  1. In IntelliJ IDEA, select Create New Project.
  2. In the New Project window, select Maven from the left pane.
  3. Select the Create from archetype check box, and then select Add Archetype for the azure-functions-archetype.
  4. In the Add Archetype window, complete the fields as follows:
    • GroupId: com.microsoft.azure
    • ArtifactId: azure-functions-archetype
    • Version: Use the latest version from the central repository
  5. Select OK, and then select Next.
  6. Enter your details for the current project, and select Finish.

Maven creates the project files in a new folder with the same name as the ArtifactId value. The project's generated code is a simple HTTP-triggered function that echoes the body of the triggering HTTP request.

create new Kotlin project

4. Specify the Maven basic Elements

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 project

create new kotlin project

5. Confirm the Maven Project settings

Maven summary

6. Confirm Project Name and Location

create new kotlin project

The project structure will be created.

7. Enable Auto-Import

create new kotlin project

8. Azure Configuration

By default the pom.xml file is opened when the project is created.

You can set the following project properties in the pom.xml file

  1. Azure Function App Name
  2. 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

create new kotlin project

9. 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.

create new kotlin project

10. Convert the Java Azure Function to Kotlin file

Right mouse click the default Java Function named Function and select Convert Java File to Kotlin File. And voila, the Java file magically converts a Kotlin project.

create new kotlin project

The default Azure Function converted from Java will now look like beautiful Kotlin.

create new kotlin project

11. Configure the Project for Kotlin

From the Tools menu, select Kotlin, then Configure Kotlin in Project

create new kotlin project

12. Enable All Modules containing Kotlin files

create new kotlin project

13. 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.

create new kotlin project

14. Execute Azure Functions Run

From the Maven pop-out tab, expand Plugins, then run the 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-Java link

create new kotlin project

15. 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-Java?name=dave and you will see the webpage echos the value passed in for name.

create new kotlin project

16. Stop the function

Click the stop icon to stop the function from running.

create new kotlin project

17. Enable Debugging

Right mouse click on the azure-functions:run Maven Archetype and select Create 'glovebox-function [...

create new kotlin project

18. Create Run/Debug Configuration

Add -DenableDebug to the command line.

create new kotlin project

19. Enable Java Debugger Attach

From Run menu, select Edit Configuration

create new kotlin project

20. Add New Configuration

Click the + sign, then select Remote.

create new kotlin project

21. Name the New Configuration

In this case the configuration is named Attach Debugger

create new kotlin project

22. Set a breakpoint in the Kotlin Azure Function source

create new kotlin project

23. 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.

create new kotlin project

24. 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.

create new kotlin project

25. Initiate the Http Trigger in Debug Mode

Click the http://localhost:7071/api/HttpTrigger-Java link to initiate the Http Trigger.

create new kotlin project

26. Step through the Http Trigger Azure Function with the Debugger

Using the debugger controls, step through the Azure Function code.

kotlin debugging

27. Stop the Debugger and Azure Function

Click the Stop icon to detach the debugger and stop the Azure Function

create new kotlin project

28. Adding new Azure Function Triggers

azure-functions:add

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

Azure functions add

29. Deploying Kotlin Azure Functions to Azure

azure-functions:deploy

To deploy the staging directory to target Azure Functions. If target Azure Functions does not exist already, it will be created.

Azure functions deploy

30. Building an End to End Secure, Scalable IoT Serverless Solution

The next article will be about creating an end to end IoT Serverless solution with Kotlin Azure Functions and Azure SignalR.

end to end solution

Posted on Apr 8 '19 by:

Microsoft Azure

Any language. Any platform.

Discussion

markdown guide
 
 

Thank you! There's another Kotlin post coming in a day or so too:)