Azure Functions using Java Spring with CI/CD — Part 1

Visweshwar Ganesh
Sep 30, 2019 · 5 min read

Over the past few days I’ve been exploring using Azure functions, evaluating the feature set , CI / CD pipeline capabilities. In short, it checked all the boxes for a basic application.

This post will cover the following:

  • Setting up a local environment for testing
  • Deploying a Simple Function To Azure
  • Using Spring Boot for Functions
  • Enabling Azure CI/CD with github and Azure Pipelines

This Post is NOT intended to be

  • A production deployment strategy
  • A reference implementation for code

This Post is going to be Divided into Two Parts

  1. Local Setup, getting functions running in Azure
  2. Setting Up Spring Boot, Github and Pipelines

Prerequisites for following along

  • JDK 8+ environment
  • Basic Knowledge of a Java build system (Maven)
  • Azure Subscription (Even Trial would suffice)
  • Github account (Thanks to GitHub & Microsoft + Open Source you can create a free private repos)

Local Environment Setup

We need to install certain libraries.

$ mvn -v
Apache Maven 3.5.0
$ az --version
azure-cli 2.0.74
$ func -v
2.7.1633

Using an mvn archetype for our starter code

You can go about this two ways:

  • Command Line
  • IDE (I’m going to use IntelliJ for this Post)

Using Command Line to Generate the Project

Go to a new Directory where you plan to work from

Bash (Linux / Mac)mvn archetype:generate \     
-DarchetypeGroupId=com.microsoft.azure \ -DarchetypeArtifactId=azure-functions-archetype
Windows (Cmd)mvn archetype:generate ^
"-DarchetypeGroupId=com.microsoft.azure" ^
"-DarchetypeArtifactId=azure-functions-archetype"

Below are the values to fill. Note appName has to be unique across Azure

Define value for property 'groupId' (should match expression '[A-Za-z0-9_\-\.]+'): com.example.functions 
Define value for property 'artifactId' (should match expression '[A-Za-z0-9_\-\.]+'): example-functions
Define value for property 'version' 1.0-SNAPSHOT :
Define value for property 'package': com.example.functions
Define value for property 'appName' example-functions-20190927220323382:
Define value for property 'appRegion' eastus: :
Define value for property 'resourceGroup' java-functions-group: : Confirm properties configuration: Y

Using IntelliJ IDE to achieve the same

Add the Azure Archetype and create the Project

Add the Azure Functions archetype to IntelliJ

File Structure

You should have a file structure something similar to that below.

bash-3.2$ tree examplefunctions/
examplefunctions/
├── examplefunctions.iml Ignore this file, intellij File
├── host.json This is the Azure Host property file
├── local.settings.json This is the file for local development
├── package-lock.json
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── example
│ └── functions
│ └── Function.java
└── test
└── java
└── com
└── example
└── functions
├── FunctionTest.java
└── HttpResponseMessageMock.java

Test Locally

  1. Package the project
  2. Modify the local-settings.json to choose your port (default 7071)
  3. Add this to the settings

"Host": {
"LocalHttpPort": 8091,
"CORS": "*",
"CORSCredentials": false
}

4. Run the project locally

You should see something like this in your console.

Now listening on: http://0.0.0.0:8091
Application started. Press Ctrl+C to shut down.
Http Functions:HttpTrigger-Java: [GET,POST] http://localhost:8091/api/HttpTrigger-Java

5. Test the server

bash-3.2$ curl -w "\n" http://localhost:8091/api/HttpTrigger-Java -d LocalFunction
Hello, LocalFunction

Concepts

What we’ve done so far is, we’ve downloaded and template and just ensured the template is working. The main activity happens in Function.java

The Annotation defines the name of the function which is unique to the project. There are different triggers which you can associate to on Azure (HttpTrigger being the one being shown below).

After we package the application, individual functions are exposed in the package with a manifest on how to trigger the jar. See below

Deploying to Azure

We will deploy to azure through the command line.

To begin with we have to login into Azure through the command line

bash-3.2$ az login
Note, we have launched a browser for you to login. For old experience with device code, use "az login --use-device-code"
You have logged in. Now let us find all the subscriptions to which you have access...

We will then have to initiate a deploy using maven

[INFO] Trying to deploy artifact to example-functions-20190929215243949...
[INFO] Successfully deployed the artifact to https://example-functions-20190929215243949.azurewebsites.net
[INFO] Successfully deployed the function app at https://example-functions-20190929215243949.azurewebsites.net

Navigate to https://portal.azure.com

You can then go to the Resource called Function App

This has been Successfully deployed. Let us retrieve the code to test this. You can capture the hosty key for master or create a new one.

To Test this we can use the published URL and curl at it.

curl -G https://example-functions-20190929215243949.azurewebsites.net/api/HttpTrigger-Java -d AzureFunction -d code=<code>

or You can use the browser

Part 2

Azure Functions using Java Spring with CI/CD using Azure Pipelines — Part 2 https://link.medium.com/sSPPLEVHr0

References

Azure Functions https://azure.microsoft.com/en-us/services/functions/
Azure Functions using Java https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven

Visweshwar Ganesh

Written by

Developer, Solution Lead @Paychex. Always look for opportunities to innovate and explore. Twitter: @visweshwar.LinkedIn: visweshwar-ganesh-5797405

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade