Serverless Compute

Monolithic Architecture

In monolithic architecture, all system functions exist in one codebase and on a single host providing compute, storage, database and networking functionalities.

Microservice Architecture

A microservices architecture is the inverse of a monolithic architecture. Instead of having all system functions in one codebase, components are separated into microservices and operate independently. A microservice does one thing - and does it well. Operations, updates, and scaling can be done on a per-microservice basis.

Inflexible scaling: Either increasing the instance size or duplicating the instance.

Microservices operate as independent applications. They allow direct communication between components and the end user. If one part of the system requires more capacity, that service can be scaled and updated as needed.

API - Application Programming Interface

We access the AWS console via a user interface, which is designed for a human being. An API is an interface accessed by another service or application. Major types of APIs include REST (JSON) and SOAP (XML)

API Endpoint

An API endpoint hosts one or more APIs and makes them available on a network (private or public internet). APIs remain static - they are abstracted from what the code inside the service is doing. API consumers do not care how things are done. For consumers, the interface should be working. The AWS CLI tools use the AWS APIs.

Event Driven Architecture

In traditional architecture, resources constantly poll the servers causing the system to overload. For example, constant polling of home environmental sensors will result in high resource usage of smart home server and its network.

However, when using an event-driven architecture, a system operates around "events" that represent an action or a change of state - e.g. a button being clicked, a file being uploaded or a temperature dropping below a certain level. It is efficient because events are generated and pushed, rather than things being polled. Polling requires always on compute and does not scale well.

Serverless Architecture

It consists of two main principles, including BaaS (Backend as a Service) which means using third-party services where possible rather than running our own. Examples include Auth0 or Cognito for authentication and Firebase or DynamoDB for data storage.

Serverless also means using an event-driven architecture where possible using FaaS (Function as a Service) products to provide application logic. These functions are only invoked when they are needed (i.e. when an event is received).

Lambda Essentials

Lambda is a FaaS product. Functions are code, which run in a runtime. Functions are invoked by events, perform actions for up to 15 minutes and terminate. Functions are also stateless - each run is clean. Functions can consume internet API endpoints or other services. Functions can be allowed access to a VPC - allowing private resource access. Access to AWS services is provided by the function's execution role. This role is assumed by Lambda and temporary security credentials are available to the function via STS. Lambda memory limitation is between 128 Mbps to a maximum of 3008 Mbps and a run-time of maximum 15 minutes.

API Gateway

API gateway is a managed API endpoint service. It can be used to create, publish, monitor and secure "APIs as a Service". API Gateway can use other AWS services for compute (FaaS/IaaS) as well as to store and recall data.

There are three types of service models accessed by API Gateway.

  1. Monolith: API Gateway can access AWS services (EC2 to access EBS) directly using proxy mode.

  2. Microservices: API Gateway can also access microservices such as Amazon Fargate (container compute based service) to access Amazon Aurora.

  3. Serverless: API Gateway can also access serverless and FaaS based architecture such as AWS Lambda to access Amazon DynamoDB. Also note that APIs can also directly access DynamoDB without Lambda.

APIs can be migrated to API gateway in a monolithic form and gradually moved to a micro services architecture and then once components have been fully broken up a serverless and FaaS based architecture.

Pricing is based on the number of API calls, the data transferred and any caching required to improve performance.

With API gateway, we can create two forms of APIs. REST and Websocket.

REST: Rest uses request and response style exchange and it is stateless. All requests are considered as new connections.

Websocket: A connection architecture is considered. When websocket is used, a connection is opened and remains active during data transfer between end consumer and API gateway. This reduces the number of API requests and hence reduces load from API Gateway.

We need to enable API Gateway CORS (Cross Origin Resource Sharing) for using APIs across multiple domains.

As an example, in API Gateway, we select REST, then we create resource, then create a GET method in Action which is in resource. Then we select the integration type from Lambda Function OR HTTP OR Mock OR AWS Service OR VPC Link. We pick Lambda here. Then we map the operands between API and Lambda and test. Once test is successful, we can deploy this API. Once API is deployed, an invoke URL is generated and visible. Postman is an application that interacts with APIs.

Step Functions

Step Functions is a serverless visual workflow service that provides state machines. A state machine can orchestrate other AWS services with simple logic, branching and parallel execution and it maintains a state. Workflow steps are known as states and they can perform work via tasks. Step Functions allows for long-running serverless workflows. A state machine can be defined using Amazon States Language (ASL). State machine is executed from another service or component.

Without step functions, Lambda functions could only run for 15 minutes. Lambda functions are stateless. State machines maintain state and allow longer-running processes. Step Functions "replaces" Simple workflow service (SWF) with a serverless version.

Next: Container-Based Compute and Microservices