Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda...

52
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Sascha Möllering, Solutions Architect October 2018 Serverless Architectural Patterns and Best Practices

Transcript of Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda...

Page 1: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Sascha Möllering, Solutions Architect

October 2018

Serverless Architectural

Patterns and Best Practices

Page 2: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

About me

Sascha Möllering

Solutions Architect

Amazon Web Services EMEA SARL

• 16 years of dev, software architecture, and systems architecture

background

• Has written a lot of Java code.

• Enjoys containers and serverless. All day.

Twitter: @sascha242

Email: [email protected]

Page 3: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Agenda

• Microservices

• Serverless Foundations

• Web application

• Data Lake

• Stream processing

Page 4: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Microservices

Page 5: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Characteristics of Microservice Architectures

Do one

thing wellIndependent

Decentralized

Black box

Polyglot

You build it, you run it

Page 6: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Microservices should be stateless.

Keep state in external systems.

Page 7: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

No shared libraries or

shared SDKs.

Page 8: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Avoid

Host-Affinity.

Page 9: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Use mechanisms for

registration.

Page 10: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Use lightweight

protocols for

communication.

Page 11: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Serverless Foundations

Page 12: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

AWS

Lambda

Amazon

Kinesis

Amazon

S3

Amazon API

Gateway

Amazon

SQS

Amazon

DynamoDB

AWS IoT

Amazon

EMR

Amazon

ElastiCache

Amazon

RDS

Amazon

Redshift

Amazon ES

Managed Serverless

Amazon EC2

Microsoft SQL

Server

“On EC2”

Amazon

Cognito

Amazon

CloudWatch

Spectrum of AWS offerings

Page 13: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Serverless means…

• No servers to provision or manage

• Scales with usage

• Never pay for idle

• Built-in High-Availability and Disaster Recovery

Page 14: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

SERVICES (ANYTHING)

Changes in

data state

Requests to

endpoints

Changes in

resource state

EVENT SOURCE FUNCTION

Node.js

Python

Java

C#

Go

Serverless applications

Page 15: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Lambda considerations and best practices

AWS Lambda is stateless—architect accordingly

• Assume no affinity with underlying compute

infrastructure

• Local filesystem access and child process may not

extend beyond the lifetime of the Lambda request

Page 16: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Lambda considerations and best practices

Can your Lambda functions survive

the cold?

• Instantiate AWS clients and database

clients outside the scope of the handler

to take advantage of container re-use.

• Schedule with CloudWatch Events for

warmth

• ENIs for VPC support are attached

during cold start

import sys

import logging

import rds_config

import pymysql

rds_host = "rds-instance"

db_name = rds_config.db_name

try:

conn = pymysql.connect(

except:

logger.error("ERROR:

def handler(event, context):

with conn.cursor() as cur:

Executes with

each invocation

Executes during

cold start

Page 17: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Lambda Best Practices

• Minimize package size to necessities

• Separate the Lambda handler from core logic

• Use Environment Variables to modify operational

behavior

• Self-contain dependencies in your function package

• Leverage “Max Memory Used” to right-size your

functions

• Delete large unused functions (75GB limit)

Page 18: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

AWS X-Ray Integration with Serverless

• Lambda instruments incoming requests for all

supported languages

• Lambda runs the X-Ray daemon on all

languages with an SDK

var AWSXRay = require(‘aws-xray-sdk-core‘);

AWSXRay.middleware.setSamplingRules(‘sampling-rules.json’);

var AWS = AWSXRay.captureAWS(require(‘aws-sdk’));

S3Client = AWS.S3();

Page 19: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

AWS X-Ray Trace Example

Page 20: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Meet

SAM!

Page 21: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

AWS Serverless Application Model (SAM)

• CloudFormation extension optimized for

serverless

• New serverless resource types: functions,

APIs, and tables

• Supports anything CloudFormation

supports

• Open specification (Apache 2.0)

https://github.com/awslabs/serverless-application-model

Page 22: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

SAM Local

• Develop and test Lambda locally

• Invoke functions with mock

serverless events

• Local template validation

• Local API Gateway with hot

reloading

https://github.com/awslabs/aws-sam-local

Page 23: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Delivery via CodePipeline

Pipeline flow:

1. Commit code to source code repository

2. Package/test in CodeBuild

3. CloudFormation actions in CodePipeline to create or

update stacks via SAM templates

Optional: Make use of ChangeSets

4. Make use of specific stage/environment parameter

files to pass in Lambda variables

5. Test our application between stages/environments

Optional: Make use of manual approvals

Page 24: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

AWS CodeDeploy and Lambda Canary Deployments

• Direct a portion of traffic to

a new version

• Monitor stability with

CloudWatch

• Initiate rollback if needed

• Incorporate into your SAM

templates

Page 25: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Pattern 1: Web App/Microservice/API

Page 26: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Web application

Data stored in

Amazon

DynamoDB

Dynamic content

in AWS Lambda

Amazon API

Gateway

BrowserAmazon

CloudFront

Amazon

S3

Amazon Cognito

Page 27: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Amazon API

GatewayAWS

Lambda

Amazon

DynamoDB

Amazon

S3

Amazon

CloudFront

• Bucket Policies

• ACLs

• OAI

• Geo-Restriction

• Signed Cookies

• Signed URLs

• DDOS Protection

IAM

AuthZ

IAM

Serverless web app security

• Throttling

• Caching

• Usage Plans

• ACM

Browser

Amazon Cognito

Page 28: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Multi-Region with API Gateway

us-west-2

us-east-1

Client

Amazon

Route 53

Regional

API

Endpoint

Regional

API

Endpoint

Custom

Domain

Name

Custom

Domain

Name

API Gateway

API Gateway

Lambda

Lambda

Page 29: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Useful Frameworks for Serverless Web Apps

• AWS Chalice

Python Serverless Framework

https://github.com/aws/chalice

Familiar decorator-based api similar to Flask/Bottle

Similar to 3rd Party frameworks, Zappa or Claudia.js

• AWS Serverless ExpressRun Node.js Express apps

https://github.com/awslabs/aws-serverless-express

• Java - HttpServlet, Spring, Spark and Jerseyhttps://github.com/awslabs/aws-serverless-java-container

Page 30: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Pattern 2: Data Lake

Page 31: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Serverless Data Lake Characteristics

• Collect/Store/Process/Consume and Analyze all

organizational data

• Structured/Semi-Structured/Unstructured data

• AI/ML and BI/Analytical use cases

• Fast automated ingestion

• Schema on Read

• Complementary to EDW

• Decoupled Compute and Storage

Page 32: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

AWS Serverless Data Lake

S3

Bucket(s)

Key

Management

Service

Amazon

Athena

AWS

CloudTrail

Amazon

Cognito

AWS IAM

Amazon

Kinesis Data

Streams

Amazon

Kinesis Data

Firehose

Amazon ES

Amazon

QuickSight

AWS GlueAmazon

DynamoDB

Amazon

Macie

Amazon API

GatewayAWS IAM

Amazon

Redshift

Spectrum

AWS

Direct

Connect

Ingest

Catalog & Search

Security & Auditing

API/UI

Analytics & Processing

AWS GlueAWS

Lambda

Page 33: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

The Foundation…S3

• No need to run compute clusters for storage

• Virtually unlimited number of objects and volume

• Very high bandwidth – no aggregate throughput

limit

• Multiple storage classes

• Versioning

• Encryption

Page 34: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Search and Data Catalog

• DynamoDB as Metadata

repository

• Amazon Elasticsearch Service

Catalog & Search

AWS Lambda

AWS Lambda

Metadata Index

(DynamoDB)

Search Index

(Amazon ES)

ObjectCreated

ObjectDeleted PutItem

Update Stream

Update Index

Extract Search Fields

S3 Bucket

https://aws.amazon.com/answers/big-data/data-lake-solution/

Page 35: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

AWS Glue

CrawlersAWS Glue

Data Catalog

Amazon

QuickSight

Amazon

Redshift

Spectrum

Amazon

Athena

S3

Bucket(s)

Catalog & Search

Instantly query your data lake on Amazon S3

Page 36: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Analytics and Processing

• Amazon QuickSight

• Amazon Athena

• AWS Lambda

• Predictive Analytics

• Amazon EMR

• AWS Glue (ETL)

Analytics & Processing

Page 37: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Athena – Serverless Interactive Query Service

44.66 seconds...Data scanned: 169.53GB

Cost: $5/TB or $0.005/GB = $0.85

SELECT gram, year, sum(count) FROM ngram

WHERE gram = 'just say no'

GROUP BY gram,year ORDER BY year ASC;

Analytics & Processing

Page 38: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Athena – Best Practices

• Partition data

s3://bucket/flight/parquet/year=1991/month=1/day=2/

• Columnar formats – Apache Parquet, AVRO, ORC

• Optimize file sizes

• Compress files with splittable compression (bzip2)

https://aws.amazon.com/blogs/big-data/top-10-performance-tuning-tips-for-amazon-athena/

Analytics & Processing

Page 39: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Serverless batch processing

AWS Lambda:

Splitter

Amazon S3

Object

Amazon DynamoDB:

Mapper Results

AWS Lambda:

Mappers

….

….AWS Lambda:

ReducerAmazon S3

Results

Analytics & Processing

Page 40: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Pattern 3: Stream Processing

Page 41: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Stream processing characteristics

• High ingest rate

• Near real-time processing (low latency from ingest to

process)

• Spiky traffic (lots of devices with intermittent network

connections)

• Message durability

• Message ordering

Page 42: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Streaming data ingestion

Amazon CloudWatch:

Delivery metrics

Amazon S3:

Buffered files

Kinesis

Agent

Record

Producer

s Amazon Redshift:

Table loads

Amazon ElasticSearch Service:

Domain loads

Amazon S3:

Source record backup

AWS Lambda:

Transformations &

enrichment

Amazon DynamoDB:

Lookup tables

Raw records

Lookup

Transformed records

Transformed recordsRaw records

Amazon Kinesis Data Firehose:

Delivery stream

Page 43: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Best practices

• Tune Firehose buffer size and buffer interval

• Larger objects = fewer Lambda invocations, fewer S3 PUTs

• Enable compression to reduce storage costs

• Enable Source Record Backup for transformations

• Recover from transformation errors

• Follow Amazon Redshift Best Practices for Loading Data

Page 44: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Real-time analytics

Amazon Kinesis Data Streams:

Ingest stream

Amazon Kinesis Data Streams:

Aggregates stream

Amazon Kinesis Data Firehose:

Error stream

Amazon S3:

Error records

Record

Producer

s

Amazon DynamoDB:

Device thresholds

AWS SNS:

Notifications

Amazon Kinesis Analytics:

Time window aggregationAWS Lambda:

Alert function

Page 45: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Amazon Kinesis Analytics

CREATE OR REPLACE PUMP "STREAM_PUMP" AS INSERT INTO "DESTINATION_SQL_STREAM"

SELECT STREAM "device_id",

STEP("SOURCE_SQL_STREAM_001".ROWTIME BY INTERVAL '1' MINUTE) as "window_ts",

SUM("measurement") as "sample_sum",

COUNT(*) AS "sample_count"

FROM "SOURCE_SQL_STREAM_001"

GROUP BY "device_id",

STEP("SOURCE_SQL_STREAM_001".ROWTIME BY INTERVAL '1' MINUTE);

Aggregation1 minute tumbling window

Amazon Kinesis Analytics:

Time window aggregation

Source stream Destination stream

Page 46: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Real-time analytics

Amazon Kinesis Data Streams:

Ingest stream

Amazon Kinesis Data Streams:

Aggregates stream

Amazon Kinesis Data Firehose:

Error stream

Amazon S3:

Error records

Record

Producer

s

Amazon DynamoDB:

Device thresholds

AWS SNS:

Notifications

Amazon Kinesis Analytics:

Time window aggregationAWS Lambda:

Alert function

Page 47: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Amazon Kinesis Streams and AWS Lambda

• Number of Amazon Kinesis Streams shards corresponds to concurrent

invocations of Lambda function

• Batch size sets maximum number of records per Lambda function invocation

Amazon Kinesis:

Data Stream

AWS Lambda:

Processor function

Streaming source Other AWS services

Page 48: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Fan-out pattern

Fan-out pattern trades strict message ordering vs higher throughput & lower latency

Amazon Kinesis:

Data Stream

Lambda:

Dispatcher functionLambda:

Processor function

Increase throughput, reduce processing latency

Streaming source

Page 49: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Best practices

• Tune batch size when Lambda is triggered by Amazon Kinesis

Streams

• Higher batch size = fewer Lambda invocations

• Tune memory setting for your Lambda function

• Higher memory = shorter execution time

• Use Kinesis Producer Library (KPL) to batch messages and saturate

Amazon Kinesis Stream capacity

Page 50: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Go build something!

Amazon API

GatewayAWS Lambda Amazon

DynamoDB

Page 51: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Further Reading

Optimizing Enterprise Economics with Serverless Architectures

https://d0.awsstatic.com/whitepapers/optimizing-enterprise-economics-serverless-architectures.pdf

Serverless Architectures with AWS Lambda

https://d1.awsstatic.com/whitepapers/serverless-architectures-with-aws-lambda.pdf

Serverless Applications Lens - AWS Well-Architected Framework

https://d1.awsstatic.com/whitepapers/architecture/AWS-Serverless-Applications-Lens.pdf

Streaming Data Solutions on AWS with Amazon Kinesis

https://d1.awsstatic.com/whitepapers/whitepaper-streaming-data-solutions-on-aws-with-amazon-kinesis.pdf

AWS Serverless Multi-Tier Architectures

https://d1.awsstatic.com/whitepapers/AWS_Serverless_Multi-Tier_Archiectures.pdf

Page 52: Serverless Architectural Patterns and Best Practices · Amazon Kinesis Streams and AWS Lambda •Number of Amazon Kinesis Streams shards corresponds to concurrent invocations of Lambda

© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Thank you!