Exploring Azure Functions

Awhile ago I was asked to write some demo service code. Two of the suggested technologies were Azure Functions and Azure Cosmos DB. The experience prompted me to finally setup a blog and commit to exploring technologies that interest me. So my plan is to start exploring Azure Functions using a demo project that I have started in my github account.

The Project

The project I will be working on is a trip logger service. It is named generically but it is intended for logging bicycling trips. I am an avid cyclist and track the miles I bike for commuting and fun. I have been logging the data just in Excel but I have several years worth of data that I can eventually use as the project expands.

The initial setup uses the Azure Functions v3 library and is written in C#. I have only created a single function that triggers on a http POST request and saves the submitted request to a Cosmos DB collection. There is minimal validation and no security at this point. There will be a lot of future post that I will be able to write to explore validation, security, and I’m sure a lot more.

Create Trigger/Binding

The first Azure function in the TripLoggerServices project is for creating new ‘trips’ within Cosmos DB. The function takes advantage of the trigger/binding attributes for input via a HttpTrigger attribute and persistence via a CosmosDB attribute.

public static class LogTripRequest
{
	[FunctionName("LogTripRequest")]
	public static IActionResult Run(
		[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "trips")] HttpRequest req,
		[CosmosDB(
			databaseName: "TripLog",
			collectionName: "Trips",
			CreateIfNotExists = true,
			ConnectionStringSetting = "CosmosDBConnection")]out TripEntry tripEntry,
		ILogger log)
	{
	}
}

In the code sample above the FunctionName attribute marks the method as an Azure function entry point.

HttpTrigger

The first parameter to the http trigger sets the authorization level. For now the function is set to AuthorizationLevel. Anonymous to simplify development. Eventually I will explore security with functions.

Since this function is for creating trips the http trigger is configured to respond to http POST requests using the second parameter.

The Route parameter sets the relative url path for the function when it is hosted.
Note: The host.json file is setup so there is no prefix for the url.

{
  "version": "2.0",
  "extensions": {
    "http": {
      "routePrefix": ""
    }
  }
}

Using this trigger setup POST requests will trigger the function and the http request details will be contained in the req parameter.

Cosmos Binding

To persist data for a valid request the CosmosDB binding attribute is used. The databaseName and collectionName help identify the database and collection within the database where you want to persist your entities.

The CreateIfNotExists is set to true for development but in production you should create/configure your Cosmos DB database/collection outside the function.

The ConnectionStringSetting is used to specify the configuration variable that contains the connection string to the Cosmos DB subscription. The local.settings.json contains the default connection string to the Cosmos DB emulator for local development.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "CosmosDBConnection": "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
  }
}

The tripEntry parameter is an out parameter since it is only being used to create new entries in Cosmos DB if the function execution is successful. The CosmosDB binding knows how to save the object instance assigned to the tripEntry parameter.

Leave a comment