Serverless Architecture Patterns and Best Practices
Serverless Architecture Patterns and Best Practices
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Agenda
1. Serverless Key Concepts
2. Lambda Basics
3. Lambda Best Practices
4. Serverless Application Model
5. CI/CD using CodeStar
6. Monitoring
7. Event Processing
8. Real-time Streaming
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Serverless Key Concepts
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Serverless means…
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda Basics
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Using AWS Lambda
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda execution model
Synchronous (push) Asynchronous (event) Stream-based
Amazon Amazon Amazon
API Gateway SNS DynamoDB
Amazon Amazon
S3 Kinesis
/api/hello
reqs changes
AWS Lambda
service
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Anatomy of a Lambda function
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda Best Practices
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda Best Practices
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda Best Practices
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda Best Practices
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda Best Practices
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda Best Practices
https://github.com/jconning/lambda-cpu-cost
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda Best Practices
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Meet
SAM!
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Serverless Application Model
https://github.com/awslabs/serverless-application-model
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
SAM Template
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Simple CRUD webservice.
Resources:
GetFunction:
Type: AWS::Serverless::Function
Properties:
Handler: org.sample.aws.samlocal.BookGetHandler
Runtime: java8
CodeUri: ./target/sam-local-java-1.0-SNAPSHOT.jar
Policies: AmazonDynamoDBReadOnlyAccess
Timeout: 30
Environment:
Variables:
TABLE_NAME: !Ref Table
Events:
}
GetResource:
Type: Api
Properties:
Path: /books
Method: get
Table:
Type: AWS::Serverless::SimpleTable
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
SAM Commands
Package
Creates a deployment package (.zip file)
Uploads deployment package to an S3 bucket
Adds a CodeUri property with S3 URI
Deploy
Creates CloudFormation resources
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
SAM Local
• CLI for local testing of Serverless apps
• Works with Lambda functions and
“proxy style” APIs
• Response object and function logs
available on your local machine
• Currently supports Java, Node.js and
Python
• Accepting PRs
https://github.com/awslabs/aws-sam-local
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
CI/CD using AWS CodeStar, AWS
CodeBuild and AWS CodePipeline
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Monitoring
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS X-Ray Integration with Serverless
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
X-Ray Trace Example
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Event Processing
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Event driven
Event A on B triggers C A B C
Invocation Action
Lambda functions
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Event-driven platform
Such as…
Redshift Kinesis S3
Custom CloudWatch
events events
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Event-driven actions
Triggered on
PUTs
Users upload photos
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Step Functions:
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Real-time Streaming
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
https://aws.amazon.com/solutions/case-studies/supercell/
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon Kinesis makes it easy to work with real-time
streaming data
Producers Consumers
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Processing a Kinesis Streams with AWS Lambda
Kinesis Stream
Gets Records
1x per sec
10k records
...
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Kinesis Analytics
Use SQL to build real-time applications
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Real-time Analytics Demo
http://quad.adhorn.me
Real-time analytics
Amazon
Cognito
Amazon S3
Further Reading
Serverless Architectures with AWS Lambda
https://d1.awsstatic.com/whitepapers/serverless-architectures-with-aws-lambda.pdf
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
More info:
https://aws.amazon.com/serverless/
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Thank you!
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.