The HttpTrigger
makes it incredibly easy to have your functions executed via an HTTP call to your function. This sample, exposes a
function that can get call with an HTTP GET and it will return all Patients from Cosmos DB. It can be used to build a REST API that exposes
CRUD operations. You can take this a step further by leveraging APIM. The idea is to create a serverless micro-service CRUD endpoint using
Azure functions. This is a much faster way to create an API for backends than writing a Web App. If you need better performance just use the
Premimum tier.
When you call the function, be sure you checkout which security rules you apply. If you're using an apikey, you'll need to include that in your request. For this example, I simply use anonymous.
The files in the setup/FunctionApp folder are a functioning and were created directly in the Portal, not via VS Code, so the files that are needed for this are run.csx, function.json and readme.md.
Steps to get this working.
Setup Cosmos DB
- Create a Cosmos DB account
- Create a DB called PatientsDB
- Create a Collection called Patients-Containers
- Upload the setup/Cosmos-Seed-Data.JSON data into the Patients-Containters Items (this seeds Cosmos with sample data).
Create the HTTP Trigger Azure Function
- Navigate to portal.azure.com and create a new Azure Function
- Navigate to the Code + Test Blade and select the Run.csx file and replace the contents with the Run.csx file found in this gist.
- Now, select the function.json file and replace the contents of it with the file found in this gist and do the same for the readme.md
- Now, time to test it. If you have everything setup correctly, it will return all JSON documents from Cosmos that have a type = patient.
The result set wull look something like this:
[
{
"id": "1",
"Patient_Id": "1",
"type": "patient",
"name": "Steve Smith",
"phone": "704-555-1212",
"email": "steve.smith@contoso.com",
"country": "USA",
"_rid": "uatfAJuYoXYBAAAAAAAAAA==",
"_self": "dbs/uatfAA==/colls/uatfAJuYoXY=/docs/uatfAJuYoXYBAAAAAAAAAA==/",
"_etag": "\"0c0011ba-0000-0100-0000-63fa7f320000\"",
"_attachments": "attachments/",
"_ts": 1677360946
},
{
"id": "2",
"Patient_Id": "2",
"type": "patient",
"name": "David Smith",
"phone": "704-555-1212",
"email": "david.smith@contoso.com",
"country": "USA",
"_rid": "uatfAJuYoXYCAAAAAAAAAA==",
"_self": "dbs/uatfAA==/colls/uatfAJuYoXY=/docs/uatfAJuYoXYCAAAAAAAAAA==/",
"_etag": "\"0c0012ba-0000-0100-0000-63fa7f320000\"",
"_attachments": "attachments/",
"_ts": 1677360946
},