SlideShare a Scribd company logo
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
19 Oct, 2018
Kim Kao( ), Solutions Architect
Embrace Legacy Java EE
By AWS Serverless
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Classic Java EE Core Pattern
ü Tiering Paradigm
ü Distributed System Design
ü Spring framework Birth
ü Beautiful methodologies age
http://www.corej2eepatterns.com/
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
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)
Ecosystem is changing …
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Old Soldiers never die
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
“The cloud is the new normal”
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
“All Things Distributed”
“Design to Fail”
“Redundant is hard”
“No servers is easier to manage than No server”
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS customers modernize applications one at a
time
Assess and
prioritize, one
app at a time
Re-host (lift-and-shift)
data center à EC2
Re-platform (lift-tinker-shift)
VMs à containers
Re-factor
monolith à microservices
Re-invent (cloud-native)
new serverless microservices
Determine
modernization
path
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Why AWS for modern applications?
Experience
Partners
Portfolio
Customers
Developer
Community
AWS frees
innovators to innovate
and fail without fear
Any starting point
Any application
Anything is possible
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Why migrate to Serverless?
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Computing evolution – A paradigm shift
FOCUS ON BUSINESS LOGIC
LEVELOFABSTRACTION
Virtual Servers
in Datacenters
Virtual Servers
in Datacenters
Physical Machines
Virtual Machines
Containerization
Serverless
• Continuous scaling
• Fault tolerance built-in
• Event-driven
• Pay per usage
• Zero maintenanceAWS Fargate
Amazon
API Gateway
AWS
Lambda
AWS Step
Function
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
No servers to provision
or manage
Scales with usage
Never pay for idle Availability and fault
tolerance built in
Serverless means…
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What does Fargate mean?
Serverless Container Management.
No worrying about scaling, service mesh, underlying infrastructure,
cluster resources, capacity, setup.
Just give it a task definition or pod (in 2018), set some resource
limits, and away you go.
© 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
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cost-effective and
efficient
No Infrastructure
to manage
Pay only for what you use
Bring Your
Own Code
Productivity focused compute platform to build powerful, dynamic, modular
applications in the cloud
Run code in standard
languages
Focus on business logic
Benefits of AWS Lambda
1 2 3
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
How it works
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Using AWS Lambda
Bring your own code
• Node.js, Java, Python, C#
• Bring your own libraries
(even native ones)
Simple resource model
• Select power rating from
128 MB to 1.5 GB
• CPU and network
allocated proportionately
Flexible use
• Synchronous or
asynchronous
• Integrated with other
AWS services
Flexible authorization
• Securely grant access to
resources and VPCs
• Fine-grained control for
invoking your functions
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Using AWS Lambda
Authoring functions
• WYSIWYG editor or
upload packaged .zip
• Third-party plugins
(Eclipse, Visual Studio,
SAM)
Monitoring and logging
• Metrics for requests,
errors, and throttles
• Built-in logs to Amazon
CloudWatch Logs
Programming model
• Use processes, threads,
/tmp, sockets normally
• AWS SDK built in
(Python and Node.js)
Stateless
• Persist data using
external storage
• No affinity or access to
underlying infrastructure
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Lambda Pricing
• Buy compute time in
100 ms increments
• Low request charge
• No hourly, daily, or
monthly minimums
• No per-device fees
Never pay for idle!
Free Tier
1 million requests and 400,000 GB-secs of
compute every month, every customer
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What can make migrating to / adopting AWS
Serverless easier?
ü Cost effective
ü Secure
ü Low maintenance
q Reuse existing code and frameworks
q Leverage existing developer skills
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Serverless adopt decision
Lambda Function Serverless Container
Too complexity ecosystems
Frequent API call
Huge ISV Solutions
Newly creation services
Dividable Applications
Acceptable Method invoke times
Legacy Java EE
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Comparison between Lambda & Container App
Runs on Lambda Function Runs on Container
Pay by Memory size * Execution time Stable pricing model
DDoS make you pay
Computing cost
DDoS make you pay
Infra cost
Scaling with limitation(soft & hard) Scaling by metrics/self
Consider more Cloud Native Nature fit-in Legacy
Request/method throttling control Self control
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda Function Lifecycle
© 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();
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Most Concern - Summary
“Pay as you go”
“Execution Time – Cold Start time”
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
“How to get good Cold Start time ”
Re the Legacy Spring App
© 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 (such as
Tomcat)
• Open source via awslabs, Apache 2.0 license
• https://github.com/awslabs/aws-serverless-java-container
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Serverless Java Container – How does it work?
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)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Java Container for Spring-boot
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Java Container for SparkJava + Guice
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Number matters …
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Run time binding vs Compile time binding
Spring Boot
SparkJava Guice
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
How much effort?
• Just make sure your code is AWS Lambda
friendly
• Create the infrastructure (e.g. using AWS
SAM)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
SparkJava Lambda Handler
public class SparkStreamLambdaHandler implements RequestStreamHandler {
private static SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SparkLambdaContainerHandler.getAwsProxyHandler();
SparkResourceRoute.defineResources();
Spark.awaitInitialization();
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spark container", e);
}
}
public SparkStreamLambdaHandler() {
// we enable the timer for debugging. This SHOULD NOT be enabled in production.
Timer.enable();
}
@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.
Guice DI configuration
public class ClinicModule extends AbstractModule {
@Override
protected void configure(){
/**
*
private PetRepository petRepository;
private VetRepository vetRepository;
private OwnerRepository ownerRepository;
private VisitRepository visitRepository;
private SpecialtyRepository specialtyRepository;
private PetTypeRepository petTypeRepository;
*/
bind(OwnerRepository.class).to(JpaOwnerRepositoryImpl.class);
bind(PetRepository.class).to(JpaPetRepositoryImpl.class);
bind(VetRepository.class).to(JpaVetRepositoryImpl.class);
bind(VisitRepository.class).to(JpaVisitRepositoryImpl.class);
bind(SpecialtyRepository.class).to(JpaSpecialtyRepositoryImpl.class);
bind(PetTypeRepository.class).to(JpaPetTypeRepositoryImpl.class);
bind(ClinicService.class).to(ClinicServiceImpl.class);
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Replace Spring Data JPA by Guice Module
public class DatabaseModule extends AbstractModule {
@Override
protected void configure() {
install(new JpaPersistModule("demo"));
bind(JPAInitializer.class).asEagerSingleton();
}
@Singleton
public static class JPAInitializer {
@Inject
public JPAInitializer(final PersistService service) {
service.start();
}
}
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Define Route entry point
public class SparkResourceRoute {
private static final Logger logger = LoggerFactory.getLogger(SparkResourceRoute.class);
public static void defineResources() {
Injector injector = Guice.createInjector(new JpaPersistModule("demo"),new
ClinicModule());
injector.getInstance(DatabaseModule.JPAInitializer.class);
// rest controller routing
OwnerRestController ownerRestController =
injector.getInstance(OwnerRestController.class);
//@RequestMapping(value = "", method = RequestMethod.GET, produces =
MediaType.APPLICATION_JSON_UTF8_VALUE)
get("/owners", (req, res) -> {
Collection<Owner> owners = ownerRestController.getOwners(() -> res.status(404));
res.status(200);
return owners;
}, new JsonTransformer());
}
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Could be more better
• Load/Include less libs/classes
• Check by Java Agent
• Using Dagger2 not Guice
• Lighter, faster
• Using AWS SDK for Java v2
• Excluded unnecessary Deps
• Fixed on the URLConnection client
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Making your existing Java code AWS Lambda Friendly
Difference What to avoid Possible Solution
No sticky sessions /
session replication
Avoid HttpSession variables Store state in (DynamoDB / RDS)
15 minute timeout Avoid long blocking requests
Call another Lambda asynchronously.
Use an appropriate service for long running tasks
Container freezes on
handler return
Avoid background task
execution and scheduling
Same as above +
CloudWatch Events Scheduled Rules for
scheduling
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
NEW
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Making your existing Java code AWS Lambda Friendly
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.
Making your existing Java code AWS Lambda Friendly
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.
Testing
ü Pure Unit tests – no significant change
ü MockMvc – no significant change
ü Selenium / UI tests – no significant change
• Full integration tests without deploying to Lambda:
• AWS SAM CLI when possible
• Build as a regular jar file (requires some setup effort)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Deployment
AWS Serverless Application Model (SAM)
CloudFormation extension optimized for serverless
(Infrastructure as Code)
Resource types: functions, APIs, and tables
Supports anything CloudFormation supports
Open specification (Apache 2.0)
https://github.com/awslabs/serverless-application-model
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
SAM template
AWSTemplateFormatVersion: '2010-09-09’
Transform: AWS::Serverless-2016-10-31
Resources:
GetHtmlFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/todo_list.zip
Handler: index.gethtml
Runtime: nodejs4.3
Policies: AmazonDynamoDBReadOnlyAccess
Events:
GetHtml:
Type: Api
Properties:
Path: /{proxy+}
Method: ANY
ListTable:
Type: AWS::Serverless::SimpleTable
Tells CloudFormation this is a SAM
template it needs to “transform”
Creates a Lambda function with the
referenced managed IAM policy,
runtime, code at the referenced zip
location, and handler as defined.
Also creates an API Gateway and
takes care of all
mapping/permissions necessary
Creates a DynamoDB table with 5
Read & Write units
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
“Lambda goes well, how about container”
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Container related offering
Amazon ECS
(available now)
Amazon EKS
(preview)
Fargate mode for
ECS
(available now)
Fargate mode
for EKS
(available
2018)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
ECS
Easiest way to deploy and
manage containers
Integration with entire AWS platform
ALB, Auto Scaling, Batch, Elastic Beanstalk,
CloudFormation, CloudTrail, CloudWatch Events,
CloudWatch Logs, CloudWatch Metrics, ECR, EC2 Spot,
IAM, NLB, Parameter Store, and VPC
Scales to support clusters of any size
Service integrations (like ALB and NLB) are at
container level
1
2
3
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
EKS
Managed Kubernetes on AWS
Highly available Automated
version upgrades
Integration with
other AWS
services
Etcd
Master
Managed
Kubernetes
control plane
CloudTrail, CloudWatch,
ELB, IAM, VPC,
PrivateLink
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AMAZON CONTAINER SERVICES
So you want to run a (managed) container on AWS
Choose your orchestration tool1
Choose your launch type2
ECS EKS
EC2 Fargate EC2 Fargate
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
FIN, ACK
It’s never been easier to build and launch APIs!
Serverless APIs:
• No management of servers
• Pay for what you use and not for idle resources!
• Instantly scale up without turning any knobs or provisioning any resources
• Tooling to get started in minutes with incredibly minimal code needed
• Built in high availability built into multiple places in the application stack
• Authentication and Authorization built into multiple places in the application stack
InternetMobile/Web
apps
AWS
Databases/
Data stores
API Gateway AWS Lambda
functions
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
All in Serverless
AWS : YikaiKao@amazon.com
Telegram : YikaiKao
WeChat : YikaiKao
Twitter : @YikaiKao
GitHub Repos
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Thank you!

More Related Content

PDF
Legacy java ee meet lambda
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
PPTX
Gluecon 2018 - The Best Practices and Hard Lessons Learned of Serverless Appl...
PDF
Serverless on AWS: Architectural Patterns and Best Practices
Legacy java ee meet lambda
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
Gluecon 2018 - The Best Practices and Hard Lessons Learned of Serverless Appl...
Serverless on AWS: Architectural Patterns and Best Practices

Similar to 2018 10-19-jc conf-embrace-legacy-java-ee-by-aws-serverless (7)

PDF
Serverless use cases with AWS Lambda - More Serverless Event
PPTX
AWS Startup Day - Boston 2018 - The Best Practices and Hard Lessons Learned o...
PDF
AWS SUMMIT TEL AVIV - 2018
PDF
The Best Practices and Hard Lessons Learned of Serverless Applications
PDF
CI/CD for AWS Lambda Projects - IsraelCloud Meetup
PPTX
The Serverless Tidal Wave - SwampUP 2018 Keynote
PDF
Wildrydes Serverless Workshop Tel Aviv
Serverless use cases with AWS Lambda - More Serverless Event
AWS Startup Day - Boston 2018 - The Best Practices and Hard Lessons Learned o...
AWS SUMMIT TEL AVIV - 2018
The Best Practices and Hard Lessons Learned of Serverless Applications
CI/CD for AWS Lambda Projects - IsraelCloud Meetup
The Serverless Tidal Wave - SwampUP 2018 Keynote
Wildrydes Serverless Workshop Tel Aviv
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
Essential Infomation Tech presentation.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Introduction to Artificial Intelligence
PDF
Digital Strategies for Manufacturing Companies
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
history of c programming in notes for students .pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Essential Infomation Tech presentation.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Choose the Right IT Partner for Your Business in Malaysia
Design an Analysis of Algorithms II-SECS-1021-03
Understanding Forklifts - TECH EHS Solution
Design an Analysis of Algorithms I-SECS-1021-03
How to Migrate SBCGlobal Email to Yahoo Easily
PTS Company Brochure 2025 (1).pdf.......
Introduction to Artificial Intelligence
Digital Strategies for Manufacturing Companies
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
history of c programming in notes for students .pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
VVF-Customer-Presentation2025-Ver1.9.pptx
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf

2018 10-19-jc conf-embrace-legacy-java-ee-by-aws-serverless

  • 1. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 19 Oct, 2018 Kim Kao( ), Solutions Architect Embrace Legacy Java EE By AWS Serverless
  • 2. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Classic Java EE Core Pattern ü Tiering Paradigm ü Distributed System Design ü Spring framework Birth ü Beautiful methodologies age http://www.corej2eepatterns.com/
  • 3. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 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) Ecosystem is changing …
  • 4. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Old Soldiers never die
  • 5. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. “The cloud is the new normal”
  • 6. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. “All Things Distributed” “Design to Fail” “Redundant is hard” “No servers is easier to manage than No server”
  • 7. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS customers modernize applications one at a time Assess and prioritize, one app at a time Re-host (lift-and-shift) data center à EC2 Re-platform (lift-tinker-shift) VMs à containers Re-factor monolith à microservices Re-invent (cloud-native) new serverless microservices Determine modernization path
  • 8. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Why AWS for modern applications? Experience Partners Portfolio Customers Developer Community AWS frees innovators to innovate and fail without fear Any starting point Any application Anything is possible
  • 9. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Why migrate to Serverless?
  • 10. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Computing evolution – A paradigm shift FOCUS ON BUSINESS LOGIC LEVELOFABSTRACTION Virtual Servers in Datacenters Virtual Servers in Datacenters Physical Machines Virtual Machines Containerization Serverless • Continuous scaling • Fault tolerance built-in • Event-driven • Pay per usage • Zero maintenanceAWS Fargate Amazon API Gateway AWS Lambda AWS Step Function
  • 11. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. No servers to provision or manage Scales with usage Never pay for idle Availability and fault tolerance built in Serverless means…
  • 12. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. What does Fargate mean? Serverless Container Management. No worrying about scaling, service mesh, underlying infrastructure, cluster resources, capacity, setup. Just give it a task definition or pod (in 2018), set some resource limits, and away you go.
  • 13. © 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
  • 14. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Cost-effective and efficient No Infrastructure to manage Pay only for what you use Bring Your Own Code Productivity focused compute platform to build powerful, dynamic, modular applications in the cloud Run code in standard languages Focus on business logic Benefits of AWS Lambda 1 2 3
  • 15. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. How it works
  • 16. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Using AWS Lambda Bring your own code • Node.js, Java, Python, C# • Bring your own libraries (even native ones) Simple resource model • Select power rating from 128 MB to 1.5 GB • CPU and network allocated proportionately Flexible use • Synchronous or asynchronous • Integrated with other AWS services Flexible authorization • Securely grant access to resources and VPCs • Fine-grained control for invoking your functions
  • 17. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Using AWS Lambda Authoring functions • WYSIWYG editor or upload packaged .zip • Third-party plugins (Eclipse, Visual Studio, SAM) Monitoring and logging • Metrics for requests, errors, and throttles • Built-in logs to Amazon CloudWatch Logs Programming model • Use processes, threads, /tmp, sockets normally • AWS SDK built in (Python and Node.js) Stateless • Persist data using external storage • No affinity or access to underlying infrastructure
  • 18. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Lambda Pricing • Buy compute time in 100 ms increments • Low request charge • No hourly, daily, or monthly minimums • No per-device fees Never pay for idle! Free Tier 1 million requests and 400,000 GB-secs of compute every month, every customer
  • 19. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. What can make migrating to / adopting AWS Serverless easier? ü Cost effective ü Secure ü Low maintenance q Reuse existing code and frameworks q Leverage existing developer skills
  • 20. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless adopt decision Lambda Function Serverless Container Too complexity ecosystems Frequent API call Huge ISV Solutions Newly creation services Dividable Applications Acceptable Method invoke times Legacy Java EE
  • 21. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Comparison between Lambda & Container App Runs on Lambda Function Runs on Container Pay by Memory size * Execution time Stable pricing model DDoS make you pay Computing cost DDoS make you pay Infra cost Scaling with limitation(soft & hard) Scaling by metrics/self Consider more Cloud Native Nature fit-in Legacy Request/method throttling control Self control
  • 22. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda Function Lifecycle
  • 23. © 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();
  • 24. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Most Concern - Summary “Pay as you go” “Execution Time – Cold Start time”
  • 25. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. “How to get good Cold Start time ” Re the Legacy Spring App
  • 26. © 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 (such as Tomcat) • Open source via awslabs, Apache 2.0 license • https://github.com/awslabs/aws-serverless-java-container
  • 27. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless Java Container – How does it work? 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)
  • 28. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Java Container for Spring-boot
  • 29. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Java Container for SparkJava + Guice
  • 30. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Number matters …
  • 31. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Run time binding vs Compile time binding Spring Boot SparkJava Guice
  • 32. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. How much effort? • Just make sure your code is AWS Lambda friendly • Create the infrastructure (e.g. using AWS SAM)
  • 33. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. SparkJava Lambda Handler public class SparkStreamLambdaHandler implements RequestStreamHandler { private static SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler; static { try { handler = SparkLambdaContainerHandler.getAwsProxyHandler(); SparkResourceRoute.defineResources(); Spark.awaitInitialization(); } catch (ContainerInitializationException e) { // if we fail here. We re-throw the exception to force another cold start e.printStackTrace(); throw new RuntimeException("Could not initialize Spark container", e); } } public SparkStreamLambdaHandler() { // we enable the timer for debugging. This SHOULD NOT be enabled in production. Timer.enable(); } @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(); } }
  • 34. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Guice DI configuration public class ClinicModule extends AbstractModule { @Override protected void configure(){ /** * private PetRepository petRepository; private VetRepository vetRepository; private OwnerRepository ownerRepository; private VisitRepository visitRepository; private SpecialtyRepository specialtyRepository; private PetTypeRepository petTypeRepository; */ bind(OwnerRepository.class).to(JpaOwnerRepositoryImpl.class); bind(PetRepository.class).to(JpaPetRepositoryImpl.class); bind(VetRepository.class).to(JpaVetRepositoryImpl.class); bind(VisitRepository.class).to(JpaVisitRepositoryImpl.class); bind(SpecialtyRepository.class).to(JpaSpecialtyRepositoryImpl.class); bind(PetTypeRepository.class).to(JpaPetTypeRepositoryImpl.class); bind(ClinicService.class).to(ClinicServiceImpl.class); }
  • 35. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Replace Spring Data JPA by Guice Module public class DatabaseModule extends AbstractModule { @Override protected void configure() { install(new JpaPersistModule("demo")); bind(JPAInitializer.class).asEagerSingleton(); } @Singleton public static class JPAInitializer { @Inject public JPAInitializer(final PersistService service) { service.start(); } } }
  • 36. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Define Route entry point public class SparkResourceRoute { private static final Logger logger = LoggerFactory.getLogger(SparkResourceRoute.class); public static void defineResources() { Injector injector = Guice.createInjector(new JpaPersistModule("demo"),new ClinicModule()); injector.getInstance(DatabaseModule.JPAInitializer.class); // rest controller routing OwnerRestController ownerRestController = injector.getInstance(OwnerRestController.class); //@RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) get("/owners", (req, res) -> { Collection<Owner> owners = ownerRestController.getOwners(() -> res.status(404)); res.status(200); return owners; }, new JsonTransformer()); } }
  • 37. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Could be more better • Load/Include less libs/classes • Check by Java Agent • Using Dagger2 not Guice • Lighter, faster • Using AWS SDK for Java v2 • Excluded unnecessary Deps • Fixed on the URLConnection client
  • 38. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Making your existing Java code AWS Lambda Friendly Difference What to avoid Possible Solution No sticky sessions / session replication Avoid HttpSession variables Store state in (DynamoDB / RDS) 15 minute timeout Avoid long blocking requests Call another Lambda asynchronously. Use an appropriate service for long running tasks Container freezes on handler return Avoid background task execution and scheduling Same as above + CloudWatch Events Scheduled Rules for scheduling 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 NEW
  • 39. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Making your existing Java code AWS Lambda Friendly 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
  • 40. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Making your existing Java code AWS Lambda Friendly 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)
  • 41. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Testing ü Pure Unit tests – no significant change ü MockMvc – no significant change ü Selenium / UI tests – no significant change • Full integration tests without deploying to Lambda: • AWS SAM CLI when possible • Build as a regular jar file (requires some setup effort)
  • 42. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Deployment AWS Serverless Application Model (SAM) CloudFormation extension optimized for serverless (Infrastructure as Code) Resource types: functions, APIs, and tables Supports anything CloudFormation supports Open specification (Apache 2.0) https://github.com/awslabs/serverless-application-model
  • 43. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. SAM template AWSTemplateFormatVersion: '2010-09-09’ Transform: AWS::Serverless-2016-10-31 Resources: GetHtmlFunction: Type: AWS::Serverless::Function Properties: CodeUri: s3://sam-demo-bucket/todo_list.zip Handler: index.gethtml Runtime: nodejs4.3 Policies: AmazonDynamoDBReadOnlyAccess Events: GetHtml: Type: Api Properties: Path: /{proxy+} Method: ANY ListTable: Type: AWS::Serverless::SimpleTable Tells CloudFormation this is a SAM template it needs to “transform” Creates a Lambda function with the referenced managed IAM policy, runtime, code at the referenced zip location, and handler as defined. Also creates an API Gateway and takes care of all mapping/permissions necessary Creates a DynamoDB table with 5 Read & Write units
  • 44. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. “Lambda goes well, how about container”
  • 45. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Container related offering Amazon ECS (available now) Amazon EKS (preview) Fargate mode for ECS (available now) Fargate mode for EKS (available 2018)
  • 46. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. ECS Easiest way to deploy and manage containers Integration with entire AWS platform ALB, Auto Scaling, Batch, Elastic Beanstalk, CloudFormation, CloudTrail, CloudWatch Events, CloudWatch Logs, CloudWatch Metrics, ECR, EC2 Spot, IAM, NLB, Parameter Store, and VPC Scales to support clusters of any size Service integrations (like ALB and NLB) are at container level 1 2 3
  • 47. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. EKS Managed Kubernetes on AWS Highly available Automated version upgrades Integration with other AWS services Etcd Master Managed Kubernetes control plane CloudTrail, CloudWatch, ELB, IAM, VPC, PrivateLink
  • 48. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AMAZON CONTAINER SERVICES So you want to run a (managed) container on AWS Choose your orchestration tool1 Choose your launch type2 ECS EKS EC2 Fargate EC2 Fargate
  • 49. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. FIN, ACK It’s never been easier to build and launch APIs! Serverless APIs: • No management of servers • Pay for what you use and not for idle resources! • Instantly scale up without turning any knobs or provisioning any resources • Tooling to get started in minutes with incredibly minimal code needed • Built in high availability built into multiple places in the application stack • Authentication and Authorization built into multiple places in the application stack InternetMobile/Web apps AWS Databases/ Data stores API Gateway AWS Lambda functions
  • 50. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. All in Serverless AWS : YikaiKao@amazon.com Telegram : YikaiKao WeChat : YikaiKao Twitter : @YikaiKao GitHub Repos
  • 51. © 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Thank you!