An introduction to AWS Lambda and the Serverless Framework
© Rowell Belen 1
About Me
Rowell Belen / Senior Platform Engineer @ Algorithmia
» LinkedIn: https://linkedin.com/in/rowellbelen
» Blog: https://www.rowellbelen.com
» GitHub: https://github.com/bytekast
» Twitter: @bytekast
» Email: rowell.belen@bytekast.com
© Rowell Belen 2
What is Serverless Computing?
Serverless Computing is a cloud computing model that allows
you to build and run applications and services without thinking
about servers. The platform takes care of everything required to
run and scale your code with high availability.
© Rowell Belen 3
The Sweet Spot
© Rowell Belen 4
But... but... aren't PaaS and Serverless the same?
© Rowell Belen 5
What is the key
difference between
PaaS and Serverless?
© Rowell Belen 6
ORCHESTRATION
&
MANAGEMENT
© Rowell Belen 7
and...
SCALING!!
© Rowell Belen 8
Unit of Scale
» Data Center: Hardware (Physical Hosting Env Abstraction)
» IaaS: Virtual Machines (Hardware Abstraction)
» Paas: Application (VM Abstraction)
» Serverless: Function (Runtime Abstraction)
© Rowell Belen 9
Wait.. wait... What about containers?
© Rowell Belen 10
What are the key
differences between
Containers and Serverless?
© Rowell Belen 11
ORCHESTRATION
&
MANAGEMENT
© Rowell Belen 12
and...
SCALING!!
© Rowell Belen 13
What is AWS Lambda?
Amazon's Serverless compute platform for stateless code
execution in response to events
© Rowell Belen 14
Other Serverless Platform Providers
» Microsoft Azure Functions
» Iron.io
» Google Cloud Functions
» IBM Open Whisk
» WebTask.io
» PubNub BLOCKS
© Rowell Belen 15
How Is AWS Lambda Used?
» Stream Data Processing
» REST Backend Services
» One-off Processes
» Background Workers
» Event Responders
© Rowell Belen 16
How are Lambda Functions Triggered?
» Event-driven (SNS, SQS, S3, API-Gateway, Amazon Echo
Skills, IoT, etc.)
» Direct Invocation (CLI, SDK, etc.)
» Scheduled Interval
© Rowell Belen 17
Supported Languages
» Node.js (Javascript)
» Python
» JVM ( Java, Scala, Groovy, Kotlin, Clojure, etc. )
» C#
» GoLang
© Rowell Belen 18
Pricing
Memory (MB) Free tier seconds per month Price per 100ms ($)
128 3,200,000 0.000000208
256 1,600,000 0.000000417
512 800,000 0.000000834
1024 400,000 0.000001667
1536 266,667 0.000002501
© Rowell Belen 19
Top Tier Pricing (1536 MB Memory)
1 Million Executions @ 1 sec/exec ≈ $18.34
© Rowell Belen 20
Benefits
» Cost and Utilization
» Fully Managed Infrastructure
» Rapid Development
» Streamlined AWS Integrations
» Pay Per Use
» Auto Scale
» Built-in Versioning
© Rowell Belen 21
Drawbacks
» Limited Language Support
» Not Suitable for Long-running Tasks
» Local Development and Debugging Challenges
» Limited Infrastructure Transparency / Less Control
» Potential Vendor Lock-in
» Cutting-edge quirks
» Concurrent Execution Limit is Shared across entire AWS
© Rowell Belen 22
Enough chit-chat,
let's see some code!
© Rowell Belen 23
Sample Function
def sendImage(Map httpEvent, Context context) {
try {
final request = new RequestContext().input(httpEvent).context(context)
final imageUrl = request.httpBody()
SqsUtil.instance.sendSQSMessage(inputQueueUrl, imageUrl)
new Response().statusCode(200).body("QUEUED: ${imageUrl}")
} catch (e) {
new Response().statusCode(500).body(e.message)
}
}
© Rowell Belen 24
What is the Serverless Framework?
Development toolkit for building, managing and deploying
Serverless applications and resources
© Rowell Belen 25
Serverless Framework CLI
npm install serverless -g
mkdir my-api && cd my-api
serverless create --template aws-groovy-gradle
serverless deploy --stage dev
serverless invoke --function my-function --log
© Rowell Belen 26
Available Templates
» aws-nodejs
» aws-python
» aws-groovy-gradle
» aws-java-maven
» aws-scala-sbt
» ...etc
© Rowell Belen 27
serverless.yml - Basic Function
service: serverless-demo
provider:
runtime: java8
timeout: 300
memorySize: 1536
package:
artifact: /build/dist/serverless-demo.zip
functions:
my-function:
handler: com.bytekast.serverless.MyLambdaFunction::handler
© Rowell Belen 28
serverless.yml - API Gateway
functions:
createUser:
handler: com.bytekast.serverless.UserService::createUser
events:
- http:
path: users/create
method: post
deleteUser:
handler: com.bytekast.serverless.UserService::deleteUser
events:
- http:
path: users/delete
method: delete
© Rowell Belen 29
serverless.yml - API Gateway (Custom Authorization)
...
events:
- http:
path: users/create
method: post
cors: true
authorizer:
arn: arn:aws:lambda:us-east-1:1234567890123:function:authorizer
identitySource: method.request.header.Authorization
identityValidationExpression: Bearer .*
© Rowell Belen 30
serverless.yml - SNS Topic Subscription
functions:
audit:
handler: com.bytekast.serverless.AuditService::audit
events:
- sns: dev-audit-topic
© Rowell Belen 31
serverless.yml - Scheduled Trigger
functions:
crawl:
handler: com.bytekast.serverless.SearchService::crawl
events:
- schedule: rate(2 hours)
- schedule: cron(0 12 * * ? *)
© Rowell Belen 32
serverless.yml - IAM Role Permissions
provider:
...
iamRoleStatements:
- Effect: "Allow"
Action:
- "sqs:*"
Resource: arn:aws:sqs:us-east-1:1234567890123:dev-serverless-demo
© Rowell Belen 33
serverless.yml - Create AWS Resources
resources:
Resources:
InboundQueue:
Type: "AWS::SQS::Queue"
Properties:
QueueName: ${self:provider.stage}-serverless-demo
MessageRetentionPeriod: 1209600
VisibilityTimeout: 60
© Rowell Belen 34
Sample Serverless ETL Pattern
© Rowell Belen 35
Sample Serverless Platform Architecture
© Rowell Belen 36
DEMO
Build Car Image Recognition Service using
Serverless Framework + API Gateway + AWS Lambda
Sample Project
© Rowell Belen 37
What about Cloud Scale
DevOps?
© Rowell Belen 38
Lambda Built-In Features
» Containers are ephemeral entities that are easily created and
destroyed
» Preserved history and ability to roll back a bad deployment
( revert to previous version )
» Auto-managed horizontal scaling (Scale out / Scale In)
» Automatic Load Balancing
» Zero/Minimal Downtime during deployment
© Rowell Belen 39
Environments should not be connected
or otherwise intercommunicate
© Rowell Belen 40
Solutions:
» AWS Lambda supports running functions in different VPCs
(dev, stage, prod)
» Use separate AWS accounts per Environment
© Rowell Belen 41
Promote immutable application
artifacts from the lowest environment
to the highest ( Deployment )
© Rowell Belen 42
Solutions:
» Publish Versioned Build Artifacts to Repository
» Continuous Integration
» Continuous Delivery
© Rowell Belen 43
Centralized Logging
© Rowell Belen 44
Metrics
» Better Dashboards
» Custom Metrics and Events
© Rowell Belen 45
Standardize Projects using
Templates
Custom Project Starter Kit
© Rowell Belen 46
Other Things to Consider
» AWS functions are recycled about every 5-8 hours
» Container instances idle for about 5 minutes are destroyed
» Cold starts can cause delay in response times
» 50 MB max deployment package size
» 5 minute running time limit / ~ 30 seconds if API Gateway
triggered
» 1000 default concurrent function executions across entire
AWS account
© Rowell Belen 47
Questions?
© Rowell Belen 48

Serverless Framework (2018)

  • 1.
    An introduction toAWS Lambda and the Serverless Framework © Rowell Belen 1
  • 2.
    About Me Rowell Belen/ Senior Platform Engineer @ Algorithmia » LinkedIn: https://linkedin.com/in/rowellbelen » Blog: https://www.rowellbelen.com » GitHub: https://github.com/bytekast » Twitter: @bytekast » Email: rowell.belen@bytekast.com © Rowell Belen 2
  • 3.
    What is ServerlessComputing? Serverless Computing is a cloud computing model that allows you to build and run applications and services without thinking about servers. The platform takes care of everything required to run and scale your code with high availability. © Rowell Belen 3
  • 4.
    The Sweet Spot ©Rowell Belen 4
  • 5.
    But... but... aren'tPaaS and Serverless the same? © Rowell Belen 5
  • 6.
    What is thekey difference between PaaS and Serverless? © Rowell Belen 6
  • 7.
  • 8.
  • 9.
    Unit of Scale »Data Center: Hardware (Physical Hosting Env Abstraction) » IaaS: Virtual Machines (Hardware Abstraction) » Paas: Application (VM Abstraction) » Serverless: Function (Runtime Abstraction) © Rowell Belen 9
  • 10.
    Wait.. wait... Whatabout containers? © Rowell Belen 10
  • 11.
    What are thekey differences between Containers and Serverless? © Rowell Belen 11
  • 12.
  • 13.
  • 14.
    What is AWSLambda? Amazon's Serverless compute platform for stateless code execution in response to events © Rowell Belen 14
  • 15.
    Other Serverless PlatformProviders » Microsoft Azure Functions » Iron.io » Google Cloud Functions » IBM Open Whisk » WebTask.io » PubNub BLOCKS © Rowell Belen 15
  • 16.
    How Is AWSLambda Used? » Stream Data Processing » REST Backend Services » One-off Processes » Background Workers » Event Responders © Rowell Belen 16
  • 17.
    How are LambdaFunctions Triggered? » Event-driven (SNS, SQS, S3, API-Gateway, Amazon Echo Skills, IoT, etc.) » Direct Invocation (CLI, SDK, etc.) » Scheduled Interval © Rowell Belen 17
  • 18.
    Supported Languages » Node.js(Javascript) » Python » JVM ( Java, Scala, Groovy, Kotlin, Clojure, etc. ) » C# » GoLang © Rowell Belen 18
  • 19.
    Pricing Memory (MB) Freetier seconds per month Price per 100ms ($) 128 3,200,000 0.000000208 256 1,600,000 0.000000417 512 800,000 0.000000834 1024 400,000 0.000001667 1536 266,667 0.000002501 © Rowell Belen 19
  • 20.
    Top Tier Pricing(1536 MB Memory) 1 Million Executions @ 1 sec/exec ≈ $18.34 © Rowell Belen 20
  • 21.
    Benefits » Cost andUtilization » Fully Managed Infrastructure » Rapid Development » Streamlined AWS Integrations » Pay Per Use » Auto Scale » Built-in Versioning © Rowell Belen 21
  • 22.
    Drawbacks » Limited LanguageSupport » Not Suitable for Long-running Tasks » Local Development and Debugging Challenges » Limited Infrastructure Transparency / Less Control » Potential Vendor Lock-in » Cutting-edge quirks » Concurrent Execution Limit is Shared across entire AWS © Rowell Belen 22
  • 23.
    Enough chit-chat, let's seesome code! © Rowell Belen 23
  • 24.
    Sample Function def sendImage(MaphttpEvent, Context context) { try { final request = new RequestContext().input(httpEvent).context(context) final imageUrl = request.httpBody() SqsUtil.instance.sendSQSMessage(inputQueueUrl, imageUrl) new Response().statusCode(200).body("QUEUED: ${imageUrl}") } catch (e) { new Response().statusCode(500).body(e.message) } } © Rowell Belen 24
  • 25.
    What is theServerless Framework? Development toolkit for building, managing and deploying Serverless applications and resources © Rowell Belen 25
  • 26.
    Serverless Framework CLI npminstall serverless -g mkdir my-api && cd my-api serverless create --template aws-groovy-gradle serverless deploy --stage dev serverless invoke --function my-function --log © Rowell Belen 26
  • 27.
    Available Templates » aws-nodejs »aws-python » aws-groovy-gradle » aws-java-maven » aws-scala-sbt » ...etc © Rowell Belen 27
  • 28.
    serverless.yml - BasicFunction service: serverless-demo provider: runtime: java8 timeout: 300 memorySize: 1536 package: artifact: /build/dist/serverless-demo.zip functions: my-function: handler: com.bytekast.serverless.MyLambdaFunction::handler © Rowell Belen 28
  • 29.
    serverless.yml - APIGateway functions: createUser: handler: com.bytekast.serverless.UserService::createUser events: - http: path: users/create method: post deleteUser: handler: com.bytekast.serverless.UserService::deleteUser events: - http: path: users/delete method: delete © Rowell Belen 29
  • 30.
    serverless.yml - APIGateway (Custom Authorization) ... events: - http: path: users/create method: post cors: true authorizer: arn: arn:aws:lambda:us-east-1:1234567890123:function:authorizer identitySource: method.request.header.Authorization identityValidationExpression: Bearer .* © Rowell Belen 30
  • 31.
    serverless.yml - SNSTopic Subscription functions: audit: handler: com.bytekast.serverless.AuditService::audit events: - sns: dev-audit-topic © Rowell Belen 31
  • 32.
    serverless.yml - ScheduledTrigger functions: crawl: handler: com.bytekast.serverless.SearchService::crawl events: - schedule: rate(2 hours) - schedule: cron(0 12 * * ? *) © Rowell Belen 32
  • 33.
    serverless.yml - IAMRole Permissions provider: ... iamRoleStatements: - Effect: "Allow" Action: - "sqs:*" Resource: arn:aws:sqs:us-east-1:1234567890123:dev-serverless-demo © Rowell Belen 33
  • 34.
    serverless.yml - CreateAWS Resources resources: Resources: InboundQueue: Type: "AWS::SQS::Queue" Properties: QueueName: ${self:provider.stage}-serverless-demo MessageRetentionPeriod: 1209600 VisibilityTimeout: 60 © Rowell Belen 34
  • 35.
    Sample Serverless ETLPattern © Rowell Belen 35
  • 36.
    Sample Serverless PlatformArchitecture © Rowell Belen 36
  • 37.
    DEMO Build Car ImageRecognition Service using Serverless Framework + API Gateway + AWS Lambda Sample Project © Rowell Belen 37
  • 38.
    What about CloudScale DevOps? © Rowell Belen 38
  • 39.
    Lambda Built-In Features »Containers are ephemeral entities that are easily created and destroyed » Preserved history and ability to roll back a bad deployment ( revert to previous version ) » Auto-managed horizontal scaling (Scale out / Scale In) » Automatic Load Balancing » Zero/Minimal Downtime during deployment © Rowell Belen 39
  • 40.
    Environments should notbe connected or otherwise intercommunicate © Rowell Belen 40
  • 41.
    Solutions: » AWS Lambdasupports running functions in different VPCs (dev, stage, prod) » Use separate AWS accounts per Environment © Rowell Belen 41
  • 42.
    Promote immutable application artifactsfrom the lowest environment to the highest ( Deployment ) © Rowell Belen 42
  • 43.
    Solutions: » Publish VersionedBuild Artifacts to Repository » Continuous Integration » Continuous Delivery © Rowell Belen 43
  • 44.
  • 45.
    Metrics » Better Dashboards »Custom Metrics and Events © Rowell Belen 45
  • 46.
    Standardize Projects using Templates CustomProject Starter Kit © Rowell Belen 46
  • 47.
    Other Things toConsider » AWS functions are recycled about every 5-8 hours » Container instances idle for about 5 minutes are destroyed » Cold starts can cause delay in response times » 50 MB max deployment package size » 5 minute running time limit / ~ 30 seconds if API Gateway triggered » 1000 default concurrent function executions across entire AWS account © Rowell Belen 47
  • 48.