Chris Franklin
Chris Franklin

Categories

  • serverless

Most applications will require a way to store data that persists between sessions. When the user starts the application, we need a way to retrieve the latest data. The way the client interacts with the application is called the Application Programming Interface, or API.

To build an API, we can use two services provided by Amazon Web Services, AWS Lambda and API Gateway.

AWS Lambda

Lambda was the first mainstream Function-as-a-Service (FaaS) platform. FaaS allows developers to build small pieces of functionality without having to worry about setting up infrastructure or scaling to meet demand.

In jargon speak, AWS Lambda is a managed, stateless, compute container. In layman terms, this means AWS Lambda only runs your code when an event it’s tied to fires. The events can be anything from a request from a web page to a new file being stored in Amazons S3 file storage.

At the time of publication there are 5 languages supported for writing AWS Lambda functions: Java, JavaScript (NodeJS), Python, C#, and Go. We can use nearly any library written in these languages within our lambda function. The only caveat is that we can’t store state between executions of the lambda function, so we can’t write anything to the disk and expect it to be available when the function is called again.

Lambda functions are charged on a GB-s (Gigabit-seconds). This means it’s based on the amount of memory times the number of seconds it ran. The expectation is that each lambda will only execute for a few hundred milliseconds at most. It’s possible to let one execute for multiple seconds, but this starts to increase cost significantly, especially if it can be kicked off multiple times simultaneously.

API Gateway

Lambda functions are great on their own. They can be registered to respond to a wide variety of events internal to AWS. However, we also need a way to trigger them from our client applications. This is where the API Gateway comes in.

API Gateway is an AWS service that exposes a REST style API to the world. With it, we can accept requests from any client that knows how to send an HTTP request. It’s essentially the ‘Front-door’ of our application.

We can use API Gateway to interact with our Lambdas. But, we could also use it as the gatekeeper for more traditional web services running on EC2. API Gateway allows us to control access through authentication and tokens so we can lock down our application security.

One other benefit to using API Gateway is automated SDK generation. We can generate an SDK for javascript, iOS, and Android with the push of a button. This allows all communication to our backend to be done through a client native interface instead of dealing directly with REST requests.

Conclusion

We can build fully featured and robust APIs using these two technologies. We don’t need to host a large fleet of servers to expose our application to the world. Instead we can use Serverless to allow us to scale at whatever pace we need to.