DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Azure Functions: Process Events With a Serverless Code Architecture

Azure Functions: Process Events With a Serverless Code Architecture

This example project will give you a quick overview of Azure Functions' serverless capabilities, so you can run your code without caring where it's running from.

Ezequiel Reyno user avatar by
Ezequiel Reyno
·
Feb. 28, 17 · Tutorial
Like (4)
Save
Tweet
Share
4.81K Views

Join the DZone community and get the full member experience.

Join For Free

What’s up guys, Eze is here. During our last posts we were talking about Firebase, AngularJs, and a bunch of cool tools to create rapidly powerful front-end apps, in case you missed it you can check here. Today, I want to bring you some server side code that runs in a serverless architecture. Yes, we are going to talk about Azure Functions.

About Azure Functions

This is a cool feature that the Azure team has created for us. The idea is to mix events and code to execute in a serverless environment, which basically means you don't need to care about where your code is going to run, server configuration, how much memory you're using, or which OS version you're dealing with. You just need to define the code to execute and which event is going to trigger our function. As they say:

An event-based serverless compute experience to accelerate your development. It can scale based on demand and you pay only for the resources you consume.

Common Scenarios for Azure Functions

Trigger by time: Azure Functions uses CRON job syntax to specify an event based on a timer. So let's say that we want our code to run every 10 minutes.

Azure service event: We can set up our function to execute every time something happens in an Azure service. For example, when a new file is uploaded into blob storage.

Serverless web application architectures: This one in particular is the one I've used for our example. In this case, a front-end app calls a function to execute a procedure on the server side.

Function Components

As you can see in the image below, the Azure function is made up by two files: The first one is function.json, which represents the structure of our function — basically the metadata of our function. The second one is a run.csx file (because I'm using C#), which is the method we want our function to execute. Microsoft offers us a web editor to edit our functions directly in the browser, but if we want, we can configure a GitHub project and set up an automatic deploy when we push to our repository.

function.png

I have a working example in this link if you want to see the complete implementation. In this example, every time a user makes a request in our front-end app, we call this Azure Function to store the request in an Azure storage table, as you can see below:

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ICollector<Request> outTable, TraceWriter log)
{
  dynamic data = await req.Content.ReadAsAsync<object>();
  string name = data?.userName;

  if (name == null)
  {
  return req.CreateResponse(HttpStatusCode.BadRequest, "Please provide an user name in the request body");
  }

  outTable.Add(new Request()
              {
                PartitionKey = "Functions",
                RowKey = Guid.NewGuid().ToString(),
                cityName = data.cityName,
                convertedLocalTime = data.convertedLocalTime,
                convertedRequestedTime = data.convertedRequestedTime,
                dstOffset = data.dstOffset,
                rawOffset = data.rawOffset,
                timeAtLocation = data.timeAtLocation,
                timeZoneId = data.timeZoneId,
                timeZoneName = data.timeZoneName,
                userName = data.userName
              });
  return req.CreateResponse(HttpStatusCode.Created);
}


And there you have it! For more details on Azure Functions, you can use this link.

If you found this post useful, please don't forget to press the like button and share it. If you are in doubt, don't hesitate to ask a question and, as always, thank you for reading.

azure Event Architecture

Published at DZone with permission of Ezequiel Reyno. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • OpenID Connect Flows
  • Distributed SQL: An Alternative to Database Sharding
  • Iptables Basic Commands for Novice
  • Upgrade Guide To Spring Data Elasticsearch 5.0

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: