SlideShare a Scribd company logo
AWS Lambda with Serverless Framework and Java
About
Manish Pandit
Director of Platform Engineering @Marqeta
Twitter : @lobster1234
Blog: lobster1234.github.io
LinkedIn: in/mpandit
Serverless
Serverless is the finest grain abstraction in distributed computing
100% managed with zero infrastructure concerns
Lowest cost compared to Servers and Containers (if designed correctly!)
Why Serverless
Ideal fit for microservices architectures - functions have limited, well
defined scopes
Pricing - you pay for what you use
No need to worry about infrastructure
AWS Lambda
Function as a Service
Simple Pricing Model - Memory * time
Event Sources - DynamoDB, S3, API Gateway, Cloudwatch, SNS...
Security via VPC and IAM Roles
Serverless Framework
Node.js based CLI
Makes working with FaaS much, much easier
Simple, YAML based configuration abstracts Cloudformation
Supports many cloud providers - AWS, OpenWhisk, GCP, Azure
Supports stages
Popular, feature rich, well documented
AWS Lambda with Serverless Framework and Java
Install
bash-3.2$ npm install serverless -g
+ serverless@1.23.0
bash-3.2$ serverless --version
1.23.0
Configure AWS - Option 1
bash-3.2$ aws configure
AWS Access Key ID [****************4MTA]:
AWS Secret Access Key [****************g2uU]:
Default region name [us-east-1]:
Default output format [json]:
bash-3.2$
Configure AWS - Option 2
bash-3.2$ serverless config credentials --provider aws --key XXXX --secret YYY
If you already have a default profile….
Serverless: Setting up AWS...
Serverless: Failed! ~/.aws/credentials already has a "default" profile. Use the
overwrite flag ("-o" or "--overwrite") to force the update
Create a Project
bash-3.2$ serverless create --template aws-java-maven
Serverless: Generating boilerplate...
Serverless: Successfully generated boilerplate for template: "aws-java-maven"
Serverless: NOTE: Please update the "service" property in serverless.yml with your
service name
bash-3.2$
The Project Structure
bash-3.2$ tree .
.
├── pom.xml
├── serverless.yml
└── src
└── main
├── java
│ └── com
│ └── serverless
│ ├── ApiGatewayResponse.java
│ ├── Handler.java
│ └── Response.java
└── resources
└── log4j.properties
6 directories, 6 files
serverless.yml
service: aws-java-maven # NOTE: update this with your service name
provider:
name: aws
runtime: java8
package:
artifact: target/hello-dev.jar
functions:
hello:
handler: com.serverless.Handler
# The following are a few example events you can configure
# NOTE: Please make sure to change your handler code to work with those events
# Check the event documentation for details
# events:
# - http:
# path: users/create
# method: get
Handler Class
package com.serverless;
import …
public class Handler implements RequestHandler<Map<String, Object>, ApiGatewayResponse>
handleRequest Method
@Override
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
BasicConfigurator.configure();
LOG.info("received: " + input);
Response responseBody = new Response("Go Serverless v1.x! Your function executed successfully!",
input);
return ApiGatewayResponse.builder()
.setStatusCode(200)
.setObjectBody(responseBody)
.setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless"))
.build();
}
Build
bash-3.2$ mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello dev
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.666 s
[INFO] Finished at: 2017-10-07T01:34:10-07:00
[INFO] Final Memory: 16M/214M
[INFO] ------------------------------------------------------------------------
Deploy
bash-3.2$ serverless deploy
Serverless: Packaging service...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
...............
Serverless: Stack update finished...
Service Information
service: aws-java-maven
stage: dev
region: us-east-1
stack: aws-java-maven-dev
api keys:
None
endpoints:
None
functions:
hello: aws-java-maven-dev-hello
Invoke
bash-3.2$ serverless invoke -f hello
{
"statusCode": 200,
"body": "{"message":"Go Serverless v1.x! Your function executed successfully!","input":{}}",
"headers": {
"X-Powered-By": "AWS Lambda & serverless"
},
"isBase64Encoded": false
}
bash-3.2$
Execution
1.An event source generates an event, or a trigger
2.A new container is spun off or an existing one is reused to launch
the runtime
3.The function (script if interpreted language, binary if compiled) is
executed
4.The runtime exits
Invoke Local
bash-3.2$ serverless invoke local -f hello
Serverless: In order to get human-readable output, please implement "toString()" method of your
"ApiGatewayResponse" object.
0 [main] INFO com.serverless.Handler - received: {}
com.serverless.ApiGatewayResponse@77cd7a0
bash-3.2$
Using an HTTP Event Source
service: aws-java-maven # NOTE: update this with your service name
provider:
name: aws
runtime: java8
package:
artifact: target/hello-dev.jar
functions:
hello:
handler: com.serverless.Handler
events:
- http:
path: /hello
method: get
Deploy with a new stage!
bash-3.2$ serverless deploy --stage prod
Serverless: Packaging service...
Serverless: Creating Stack...
Serverless: Checking Stack create progress…
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
Serverless: Stack update finished...
Service Information
service: aws-java-maven
stage: prod
region: us-east-1
stack: aws-java-maven-prod
api keys:
None
endpoints:
GET - https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello
functions:
hello: aws-java-maven-prod-hello
Test
bash-3.2$ curl https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello
{"message":"Go Serverless v1.x! Your function executed
successfully!","input":{"resource":"/hello","path":"/hello","httpMethod":"GET","headers":{"Accept":"*/*","Cl
oudFront-Forwarded-Proto":"https","CloudFront-Is-Desktop-Viewer":"true","CloudFront-Is-Mobile-
Viewer":"false","CloudFront-Is-SmartTV-Viewer":"false","CloudFront-Is-Tablet-Viewer":"false","CloudFront-
Viewer-Country":"US","Host":"f582xxq581.execute-api.us-east-1.amazonaws.com","User-
Agent":"curl/7.51.0","Via":"1.1 3cc911e7eb2df956e3f7c8f27c198a0b.cloudfront.net (CloudFront)","X-Amz-Cf-
Id":"rngpB7WCzOt_XnMXzDc0Pv5i9Eq7_ZA1olb08W84xi5Of8Od4VvVVw==","X-Amzn-Trace-Id":"Root=1-59d895b4-
375b518e32e7a6866c80fa0a","X-Forwarded-For":"69.181.73.181, 205.251.214.68","X-Forwarded-Port":"443","X-
Forwarded-
Proto":"https"},"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"
path":"/prod/hello","accountId":"xxxxxxxxxxxx","resourceId":"07sycy","stage":"prod","requestId":"cab13cdc-
ab3c-11e7-b85c-
0fc68f7d77c1","identity":{"cognitoIdentityPoolId":null,"accountId":null,"cognitoIdentityId":null,"caller":nu
ll,"apiKey":"","sourceIp":"69.181.73.181","accessKey":null,"cognitoAuthenticationType":null,"cognitoAuthenti
cationProvider":null,"userArn":null,"userAgent":"curl/7.51.0","user":null},"resourcePath":"/hello","httpMeth
od":"GET","apiId":"f582xxq581"},"body":null,"isBase64Encoded":false}}
bash-3.2$
Serverless Logs
bash-3.2$ serverless logs -f hello
START RequestId: 32c366cc-ab41-11e7-bcba-6f7680b04a52 Version: $LATEST
2017-10-07 09:23:38 <32c366cc-ab41-11e7-bcba-6f7680b04a52> INFO com.serverless.Handler:20 -
received: {}
0 [main] INFO com.serverless.Handler - received: {}
END RequestId: 32c366cc-ab41-11e7-bcba-6f7680b04a52
REPORT RequestId: 32c366cc-ab41-11e7-bcba-6f7680b04a52 Duration: 411.34 ms Billed Duration: 500 ms
Memory Size: 1024 MB Max Memory Used: 63 MB
START RequestId: e41254f3-ab41-11e7-ab1b-750906e00517 Version: $LATEST
2017-10-07 09:28:34 <e41254f3-ab41-11e7-ab1b-750906e00517> INFO com.serverless.Handler:20 -
received: {}
END RequestId: e41254f3-ab41-11e7-ab1b-750906e00517
REPORT RequestId: e41254f3-ab41-11e7-ab1b-750906e00517 Duration: 9.92 ms Billed Duration: 100 ms
Memory Size: 1024 MB Max Memory Used: 63 MB
Serverless Metrics
bash-3.2$ serverless metrics -f hello
Service wide metrics
October 6, 2017 2:21 AM - October 7, 2017 2:21 AM
Invocations: 4
Throttles: 0
Errors: 0
Duration (avg.): 206.84ms
bash-3.2$
Serverless Metrics w/ Stages
bash-3.2$ serverless metrics --stage dev -f hello
hello
October 6, 2017 2:29 AM - October 7, 2017 2:29 AM
Invocations: 8
Throttles: 0
Errors: 0
Duration (avg.): 120.89ms
bash-3.2$ serverless metrics --stage prod -f hello
hello
October 6, 2017 2:29 AM - October 7, 2017 2:29 AM
Invocations: 6
Throttles: 0
Errors: 0
Duration (avg.): 187.97ms
Serverless Info
bash-3.2$ serverless info
Service Information
service: aws-java-maven
stage: dev
region: us-east-1
stack: aws-java-maven-dev
api keys:
None
endpoints:
None
functions:
hello: aws-java-maven-dev-hello
Serverless Info w/Stages
bash-3.2$ serverless info --stage prod
Service Information
service: aws-java-maven
stage: prod
region: us-east-1
stack: aws-java-maven-prod
api keys:
None
endpoints:
GET - https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello
functions:
hello: aws-java-maven-prod-hello
bash-3.2$
Remove Stack
bash-3.2$ serverless remove
Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack removal progress...
....
Serverless: Stack removal finished…
bash-3.2$
Remove Stack w/Stages
bash-3.2$ serverless remove --stage prod
Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack removal progress...
................
Serverless: Stack removal finished…
bash-3.2$ curl https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello
curl: (35) SSL peer handshake failed, the server most likely requires a client
certificate to connect
bash-3.2$
Lambda Tips
1. Write your code in a way that it can be tested without an event source
2. Lambda cannot execute for longer than 5 minutes
3. Coordinating many Lambdas can be difficult, use AWS Step Functions
4. Do not persist anything on the disk - it may not be available
5. Be aware of cold start - JVM will take a while to start
6. Do not write lambdas for everything - if a service is high throughput, you’re better off
with ECS or EC2 given the costs
7. Lambdas can be tied to VPCs and have IAM execution roles
8. Serverless supports API Gateway Proxy Integration by default - everything gets passed to
the function
Resources
Serverless Framework - https://serverless.com
AWS Lambda - https://aws.amazon.com/lambda
My Tutorial - https://lobster1234.github.io/categories/serverless
Questions
Manish Pandit
@lobster1234
lobster1234.github.io

More Related Content

What's hot (9)

PPTX
Packer
Nitesh Saini
 
PDF
The AWS DevOps combo (January 2017)
Julien SIMON
 
PDF
Packer by HashiCorp
Łukasz Cieśluk
 
PDF
Fargate 를 이용한 ECS with VPC 1부
Hyun-Mook Choi
 
PDF
ECS & ECR Deep Dive - 김기완 솔루션즈 아키텍트 :: AWS Container Day
Amazon Web Services Korea
 
PDF
Infrastructure as code with Amazon Web Services
Julien SIMON
 
PPTX
Introduction to Amazon EC2 Container Service and setting up build pipeline wi...
Swapnil Dahiphale
 
PDF
EC2 AMI Factory with Chef, Berkshelf, and Packer
George Miranda
 
PPTX
Learn you some Ansible for great good!
David Lapsley
 
Packer
Nitesh Saini
 
The AWS DevOps combo (January 2017)
Julien SIMON
 
Packer by HashiCorp
Łukasz Cieśluk
 
Fargate 를 이용한 ECS with VPC 1부
Hyun-Mook Choi
 
ECS & ECR Deep Dive - 김기완 솔루션즈 아키텍트 :: AWS Container Day
Amazon Web Services Korea
 
Infrastructure as code with Amazon Web Services
Julien SIMON
 
Introduction to Amazon EC2 Container Service and setting up build pipeline wi...
Swapnil Dahiphale
 
EC2 AMI Factory with Chef, Berkshelf, and Packer
George Miranda
 
Learn you some Ansible for great good!
David Lapsley
 

Similar to AWS Lambda with Serverless Framework and Java (20)

PDF
Serverless Framework Workshop - Tyler Hendrickson, Chicago/burbs
AWS Chicago
 
PDF
Microservices with AWS Lambda and the Serverless Framework
Rowell Belen
 
PDF
Writing and deploying serverless python applications
Cesar Cardenas Desales
 
PDF
PyConIT 2018 Writing and deploying serverless python applications
Cesar Cardenas Desales
 
PDF
Building Serverless APIs on AWS
Julien SIMON
 
PDF
PyConIE 2017 Writing and deploying serverless python applications
Cesar Cardenas Desales
 
PDF
Going Serverless: The Best Ops is NoOps.
PolarSeven Pty Ltd
 
PDF
Čtvrtkon #64 - AWS Serverless - Michal Haták
Ctvrtkoncz
 
PDF
Real world serverless - architecture, patterns and lessons learned
David Schmitz
 
PDF
Serverless Framework (2018)
Rowell Belen
 
PPTX
AWS Serverless concepts and solutions
ElenaIvannikova3
 
PDF
Serverless: A love hate relationship
Jürgen Brüder
 
PDF
AWS MeetUp - Serverless Brewing IPAs
JorisConijn
 
PDF
Building serverless apps with Node.js
Julien SIMON
 
PDF
Serverless Frameworks on AWS
Julien SIMON
 
PDF
Manchester Serverless Meetup - July 2018
Jonathan Vines
 
PDF
AWS Serverless Workshop
Mikael Puittinen
 
PDF
AWSomeDay Zurich 2018 - How to go serverless
Roman Plessl
 
PDF
Automated Serverless Pipelines with #GitOps on Codefresh
Codefresh
 
PDF
Serverless Day Zero: How to Serveless [July 2019]
Dhaval Nagar
 
Serverless Framework Workshop - Tyler Hendrickson, Chicago/burbs
AWS Chicago
 
Microservices with AWS Lambda and the Serverless Framework
Rowell Belen
 
Writing and deploying serverless python applications
Cesar Cardenas Desales
 
PyConIT 2018 Writing and deploying serverless python applications
Cesar Cardenas Desales
 
Building Serverless APIs on AWS
Julien SIMON
 
PyConIE 2017 Writing and deploying serverless python applications
Cesar Cardenas Desales
 
Going Serverless: The Best Ops is NoOps.
PolarSeven Pty Ltd
 
Čtvrtkon #64 - AWS Serverless - Michal Haták
Ctvrtkoncz
 
Real world serverless - architecture, patterns and lessons learned
David Schmitz
 
Serverless Framework (2018)
Rowell Belen
 
AWS Serverless concepts and solutions
ElenaIvannikova3
 
Serverless: A love hate relationship
Jürgen Brüder
 
AWS MeetUp - Serverless Brewing IPAs
JorisConijn
 
Building serverless apps with Node.js
Julien SIMON
 
Serverless Frameworks on AWS
Julien SIMON
 
Manchester Serverless Meetup - July 2018
Jonathan Vines
 
AWS Serverless Workshop
Mikael Puittinen
 
AWSomeDay Zurich 2018 - How to go serverless
Roman Plessl
 
Automated Serverless Pipelines with #GitOps on Codefresh
Codefresh
 
Serverless Day Zero: How to Serveless [July 2019]
Dhaval Nagar
 
Ad

More from Manish Pandit (20)

PDF
Disaster recovery - What, Why, and How
Manish Pandit
 
PDF
Serverless Architectures on AWS in practice - OSCON 2018
Manish Pandit
 
PDF
Disaster Recovery and Reliability
Manish Pandit
 
PDF
OAuth2 primer
Manish Pandit
 
PDF
AWS Primer and Quickstart
Manish Pandit
 
PPTX
Securing your APIs with OAuth, OpenID, and OpenID Connect
Manish Pandit
 
PDF
Silicon Valley 2014 - API Antipatterns
Manish Pandit
 
PDF
Scalabay - API Design Antipatterns
Manish Pandit
 
PDF
OSCON 2014 - API Ecosystem with Scala, Scalatra, and Swagger at Netflix
Manish Pandit
 
PPTX
API Design Antipatterns - APICon SF
Manish Pandit
 
PPTX
Motivation : it Matters
Manish Pandit
 
PPTX
Building Apis in Scala with Playframework2
Manish Pandit
 
PPTX
Scala at Netflix
Manish Pandit
 
PPT
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Manish Pandit
 
PPT
Evolving IGN’s New APIs with Scala
Manish Pandit
 
PPTX
IGN's V3 API
Manish Pandit
 
PPTX
Java and the JVM
Manish Pandit
 
PPTX
Object Oriented Programming
Manish Pandit
 
PPTX
Silicon Valley Code Camp 2011: Play! as you REST
Manish Pandit
 
PPTX
Silicon Valley Code Camp: 2011 Introduction to MongoDB
Manish Pandit
 
Disaster recovery - What, Why, and How
Manish Pandit
 
Serverless Architectures on AWS in practice - OSCON 2018
Manish Pandit
 
Disaster Recovery and Reliability
Manish Pandit
 
OAuth2 primer
Manish Pandit
 
AWS Primer and Quickstart
Manish Pandit
 
Securing your APIs with OAuth, OpenID, and OpenID Connect
Manish Pandit
 
Silicon Valley 2014 - API Antipatterns
Manish Pandit
 
Scalabay - API Design Antipatterns
Manish Pandit
 
OSCON 2014 - API Ecosystem with Scala, Scalatra, and Swagger at Netflix
Manish Pandit
 
API Design Antipatterns - APICon SF
Manish Pandit
 
Motivation : it Matters
Manish Pandit
 
Building Apis in Scala with Playframework2
Manish Pandit
 
Scala at Netflix
Manish Pandit
 
Introducing Scala to your Ruby/Java Shop : My experiences at IGN
Manish Pandit
 
Evolving IGN’s New APIs with Scala
Manish Pandit
 
IGN's V3 API
Manish Pandit
 
Java and the JVM
Manish Pandit
 
Object Oriented Programming
Manish Pandit
 
Silicon Valley Code Camp 2011: Play! as you REST
Manish Pandit
 
Silicon Valley Code Camp: 2011 Introduction to MongoDB
Manish Pandit
 
Ad

Recently uploaded (20)

PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 

AWS Lambda with Serverless Framework and Java

  • 2. About Manish Pandit Director of Platform Engineering @Marqeta Twitter : @lobster1234 Blog: lobster1234.github.io LinkedIn: in/mpandit
  • 3. Serverless Serverless is the finest grain abstraction in distributed computing 100% managed with zero infrastructure concerns Lowest cost compared to Servers and Containers (if designed correctly!)
  • 4. Why Serverless Ideal fit for microservices architectures - functions have limited, well defined scopes Pricing - you pay for what you use No need to worry about infrastructure
  • 5. AWS Lambda Function as a Service Simple Pricing Model - Memory * time Event Sources - DynamoDB, S3, API Gateway, Cloudwatch, SNS... Security via VPC and IAM Roles
  • 6. Serverless Framework Node.js based CLI Makes working with FaaS much, much easier Simple, YAML based configuration abstracts Cloudformation Supports many cloud providers - AWS, OpenWhisk, GCP, Azure Supports stages Popular, feature rich, well documented
  • 8. Install bash-3.2$ npm install serverless -g + [email protected] bash-3.2$ serverless --version 1.23.0
  • 9. Configure AWS - Option 1 bash-3.2$ aws configure AWS Access Key ID [****************4MTA]: AWS Secret Access Key [****************g2uU]: Default region name [us-east-1]: Default output format [json]: bash-3.2$
  • 10. Configure AWS - Option 2 bash-3.2$ serverless config credentials --provider aws --key XXXX --secret YYY If you already have a default profile…. Serverless: Setting up AWS... Serverless: Failed! ~/.aws/credentials already has a "default" profile. Use the overwrite flag ("-o" or "--overwrite") to force the update
  • 11. Create a Project bash-3.2$ serverless create --template aws-java-maven Serverless: Generating boilerplate... Serverless: Successfully generated boilerplate for template: "aws-java-maven" Serverless: NOTE: Please update the "service" property in serverless.yml with your service name bash-3.2$
  • 12. The Project Structure bash-3.2$ tree . . ├── pom.xml ├── serverless.yml └── src └── main ├── java │ └── com │ └── serverless │ ├── ApiGatewayResponse.java │ ├── Handler.java │ └── Response.java └── resources └── log4j.properties 6 directories, 6 files
  • 13. serverless.yml service: aws-java-maven # NOTE: update this with your service name provider: name: aws runtime: java8 package: artifact: target/hello-dev.jar functions: hello: handler: com.serverless.Handler # The following are a few example events you can configure # NOTE: Please make sure to change your handler code to work with those events # Check the event documentation for details # events: # - http: # path: users/create # method: get
  • 14. Handler Class package com.serverless; import … public class Handler implements RequestHandler<Map<String, Object>, ApiGatewayResponse>
  • 15. handleRequest Method @Override public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) { BasicConfigurator.configure(); LOG.info("received: " + input); Response responseBody = new Response("Go Serverless v1.x! Your function executed successfully!", input); return ApiGatewayResponse.builder() .setStatusCode(200) .setObjectBody(responseBody) .setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless")) .build(); }
  • 16. Build bash-3.2$ mvn clean install [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building hello dev [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.666 s [INFO] Finished at: 2017-10-07T01:34:10-07:00 [INFO] Final Memory: 16M/214M [INFO] ------------------------------------------------------------------------
  • 17. Deploy bash-3.2$ serverless deploy Serverless: Packaging service... Serverless: Creating Stack... Serverless: Checking Stack create progress... Serverless: Stack create finished... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading artifacts... Serverless: Validating template... Serverless: Updating Stack... Serverless: Checking Stack update progress... ............... Serverless: Stack update finished... Service Information service: aws-java-maven stage: dev region: us-east-1 stack: aws-java-maven-dev api keys: None endpoints: None functions: hello: aws-java-maven-dev-hello
  • 18. Invoke bash-3.2$ serverless invoke -f hello { "statusCode": 200, "body": "{"message":"Go Serverless v1.x! Your function executed successfully!","input":{}}", "headers": { "X-Powered-By": "AWS Lambda & serverless" }, "isBase64Encoded": false } bash-3.2$
  • 19. Execution 1.An event source generates an event, or a trigger 2.A new container is spun off or an existing one is reused to launch the runtime 3.The function (script if interpreted language, binary if compiled) is executed 4.The runtime exits
  • 20. Invoke Local bash-3.2$ serverless invoke local -f hello Serverless: In order to get human-readable output, please implement "toString()" method of your "ApiGatewayResponse" object. 0 [main] INFO com.serverless.Handler - received: {} com.serverless.ApiGatewayResponse@77cd7a0 bash-3.2$
  • 21. Using an HTTP Event Source service: aws-java-maven # NOTE: update this with your service name provider: name: aws runtime: java8 package: artifact: target/hello-dev.jar functions: hello: handler: com.serverless.Handler events: - http: path: /hello method: get
  • 22. Deploy with a new stage! bash-3.2$ serverless deploy --stage prod Serverless: Packaging service... Serverless: Creating Stack... Serverless: Checking Stack create progress… Serverless: Updating Stack... Serverless: Checking Stack update progress... Serverless: Stack update finished... Service Information service: aws-java-maven stage: prod region: us-east-1 stack: aws-java-maven-prod api keys: None endpoints: GET - https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello functions: hello: aws-java-maven-prod-hello
  • 23. Test bash-3.2$ curl https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello {"message":"Go Serverless v1.x! Your function executed successfully!","input":{"resource":"/hello","path":"/hello","httpMethod":"GET","headers":{"Accept":"*/*","Cl oudFront-Forwarded-Proto":"https","CloudFront-Is-Desktop-Viewer":"true","CloudFront-Is-Mobile- Viewer":"false","CloudFront-Is-SmartTV-Viewer":"false","CloudFront-Is-Tablet-Viewer":"false","CloudFront- Viewer-Country":"US","Host":"f582xxq581.execute-api.us-east-1.amazonaws.com","User- Agent":"curl/7.51.0","Via":"1.1 3cc911e7eb2df956e3f7c8f27c198a0b.cloudfront.net (CloudFront)","X-Amz-Cf- Id":"rngpB7WCzOt_XnMXzDc0Pv5i9Eq7_ZA1olb08W84xi5Of8Od4VvVVw==","X-Amzn-Trace-Id":"Root=1-59d895b4- 375b518e32e7a6866c80fa0a","X-Forwarded-For":"69.181.73.181, 205.251.214.68","X-Forwarded-Port":"443","X- Forwarded- Proto":"https"},"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{" path":"/prod/hello","accountId":"xxxxxxxxxxxx","resourceId":"07sycy","stage":"prod","requestId":"cab13cdc- ab3c-11e7-b85c- 0fc68f7d77c1","identity":{"cognitoIdentityPoolId":null,"accountId":null,"cognitoIdentityId":null,"caller":nu ll,"apiKey":"","sourceIp":"69.181.73.181","accessKey":null,"cognitoAuthenticationType":null,"cognitoAuthenti cationProvider":null,"userArn":null,"userAgent":"curl/7.51.0","user":null},"resourcePath":"/hello","httpMeth od":"GET","apiId":"f582xxq581"},"body":null,"isBase64Encoded":false}} bash-3.2$
  • 24. Serverless Logs bash-3.2$ serverless logs -f hello START RequestId: 32c366cc-ab41-11e7-bcba-6f7680b04a52 Version: $LATEST 2017-10-07 09:23:38 <32c366cc-ab41-11e7-bcba-6f7680b04a52> INFO com.serverless.Handler:20 - received: {} 0 [main] INFO com.serverless.Handler - received: {} END RequestId: 32c366cc-ab41-11e7-bcba-6f7680b04a52 REPORT RequestId: 32c366cc-ab41-11e7-bcba-6f7680b04a52 Duration: 411.34 ms Billed Duration: 500 ms Memory Size: 1024 MB Max Memory Used: 63 MB START RequestId: e41254f3-ab41-11e7-ab1b-750906e00517 Version: $LATEST 2017-10-07 09:28:34 <e41254f3-ab41-11e7-ab1b-750906e00517> INFO com.serverless.Handler:20 - received: {} END RequestId: e41254f3-ab41-11e7-ab1b-750906e00517 REPORT RequestId: e41254f3-ab41-11e7-ab1b-750906e00517 Duration: 9.92 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 63 MB
  • 25. Serverless Metrics bash-3.2$ serverless metrics -f hello Service wide metrics October 6, 2017 2:21 AM - October 7, 2017 2:21 AM Invocations: 4 Throttles: 0 Errors: 0 Duration (avg.): 206.84ms bash-3.2$
  • 26. Serverless Metrics w/ Stages bash-3.2$ serverless metrics --stage dev -f hello hello October 6, 2017 2:29 AM - October 7, 2017 2:29 AM Invocations: 8 Throttles: 0 Errors: 0 Duration (avg.): 120.89ms bash-3.2$ serverless metrics --stage prod -f hello hello October 6, 2017 2:29 AM - October 7, 2017 2:29 AM Invocations: 6 Throttles: 0 Errors: 0 Duration (avg.): 187.97ms
  • 27. Serverless Info bash-3.2$ serverless info Service Information service: aws-java-maven stage: dev region: us-east-1 stack: aws-java-maven-dev api keys: None endpoints: None functions: hello: aws-java-maven-dev-hello
  • 28. Serverless Info w/Stages bash-3.2$ serverless info --stage prod Service Information service: aws-java-maven stage: prod region: us-east-1 stack: aws-java-maven-prod api keys: None endpoints: GET - https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello functions: hello: aws-java-maven-prod-hello bash-3.2$
  • 29. Remove Stack bash-3.2$ serverless remove Serverless: Getting all objects in S3 bucket... Serverless: Removing objects in S3 bucket... Serverless: Removing Stack... Serverless: Checking Stack removal progress... .... Serverless: Stack removal finished… bash-3.2$
  • 30. Remove Stack w/Stages bash-3.2$ serverless remove --stage prod Serverless: Getting all objects in S3 bucket... Serverless: Removing objects in S3 bucket... Serverless: Removing Stack... Serverless: Checking Stack removal progress... ................ Serverless: Stack removal finished… bash-3.2$ curl https://f582xxq581.execute-api.us-east-1.amazonaws.com/prod/hello curl: (35) SSL peer handshake failed, the server most likely requires a client certificate to connect bash-3.2$
  • 31. Lambda Tips 1. Write your code in a way that it can be tested without an event source 2. Lambda cannot execute for longer than 5 minutes 3. Coordinating many Lambdas can be difficult, use AWS Step Functions 4. Do not persist anything on the disk - it may not be available 5. Be aware of cold start - JVM will take a while to start 6. Do not write lambdas for everything - if a service is high throughput, you’re better off with ECS or EC2 given the costs 7. Lambdas can be tied to VPCs and have IAM execution roles 8. Serverless supports API Gateway Proxy Integration by default - everything gets passed to the function
  • 32. Resources Serverless Framework - https://serverless.com AWS Lambda - https://aws.amazon.com/lambda My Tutorial - https://lobster1234.github.io/categories/serverless