😣Serverless Overviews from a Solution Architect Perspective

What’s serverless?

β€’ Serverless is a new paradigm in which the developers don’t have to manage servers anymore…

β€’ They just deploy code

β€’ They just deploy… functions !

β€’ Initially... Serverless == FaaS (Function as a Service)

β€’ Serverless was pioneered by AWS Lambda but now also includes anything that’s managed: β€œdatabases, messaging, storage, etc.”

β€’ Serverless does not mean there are no servers… it means you just don’t manage / provision / see them

Serverless in AWS

β€’ AWS Lambda

β€’ DynamoDB

β€’ AWS Cognito

β€’ AWS API Gateway

β€’ Amazon S3

β€’ AWS SNS & SQS

β€’ AWS Kinesis Data Firehose

β€’ Aurora Serverless

β€’ Step Functions

β€’ Fargate

Why AWS Lambda

EC2

β€’ Virtual Servers in the Cloud

β€’ Limited by RAM and CPU

β€’ Continuously running

β€’ Scaling means intervention to add / remove servers

LAMBDA

β€’ Virtual functions – no servers to manage!

β€’ Limited by time - short executions

β€’ Run on-demand

β€’ Scaling is automated!

Benefits of AWS Lambda

β€’ Easy Pricing:

β€’ Pay per request and compute time

β€’ Free tier of 1,000,000 AWS Lambda requests and 400,000 GBs of compute time

β€’ Integrated with the whole AWS suite of services

β€’ Integrated with many programming languages

β€’ Easy monitoring through AWS CloudWatch

β€’ Easy to get more resources per functions (up to 10GB of RAM!)

β€’ Increasing RAM will also improve CPU and network!

AWS Lambda language support

β€’ Node.js (JavaScript)

β€’ Python

β€’ Java (Java 8 compatible)

β€’ C# (.NET Core)

β€’ Golang

β€’ C# / Powershell

β€’ Ruby

β€’ Custom Runtime API (community supported, example Rust)

β€’ Lambda Container Image

β€’ The container image must implement the Lambda Runtime API

β€’ ECS / Fargate is preferred for running arbitrary Docker

AWS Lambda Integrations Main ones

Example: Serverless Thumbnail creation

Example: Serverless CRON Job

AWS Lambda Pricing: example

β€’ You can find overall pricing information here: https://aws.amazon.com/lambda/pricing/

β€’ Pay per calls :

β€’ First 1,000,000 requests are free

β€’ $0.20 per 1 million requests thereafter ($0.0000002 per request)

β€’ Pay per duration: (in increment of 1 ms )

β€’ 400,000 GB -seconds of compute time per month for FREE

β€’ == 400,000 seconds if function is 1GB RAM

β€’ == 3,200,000 seconds if function is 128 MB RAM

β€’ After that $1.00 for 600,000 GB-seconds

β€’ It is usually very cheap to run AWS Lambda so it’s very popular

AWS Lambda Limits to Know - per region

β€’ Execution:

β€’ Memory allocation: 128 MB – 10GB (1 MB increments)

β€’ Maximum execution time: 900 seconds (15 minutes)

β€’ Environment variables (4 KB)

β€’ Disk capacity in the β€œfunction container” (in /tmp): 512 MB

β€’ Concurrency executions: 1000 (can be increased)

β€’ Deployment:

β€’ Lambda function deployment size (compressed .zip): 50 MB

β€’ Size of uncompressed deployment (code + dependencies): 250 MB

β€’ Can use the /tmp directory to load other files at startup

β€’ Size of environment variables: 4 KB

Lambda@Edge

β€’ You have deployed a CDN using CloudFront

β€’ What if you wanted to run a global AWS Lambda alongside?

β€’ Or how to implement request filtering before reaching your application?

β€’ For this, you can use Lambda@Edge: deploy Lambda functions alongside your CloudFront CDN

β€’ Build more responsive applications

β€’ You don’t manage servers, Lambda is deployed globally

β€’ Customize the CDN content

β€’ Pay only for what you use

Lambda@Edge

β€’ You can use Lambda to change CloudFront requests and responses:

β€’ After CloudFront receives a request from a viewer (viewer request)

β€’ Before CloudFront forwards the request to the origin (origin request)

β€’ After CloudFront receives the response from the origin (origin response)

β€’ Before CloudFront forwards the response to the viewer (viewer response)

β€’ You can also generate responses to viewers without ever sending the request to the origin

Lambda@Edge: Global application

Lambda@Edge: Use Cases

β€’ Website Security and Privacy

β€’ Dynamic Web Application at the Edge

β€’ Search Engine Optimization (SEO)

β€’ Intelligently Route Across Origins and Data Centers

β€’ Bot Mitigation at the Edge

β€’ Real-time Image Transformation

β€’ A/B Testing

β€’ User Authentication and Authorization

β€’ User Prioritization

β€’ User Tracking and Analytic

Amazon DynamoDB

β€’ Fully managed, highly available with replication across multiple AZs

β€’ NoSQL database - not a relational database

β€’ Scales to massive workloads, distributed database

β€’ Millions of requests per seconds, trillions of row, 100s of TB of storage

β€’ Fast and consistent in performance (low latency on retrieval)

β€’ Integrated with IAM for security, authorization and administration

β€’ Enables event driven programming with DynamoDB Streams

β€’ Low cost and auto-scaling capabilities

β€’ Standard & Infrequent Access (IA) Table Class

DynamoDB - Basics

β€’ DynamoDB is made of Tables

β€’ Each table has a Primary Key (must be decided at creation time)

β€’ Each table can have an infinite number of items (= rows)

β€’ Each item has attributes (can be added over time – can be null)

β€’ Maximum size of an item is 400KB

β€’ Data types supported are:

β€’ Scalar Types – String, Number, Binary, Boolean, Null

β€’ Document Types – List, Map

β€’ Set Types – String Set, Number Set, Binary Set

DynamoDB –Table example

DynamoDB – Read/Write Capacity Modes

β€’ Control how you manage your table’s capacity (read/write throughput)

β€’ Provisioned Mode (default)

β€’ You specify the number of reads/writes per second

β€’ You need to plan capacity beforehand

β€’ Pay for provisioned Read Capacity Units (RCU) & Write Capacity Units (WCU)

β€’ Possibility to add auto-scaling mode for RCU & WCU

β€’ On-Demand Mode

β€’ Read/writes automatically scale up/down with your workloads

β€’ No capacity planning needed

β€’ Pay for what you use, more expensive ($$$)

β€’ Great for unpredictable workloads

DynamoDB Accelerator (DAX)

β€’ Fully-managed, highly available, seamless inmemory cache for DynamoDB

β€’ Help solve read congestion by caching

β€’ Microseconds latency for cached data

β€’ Doesn’t require application logic modification (compatible with existing DynamoDB APIs)

β€’ 5 minutes TTL for cache (default)

DynamoDB Accelerator (DAX) vs. ElastiCache

DynamoDB Streams

β€’ Ordered stream of item-level modifications (create/update/delete) in a table

β€’ Stream records can be:

β€’ Sent to Kinesis Data Streams

β€’ Read by AWS Lambda

β€’ Read by Kinesis Client Library applications

β€’ Data Retention for up to 24 hours

β€’ Use cases:

β€’ react to changes in real-time (welcome email to users)

β€’ Analytics

β€’ Insert into derivative tables

β€’ Insert into ElasticSearch

β€’ Implement cross-region replication

DynamoDB Global Tables

β€’ Make a DynamoDB table accessible with low latency in multiple-regions

β€’ Active-Active replication

β€’ Applications can READ and WRITE to the table in any region

β€’ Must enable DynamoDB Streams as a pre-requisite

A Transaction is written to both tables, or none!

Example: Building a Serverless API

AWS API Gateway

β€’ AWS Lambda + API Gateway: No infrastructure to manage

β€’ Support for the WebSocket Protocol

β€’ Handle API versioning (v1, v2…)

β€’ Handle different environments (dev, test, prod…)

β€’ Handle security (Authentication and Authorization)

β€’ Create API keys, handle request throttling

β€’ Swagger / Open API import to quickly define APIs

β€’ Transform and validate requests and responses

β€’ Generate SDK and API specifications

β€’ Cache API responses

API Gateway – Integrations High Level

β€’ Lambda Function

β€’ Invoke Lambda function

β€’ Easy way to expose REST API backed by AWS Lambda

β€’ HTTP

β€’ Expose HTTP endpoints in the backend

β€’ Example: internal HTTP API on premise, Application Load Balancer…

β€’ Why? Add rate limiting, caching, user authentications, API keys, etc…

β€’ AWS Service

β€’ Expose any AWS API through the API Gateway?

β€’ Example: start an AWS Step Function workflow, post a message to SQS

β€’ Why? Add authentication, deploy publicly, rate control…

API Gateway - Endpoint Types

β€’ Edge-Optimized (default): For global clients

β€’ Requests are routed through the CloudFront Edge locations (improves latency)

β€’ The API Gateway still lives in only one region

β€’ Regional:

β€’ For clients within the same region

β€’ Could manually combine with CloudFront (more control over the caching strategies and the distribution)

β€’ Private:

β€’ Can only be accessed from your VPC using an interface VPC endpoint (ENI)

β€’ Use a resource policy to define access

API Gateway – Security IAM Permissions

β€’ Create an IAM policy authorization and attach to User / Role

β€’ API Gateway verifies IAM permissions passed by the calling application

β€’ Good to provide access within your own infrastructure

β€’ Leverages β€œSig v4” capability where IAM credential are in headers

API Gateway – Security

Lambda Authorizer (formerly Custom Authorizers)

β€’ Uses AWS Lambda to validate the token in header being passed

β€’ Option to cache result of authentication

β€’ Helps to use OAuth / SAML / 3rd party type of authentication

β€’ Lambda must return an IAM policy for the user

API Gateway – Security Cognito User Pools

β€’ Cognito fully manages user lifecycle

β€’ API gateway verifies identity automatically from AWS Cognito

β€’ No custom implementation required

β€’ Cognito only helps with authentication, not authorization

API Gateway – Security – Summary

β€’ IAM:

β€’ Great for users / roles already within your AWS account

β€’ Handle authentication + authorization

β€’ Leverages Sig v4

β€’ Custom Authorizer:

β€’ Great for 3rd party tokens

β€’ Very flexible in terms of what IAM policy is returned

β€’ Handle Authentication + Authorization

β€’ Pay per Lambda invocation

β€’ Cognito User Pool:

β€’ You manage your own user pool (can be backed by Facebook, Google login etc…)

β€’ No need to write any custom code

β€’ Must implement authorization in the backend

AWS Cognito

β€’ We want to give our users an identity so that they can interact with our application.

β€’ Cognito User Pools:

β€’ Sign in functionality for app users

β€’ Integrate with API Gateway

β€’ Cognito Identity Pools (Federated Identity):

β€’ Provide AWS credentials to users so they can access AWS resources directly

β€’ Integrate with Cognito User Pools as an identity provider

β€’ Cognito Sync:

β€’ Synchronize data from device to Cognito.

β€’ May be deprecated and replaced by AppSync

AWS Cognito User Pools (CUP)

β€’ Create a serverless database of user for your mobile apps

β€’ Simple login: Username (or email) / password combination

β€’ Possibility to verify emails / phone numbers and add MFA

β€’ Can enable Federated Identities (Facebook, Google, SAML…)

β€’ Sends back a JSON Web Tokens (JWT)

β€’ Can be integrated with API Gateway for authentication

AWS Cognito – Federated Identity Pools

β€’ Goal:

β€’ Provide direct access to AWS Resources from the Client Side

β€’ How:

β€’ Log in to federated identity provider – or remain anonymous

β€’ Get temporary AWS credentials back from the Federated Identity Pool

β€’ These credentials come with a pre-defined IAM policy stating their permissions

β€’ Example:

β€’ provide (temporary) access to write to S3 bucket using Facebook Login

AWS Cognito Sync

β€’ Deprecated – use AWS AppSync now

β€’ Store preferences, configuration, state of app

β€’ Cross device synchronization (any platform – iOS, Android, etc…)

β€’ Offline capability (synchronization when back online)

β€’ Requires Federated Identity Pool in Cognito (not User Pool)

β€’ Store data in datasets (up to 1MB)

β€’ Up to 20 datasets to synchronise

AWS SAM - Serverless Application Model

β€’ SAM = Serverless Application Model

β€’ Framework for developing and deploying serverless applications

β€’ All the configuration is YAML code

β€’ Lambda Functions

β€’ DynamoDB tables

β€’ API Gateway

β€’ Cognito User Pools

β€’ SAM can help you to run Lambda, API Gateway, DynamoDB locally

β€’ SAM can use CodeDeploy to deploy Lambda functions

Last updated