SlideShare a Scribd company logo
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
21.Jul.2018
Legacy JavaEE Lambda
Kim Kao
Solution Architect @ AWS
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Legacy
Legacy!
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Java EE
• Oracle Java EE
• Apache / Eclipse
• Java EE 9
•
• MicroProfile by Eclipse
Jakarta EE is coming
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Jarkata EE
• Specification JCP
• Version Java EE 8
•
• Eclipse GlassFish
• Apache TomEE
• Wildfly
• Oracle Weblogic
• Jboss
• IBM Websphere
• Eclipse Foundation
Old Name New Name
Java EE Jakarta EE
Glassfish Eclipse Glassfish
Java Community
Process (JCP) ¹
Eclipse EE.next
Working
Group (EE.next)
Oracle
development
management
Eclipse Enterprise
for Java (EE4J)
Project
Management
Committee (PMC)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Java EE
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Why Migrate to Serverless
?
SERVERLESS
Serverless …
Legacy java ee  meet lambda
Legacy java ee  meet lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Serverless compute manifesto
• Functions are the unit of deployment and scaling.
• No machines, VMs, or containers visible in the programming
model.
• Permanent storage lives elsewhere.
• Scales per request. Users cannot over- or under-provision capacity.
• Never pay for idle (no cold servers/containers or their costs).
• Implicitly fault-tolerant because functions can run anywhere.
• BYOC – Bring your own code.
• Metrics and logging are a universal right.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Lambda ?
• Linux
• Process/Networking
•
•
•
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
*
API
API
?
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• API
• API Key
• SigV4 API
• API
• Lambda
Amazon API Gateway
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
API Gateway
• Managed cache to store API responses
• Reduced latency and distributed denial of service (DDoS)
protection through Amazon CloudFront
• SDK generation for iOS, Android, and JavaScript
• Swagger support
• Request/response data transformation
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Microservices and AWS Lambda
AWS Lambda + Amazon API Gateway is the
easiest way to create microservices
• Event handlers one function per event type
• Serverless backends one function per API / path
• Data processing one function per data type
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon API Gateway: Serverless APIs
Internet
Mobile apps
Websites
Services
AWS Lambda
functions
AWS
API Gateway
cache
Endpoints on
Amazon EC2
Any other publicly
accessible endpointAmazon
CloudWatch
Amazon
CloudFront
Amazon
API Gateway
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Serverless Java Container
• A Java wrapper to run Spring, Jersey and Spark Java
Apps inside AWS Lambda, without a Servlet engine
• Open source via awslabs, Apache 2.0 license, written by
Stefano Biliani
• https://github.com/awslabs/aws-serverless-java-
container
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Client Sends Request API Gateway
Lambda Proxy
Request
Serverless Java
Container
Spring
(HttpServletRequest)
Client Receives
Response API Gateway Lambda Proxy Response
Serverless Java
Container
Spring
(HttpServletResponse)
– Spring-boot
– SparkJava
–
Spring Boot
SparkJava Guice
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Legacy
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• AWS Lambda
• ServeletRequest handler lambda handler
• ! !
• AWS SAM
•
• Serverless Friendly
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Difference What to avoid Possible Solution
No sticky sessions /
session replication
Avoid HttpSession variables Store state in (DynamoDB / RDS)
5 minute timeout Avoid long blocking requests
Call another Lambda asynchronously.
Use an appropriate service for long running tasks
Memory Limit Avoid large dependencies
Break monolithic apps into separate “microservice”
Spring projects, you can use the same Cognito user
pool with multiple API Gateway APIs
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Difference What to avoid Possible Solution
Containers may be reused
for subsequent requests
Avoid relying too much on
container reuse
You can still use in-memory caching to save
costly initializations if the container is reused
Containers are never
reused for concurrent
requests.
Avoid returning DeferredResult /
CompletableFuture, @Async,
servlet 3.0 asyncSupport, relying
on large DB connection pools
Use regular @RequestMapping,
Avoid unneeded thread safety synchronization
Use per-lambda throttling to for maxconn in DBs
No streaming HTTP
response or WebSockets
support
Avoid old school “comet” (long
polling / streaming)
WebSockets
Use a service that supports WebSockets
(AWS Iot MessageBroker, AppSync) or launch
on EC2
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Difference What to avoid Possible Solution
Serving static assets /
near-static content
Avoid serving static resources
from /resources or WEB-
INF/views, or using JSPs or
other serverside rendering
Serve static assets from s3 / CloudFront.
Use a client side “SPA” framework (Angular,
React, Vue etc).
Can’t use legacy
distributed cache methods
(e.g. jGroups, multicast or
unicast)
Avoid jGroups or similar solution
for distributed Hibernate 2nd level
cache etc.
Use a managed cache service. E.g. Hibernate
2nd level cache memcached support (e.g. on
AWS ElastiCache)
Stateless
Avoid cookie based
authentication
Use JWT for authentication (Cognito)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• - S3 + Cloudfront
• DI – Spark Framework Spring
• lambda Cold start time
•
• Shade
aws-java-sdk-bom
aws-xray-recorder-sdk-bom
• X-ray
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
– Legacy SpringWebMVC
@RequestMapping(path = "/pets", method = RequestMethod.POST)
public Pet createPet(@RequestBody Pet newPet) {
if (newPet.getName() == null || newPet.getBreed() == null) {
return null;
}
Pet dbPet = newPet;
dbPet.setId(UUID.randomUUID().toString());
return dbPet;
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
– Wrapper
private static SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class);
handler.onStartup(servletContext -> {
FilterRegistration.Dynamic registration =
servletContext.addFilter("CognitoIdentityFilter", CognitoIdentityFilter.class);
registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
});
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
handler.proxyStream(inputStream, outputStream, context);
// just in case it wasn't closed by the mapper
outputStream.close();
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
•
• Junit
• Mockito
• MvcMock
• Spring related Mock/Test framework
• Selenium
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Wrapper
@Test
public void handleRequest() throws Exception {
// create a Proxy Request
AwsProxyRequest proxyRequest = new AwsProxyRequestBuilder().path("/").build();
// convert the request to JSON, then to a byte array
ByteArrayInputStream in = new
ByteArrayInputStream(objectMapper.writeValueAsString(proxyRequest).getBytes("UTF-8"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
// call the Lambda handler
underTest.handleRequest(in, out, new MockLambdaContext());
// convert the response to a Proxy Response
AwsProxyResponse awsProxyResponse = objectMapper.readValue(out.toByteArray(),
AwsProxyResponse.class);
//test something with the response body
Assert.assertEquals(awsProxyResponse.getBody(), "expected");
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Serverless
Author Package Test Deploy
• AWS
• ?
• AWS SAM( CloudFormation )
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS CloudFormation
•
• CloudFormation Stack
• YAML or JSON
https://github.com/humank-awshandson/20171116-cfn-lab
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Serverless Application Model (SAM)
• Serverless App
• : Function, API, Tables
• CloudFormation SAM
• Open specification (Apache 2.0)
NEW!
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
SAM
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Example Pet Store API written with spark/guice with the aws-serverless-java-container library
Resources:
PetStoreFunction:
Type: AWS::Serverless::Function
Properties:
Handler: solid.humank.serverlesslabs.SparkStreamLambdaHandler::handleRequest
Runtime: java8
CodeUri: target/sparkjava-petclinic-1.0.0.jar
MemorySize: 512
Policies: AWSLambdaFullAccess
Timeout: 60
Events:
GetResource:
Type: Api
Properties:
Path: /{proxy+}
Method: any
Outputs:
SpringBootPetStoreApi:
Description: URL for application
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/owners'
Export:
Name: SparkPetClinicApi
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
lambda
• Package
• (.zip)
• S3 Bucket
• S3 code URI
• Deploy
• CloudFormation ‘CreateChangeSet’ API
• CloudFormation ‘ExecuteChangeSet’ API
NEW!
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda
AWS Lambda
Amazon DynamoDB
Amazon SNS
Amazon API Gateway
Amazon SQS
Amazon Kinesis
THE COMPLETE PLATFORM
Amazon S3
AWS Step Functions
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
JavaEE Serverless
•
• Wrapper APIG Servelet
Handler
•
•
•
•

More Related Content

PDF
2018 10-19-jc conf-embrace-legacy-java-ee-by-aws-serverless
PPTX
Data Design and Modeling for Microservices I AWS Dev Day 2018
PPTX
Building Global Multi-Region, Active-Active Serverless Backends I AWS Dev Day...
PPTX
Deep Dive on Amazon Elastic Container Service (ECS) I AWS Dev Day 2018
POTX
Serverless: State of The Union I AWS Dev Day 2018
PPTX
Containers State of the Union I AWS Dev Day 2018
PDF
Serverless use cases with AWS Lambda - More Serverless Event
PPTX
Gluecon 2018 - The Best Practices and Hard Lessons Learned of Serverless Appl...
2018 10-19-jc conf-embrace-legacy-java-ee-by-aws-serverless
Data Design and Modeling for Microservices I AWS Dev Day 2018
Building Global Multi-Region, Active-Active Serverless Backends I AWS Dev Day...
Deep Dive on Amazon Elastic Container Service (ECS) I AWS Dev Day 2018
Serverless: State of The Union I AWS Dev Day 2018
Containers State of the Union I AWS Dev Day 2018
Serverless use cases with AWS Lambda - More Serverless Event
Gluecon 2018 - The Best Practices and Hard Lessons Learned of Serverless Appl...

Similar to Legacy java ee meet lambda (7)

PDF
Devops on serverless
PDF
CI/CD for AWS Lambda Projects - IsraelCloud Meetup
PDF
The Best Practices and Hard Lessons Learned of Serverless Applications
PPTX
AWS Startup Day - Boston 2018 - The Best Practices and Hard Lessons Learned o...
PDF
Serverless on AWS: Architectural Patterns and Best Practices
PDF
Wildrydes Serverless Workshop Tel Aviv
PDF
Ci/CD for AWS Lambda Projects - JLM CTO Club
Devops on serverless
CI/CD for AWS Lambda Projects - IsraelCloud Meetup
The Best Practices and Hard Lessons Learned of Serverless Applications
AWS Startup Day - Boston 2018 - The Best Practices and Hard Lessons Learned o...
Serverless on AWS: Architectural Patterns and Best Practices
Wildrydes Serverless Workshop Tel Aviv
Ci/CD for AWS Lambda Projects - JLM CTO Club
Ad

More from Kim Kao (12)

PDF
Enlarge influence by Participating in communities
PDF
2019 08-01-i ddd-studygroup-appendix
PDF
跟著Actor Model來一場與DDD的豔遇
PDF
2019 06-12-aws taipei summit-dev day-essential capabilities behind microservices
PDF
Ddd by-clark chou
PDF
My past-3 yeas-developer-journey-at-linkedin-by-iantsai
PDF
2019 04-25-agile communitymeetup-essentialcapabilitiesbehindmicroservices
PDF
2019 03-23-2nd-meetup-essential capabilities behind microservices
PDF
Ddd(meetup 2) ddd with clean architecture
PDF
2019 03-13-implementing microservices by ddd
PDF
2019-02-20-ddd taiwan-community-iddd-studygroup-1st
PDF
DDD Taiwan Community 2019 01-26-1st-meetup-why ddd matters
Enlarge influence by Participating in communities
2019 08-01-i ddd-studygroup-appendix
跟著Actor Model來一場與DDD的豔遇
2019 06-12-aws taipei summit-dev day-essential capabilities behind microservices
Ddd by-clark chou
My past-3 yeas-developer-journey-at-linkedin-by-iantsai
2019 04-25-agile communitymeetup-essentialcapabilitiesbehindmicroservices
2019 03-23-2nd-meetup-essential capabilities behind microservices
Ddd(meetup 2) ddd with clean architecture
2019 03-13-implementing microservices by ddd
2019-02-20-ddd taiwan-community-iddd-studygroup-1st
DDD Taiwan Community 2019 01-26-1st-meetup-why ddd matters
Ad

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Spectroscopy.pptx food analysis technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Empathic Computing: Creating Shared Understanding
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Big Data Technologies - Introduction.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
KodekX | Application Modernization Development
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
Digital-Transformation-Roadmap-for-Companies.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
NewMind AI Weekly Chronicles - August'25 Week I
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
Empathic Computing: Creating Shared Understanding
Review of recent advances in non-invasive hemoglobin estimation
Big Data Technologies - Introduction.pptx
Electronic commerce courselecture one. Pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KodekX | Application Modernization Development
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf
MIND Revenue Release Quarter 2 2025 Press Release
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars

Legacy java ee meet lambda

  • 1. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 21.Jul.2018 Legacy JavaEE Lambda Kim Kao Solution Architect @ AWS
  • 2. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Legacy Legacy!
  • 3. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Java EE • Oracle Java EE • Apache / Eclipse • Java EE 9 • • MicroProfile by Eclipse Jakarta EE is coming
  • 4. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Jarkata EE • Specification JCP • Version Java EE 8 • • Eclipse GlassFish • Apache TomEE • Wildfly • Oracle Weblogic • Jboss • IBM Websphere • Eclipse Foundation Old Name New Name Java EE Jakarta EE Glassfish Eclipse Glassfish Java Community Process (JCP) ¹ Eclipse EE.next Working Group (EE.next) Oracle development management Eclipse Enterprise for Java (EE4J) Project Management Committee (PMC)
  • 5. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Java EE
  • 6. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Why Migrate to Serverless
  • 7. ?
  • 12. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless compute manifesto • Functions are the unit of deployment and scaling. • No machines, VMs, or containers visible in the programming model. • Permanent storage lives elsewhere. • Scales per request. Users cannot over- or under-provision capacity. • Never pay for idle (no cold servers/containers or their costs). • Implicitly fault-tolerant because functions can run anywhere. • BYOC – Bring your own code. • Metrics and logging are a universal right.
  • 13. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda ? • Linux • Process/Networking • • • •
  • 14. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda
  • 15. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. * API API ?
  • 16. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. • API • API Key • SigV4 API • API • Lambda Amazon API Gateway
  • 17. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. API Gateway • Managed cache to store API responses • Reduced latency and distributed denial of service (DDoS) protection through Amazon CloudFront • SDK generation for iOS, Android, and JavaScript • Swagger support • Request/response data transformation
  • 18. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Microservices and AWS Lambda AWS Lambda + Amazon API Gateway is the easiest way to create microservices • Event handlers one function per event type • Serverless backends one function per API / path • Data processing one function per data type
  • 19. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon API Gateway: Serverless APIs Internet Mobile apps Websites Services AWS Lambda functions AWS API Gateway cache Endpoints on Amazon EC2 Any other publicly accessible endpointAmazon CloudWatch Amazon CloudFront Amazon API Gateway
  • 20. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 21. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless Java Container • A Java wrapper to run Spring, Jersey and Spark Java Apps inside AWS Lambda, without a Servlet engine • Open source via awslabs, Apache 2.0 license, written by Stefano Biliani • https://github.com/awslabs/aws-serverless-java- container
  • 22. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Client Sends Request API Gateway Lambda Proxy Request Serverless Java Container Spring (HttpServletRequest) Client Receives Response API Gateway Lambda Proxy Response Serverless Java Container Spring (HttpServletResponse)
  • 25.
  • 27. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Legacy
  • 28. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. • AWS Lambda • ServeletRequest handler lambda handler • ! ! • AWS SAM • • Serverless Friendly
  • 29. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Difference What to avoid Possible Solution No sticky sessions / session replication Avoid HttpSession variables Store state in (DynamoDB / RDS) 5 minute timeout Avoid long blocking requests Call another Lambda asynchronously. Use an appropriate service for long running tasks Memory Limit Avoid large dependencies Break monolithic apps into separate “microservice” Spring projects, you can use the same Cognito user pool with multiple API Gateway APIs
  • 30. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Difference What to avoid Possible Solution Containers may be reused for subsequent requests Avoid relying too much on container reuse You can still use in-memory caching to save costly initializations if the container is reused Containers are never reused for concurrent requests. Avoid returning DeferredResult / CompletableFuture, @Async, servlet 3.0 asyncSupport, relying on large DB connection pools Use regular @RequestMapping, Avoid unneeded thread safety synchronization Use per-lambda throttling to for maxconn in DBs No streaming HTTP response or WebSockets support Avoid old school “comet” (long polling / streaming) WebSockets Use a service that supports WebSockets (AWS Iot MessageBroker, AppSync) or launch on EC2
  • 31. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Difference What to avoid Possible Solution Serving static assets / near-static content Avoid serving static resources from /resources or WEB- INF/views, or using JSPs or other serverside rendering Serve static assets from s3 / CloudFront. Use a client side “SPA” framework (Angular, React, Vue etc). Can’t use legacy distributed cache methods (e.g. jGroups, multicast or unicast) Avoid jGroups or similar solution for distributed Hibernate 2nd level cache etc. Use a managed cache service. E.g. Hibernate 2nd level cache memcached support (e.g. on AWS ElastiCache) Stateless Avoid cookie based authentication Use JWT for authentication (Cognito)
  • 32. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. • - S3 + Cloudfront • DI – Spark Framework Spring • lambda Cold start time • • Shade aws-java-sdk-bom aws-xray-recorder-sdk-bom • X-ray
  • 33. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. – Legacy SpringWebMVC @RequestMapping(path = "/pets", method = RequestMethod.POST) public Pet createPet(@RequestBody Pet newPet) { if (newPet.getName() == null || newPet.getBreed() == null) { return null; } Pet dbPet = newPet; dbPet.setId(UUID.randomUUID().toString()); return dbPet; }
  • 34. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. – Wrapper private static SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler; handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig.class); handler.onStartup(servletContext -> { FilterRegistration.Dynamic registration = servletContext.addFilter("CognitoIdentityFilter", CognitoIdentityFilter.class); registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); }); @Override public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { handler.proxyStream(inputStream, outputStream, context); // just in case it wasn't closed by the mapper outputStream.close();
  • 35. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
  • 36. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. • • • Junit • Mockito • MvcMock • Spring related Mock/Test framework • Selenium
  • 37. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Wrapper @Test public void handleRequest() throws Exception { // create a Proxy Request AwsProxyRequest proxyRequest = new AwsProxyRequestBuilder().path("/").build(); // convert the request to JSON, then to a byte array ByteArrayInputStream in = new ByteArrayInputStream(objectMapper.writeValueAsString(proxyRequest).getBytes("UTF-8")); ByteArrayOutputStream out = new ByteArrayOutputStream(); // call the Lambda handler underTest.handleRequest(in, out, new MockLambdaContext()); // convert the response to a Proxy Response AwsProxyResponse awsProxyResponse = objectMapper.readValue(out.toByteArray(), AwsProxyResponse.class); //test something with the response body Assert.assertEquals(awsProxyResponse.getBody(), "expected"); }
  • 38. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless Author Package Test Deploy • AWS • ? • AWS SAM( CloudFormation )
  • 39. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS CloudFormation • • CloudFormation Stack • YAML or JSON https://github.com/humank-awshandson/20171116-cfn-lab
  • 40. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Serverless Application Model (SAM) • Serverless App • : Function, API, Tables • CloudFormation SAM • Open specification (Apache 2.0) NEW!
  • 41. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. SAM AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Example Pet Store API written with spark/guice with the aws-serverless-java-container library Resources: PetStoreFunction: Type: AWS::Serverless::Function Properties: Handler: solid.humank.serverlesslabs.SparkStreamLambdaHandler::handleRequest Runtime: java8 CodeUri: target/sparkjava-petclinic-1.0.0.jar MemorySize: 512 Policies: AWSLambdaFullAccess Timeout: 60 Events: GetResource: Type: Api Properties: Path: /{proxy+} Method: any Outputs: SpringBootPetStoreApi: Description: URL for application Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/owners' Export: Name: SparkPetClinicApi
  • 42. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. lambda • Package • (.zip) • S3 Bucket • S3 code URI • Deploy • CloudFormation ‘CreateChangeSet’ API • CloudFormation ‘ExecuteChangeSet’ API NEW!
  • 43. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda AWS Lambda Amazon DynamoDB Amazon SNS Amazon API Gateway Amazon SQS Amazon Kinesis THE COMPLETE PLATFORM Amazon S3 AWS Step Functions
  • 44. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. JavaEE Serverless • • Wrapper APIG Servelet Handler • • • •