OpenGraph API – AWS Lambda

8 years ago I was introduced to Serverless functions on AWS. The concept of having the ability to run code, without worrying about running the underlying sever infrastructure was pretty appealing, not only from a management and operations perspective, but also from the associated 24/7 runtime costs.

Serverless computing enables you to focus on writing code, wiring it up to a API gateway or endpoint, and only paying when the code is executed. Some of the key functions or aspects which make this unique compared to a traditional server stack such as LEPP, MEAN, LAMP, etc. is that you have to consider the key underlying characteristics of a micro-service architecture:

  • Event driven execution: HTTP requests, DB changes or uploads or schedule timers.
  • Stateless: Each time the function is run, it is a new instance of the code runtime.
  • Scaling: Due to the micro-service architecture and stateless runtime, allows you to really easily scale the function based on demand.
  • Billing: Pay when it runs, not for the time you are waiting for a site visitor 🙂

Each of these characteristics require you to think differently about your code base, the resources it consumes and how decoupled each layer/component of the architecture is.

Implementation

One simple implementation of an rerverless function I implemented was for a messaging platform called Relay. A nice, and these days ubiquitous messaging feature, is that when you paste a link into a message box you get a pre-rendered image and text description of the pasted link. This gives you, and the receiver, a small preview of what to expect in the link.

Building a function like this is fairly trivial from a code perspective, the server receives a request for a URL, runs a query against the provided URL and returns the data to the client.

This is what the function looks like when configured in AWS Lambda.

https://gist.github.com/paschmann/4f2439617894a292f83c6533dc1628b8

Running a HTTP POST against the API endpoint provides a simple but helpful OpenGraph response with the website details, including a small image. Perfect to render a thumbnail snapshot.

Want to run your own endpoint? Use this tiny project to try out a lambda function

https://github.com/paschmann/aws-opengraphapi