Slide 1

Slide 1 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Java on AWS Lambda: Differences and challenges Jerome Van Der Linden Sr Solutions Architect Builder @AWS 23/05/2023

Slide 2

Slide 2 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Difference #1: Scope of responsibility

Slide 3

Slide 3 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. A standard Serverful Java / Spring Application Client Application Load Balancer Instances HTTP server Spring Framework Application code Spring Boot application Dispatcher Servlet Handler Mapping Handler Adapter Controller Service Repository You take care of + networking + security + autoscaling + multi-AZ + patching + upgrades + …

Slide 4

Slide 4 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. No infrastructure provisioning, no management Automatic scaling Pay for value Highly available and secure What is serverless?

Slide 5

Slide 5 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda F U L L Y M A N A G E D S E R V E R L E S S C O M P U T E Auto Scaling group Availability Zone A Availability Zone B Availability Zone C Region Instances Instances Instances AWS Lambda ≈ Load Balancer EC2 Instance FireCracker MicroVM FireCracker MicroVM FireCracker MicroVM FireCracker MicroVM

Slide 6

Slide 6 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda F U L L Y M A N A G E D S E R V E R L E S S C O M P U T E Auto Scaling group Availability Zone A Availability Zone B Availability Zone C Region Instances Instances Instances AWS Lambda ≈ Load Balancer EC2 Instance FireCracker MicroVM Execution Environment Language Runtime Application code You take care of FireCracker MicroVM FireCracker MicroVM FireCracker MicroVM FireCracker MicroVM

Slide 7

Slide 7 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Difference #2: Execution model

Slide 8

Slide 8 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. A standard Serverful Java / Spring Application Client Application Load Balancer Instances HTTP server Spring Framework Application code Spring Boot application Dispatcher Servlet Handler Mapping Handler Adapter Controller Service Repository Always running Always accepting requests

Slide 9

Slide 9 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda Auto Scaling group Availability Zone A Availability Zone B Availability Zone C Region Instances Instances Instances AWS Lambda ≈ Load Balancer EC2 Instance FireCracker MicroVM Execution Environment Language Runtime Application code FireCracker MicroVM FireCracker MicroVM FireCracker MicroVM FireCracker MicroVM NOT always running Accept 1 request at a time

Slide 10

Slide 10 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda E X E C U T I O N E N V I R O N M E N T S Time 1 Initialization Execution Env. #1

Slide 11

Slide 11 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda E X E C U T I O N E N V I R O N M E N T S Time 1 Initialization Execution Execution environment is blocked or busy for this entire time Env. #1

Slide 12

Slide 12 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda E X E C U T I O N E N V I R O N M E N T S Time 1 Initialization Execution 2 Initialization Execution Env. #1 Env. #2 3 Initialization Execution Env. #3

Slide 13

Slide 13 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda E X E C U T I O N E N V I R O N M E N T S Time 1 Initialization Execution 2 Initialization Execution Env. #1 Env. #2 3 Initialization Execution Env. #3 Execution 4

Slide 14

Slide 14 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Difference #3: Invocation model

Slide 15

Slide 15 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda E V E N T - D R I V E N F U N C T I O N S Event source Services (anything) Database AWS Service 3rd party service Function Changes in data state Changes in resource state Request to endpoints Node.js Python C# Go Ruby Runtime API 8 / 11 / 17

Slide 16

Slide 16 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda E V E N T - D R I V E N F U N C T I O N S Event source Services(anything) Function com.amazonaws:aws-lambda-java-core:1.2.2 com.amazonaws:aws-lambda-java-events:3.11.1 software.amazon.awssdk:dynamodb:2.20.56 software.amazon.awssdk:s3:2.20.56 software.amazon.awssdk:* Or any Java library you’d like to use

Slide 17

Slide 17 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda A N A T O M Y O F A F U N C T I O N public class App implements RequestHandler{ public Response handleRequest(final Event event, final Context context) { // do some stuff return response; } } • Handler: function that will be triggered for each event received • Event: the incoming event that triggered the function (in JSON format) • Context: informations about the function configuration and invocation • Your code, anything, … • Input type • Output type

Slide 18

Slide 18 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda C O M M O N U S E C A S E S Amazon API Gateway AWS Lambda AWS Lambda AWS AppSync APIs / microservices (REST / GraphQL) AWS Lambda AWS Step Functions Orchestration AWS Lambda AWS Config Operations/ Remediation AWS Lambda Amazon SNS AWS Lambda AWS Lambda Amazon EventBridge Amazon SQS Event-driven architectures File processing Stream processing Data processing (transformation , cleansing, …) Analytics IoT backend …

Slide 19

Slide 19 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda C O M M O N U S E C A S E : H A N D L I N G H T T P / R E S T R E Q U E S T S Amazon API Gateway AWS Lambda public class App implements RequestHandler { public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { String name = input.getQueryStringParameters().get("name"); Map headers = new HashMap<>(); headers.put("Content-Type", "application/json"); APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() .withHeaders(headers); try { String output = String.format("{ \"message\": \"hello %s\" }", name); return response .withStatusCode(200) .withBody(output); } catch (IOException e) { return response .withBody("{}") .withStatusCode(500); } } }

Slide 20

Slide 20 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Difference #4: Development model

Slide 21

Slide 21 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Single-purpose functions do “x” do “y” do “z” • Benefits: • Easier to debug, trace errors • Enforce least privilege, one role per function • Simpler to test • Favor reusability • Less surface area • Tips: • Use naming conventions and tags to enhance discoverability • Don’t go nano

Slide 22

Slide 22 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Testing • Unit / Integration tests with aws-lambda-java-tests @ParameterizedTest @Event(value = "sqs/sqs_event.json", type = SQSEvent.class) public void testInjectSQSEvent(SQSEvent event) { // test your handleRequest method with this event as parameter } • Local invocation with SAM: sam local invoke MyFunction --event local-event.json

Slide 23

Slide 23 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Difference #5: Deployment model

Slide 24

Slide 24 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda Supports either Zip or Jar format • Gradle: using Zip build type • Maven: using Shade plugin / Assembly plugin or Dependency plugin P A C K A G I N G https://docs.aws.amazon.com/lambda/latest/dg/java-package.html

Slide 25

Slide 25 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda Supports container images • AWS Images for Lambda (lambda/java) • Bring your own image (ex if you want Java 20) • Run and test images locally P A C K A G I N G $ docker build -t docker-image:test . $ docker run –p 9000:8080 docker-image:test $ curl –XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}' https://docs.aws.amazon.com/lambda/latest/dg/java-image.html

Slide 26

Slide 26 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda • Using the AWS CLI: aws lambda create-function ... D E P L O Y M E N T

Slide 27

Slide 27 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda • Using the AWS CLI: aws lambda create-function ... • Using Infrastructure as code: § AWS SAM D E P L O Y M E N T https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html

Slide 28

Slide 28 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda • Using the AWS CLI • Using Infrastructure as code: § AWS SAM § AWS CDK D E P L O Y M E N T https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html

Slide 29

Slide 29 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda • Using the AWS CLI • Using Infrastructure as code: § AWS SAM § AWS CDK § Terraform § … D E P L O Y M E N T https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function

Slide 30

Slide 30 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Challenge #1: Lambda Limits

Slide 31

Slide 31 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Soft Limits C A N B E I N C R E A S E D Concurrency = (average requests per second) * (average request duration in seconds) è (average requests per second) = Concurrency / (average request duration in seconds) Ex for a function that takes 250ms: 1000 / 0.25 ≈ 4000 req / sec https://docs.aws.amazon.com/lambda/latest/dg/lambda-concurrency.html

Slide 32

Slide 32 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Hard limits AWS Step Functions Amazon S3 Container image Amazon EFS (NFS) No GPU

Slide 33

Slide 33 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Challenge #2: Cold Starts

Slide 34

Slide 34 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda I N I T I A L I Z A T I O N O F T H E E X E C U T I O N E N V I R O N M E N T Cold start Warm start Time 1 Initialization Execution Env. #1 Execution 4

Slide 35

Slide 35 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda I N I T I A L I Z A T I O N O F T H E E X E C U T I O N E N V I R O N M E N T Time 1 Initialization Execution Env. #1 Execution 4 Create Env Download code Start Runtime Initialize Function You can optimize this AWS optimizes this

Slide 36

Slide 36 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda I N I T I A L I Z A T I O N O F T H E E X E C U T I O N E N V I R O N M E N T Time 1 Initialization Execution Env. #1 Execution 4 Create Env Download code Start Runtime Initialize Function You can optimize this

Slide 37

Slide 37 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Reducing Cold Starts § Use AWS Java SDK v2 and use only the required dependencies (s3, dynamodb, …) § Remove unnecessary jar dependencies § Use lighweight dependencies (jackson-jr vs jackson, slf4j simple logger vs log4j, …) R E D U C E A S M U C H A S P O S S I B L E T H E S I Z E O F T H E P A C K A G E

Slide 38

Slide 38 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda I N I T I A L I Z A T I O N O F T H E E X E C U T I O N E N V I R O N M E N T Time 1 Initialization Execution Env. #1 Execution 4 Create Env Download code Start Runtime Initialize Function You can optimize this

Slide 39

Slide 39 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Reducing Cold Starts Use the AWS CRT HTTP Client • faster SDK startup time • smaller memory footprint • reduced latency time U S E T H E A W S C R T H T T P C L I E N T software.amazon.awssdk aws-crt-client S3AsyncClient .builder() .httpClientBuilder( AwsCrtAsyncHttpClient.builder().maxConcurrency(100)) .build(); https://aws.amazon.com/blogs/developer/announcing-availability-of-the-aws-crt-http-client-in-the-aws-sdk-for-java-2-x/

Slide 40

Slide 40 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Reducing Cold Starts § Objects that’s expected to be reused across multiple invocations should move its respective initialization code to either the static initializer or constructor. ⚠ Don’t store secrets that must not be reused § When using the AWS SDK, pre-configure specific items during client initialization. For example: region, the credentials provider, and endpoint. I N I T I A L I Z E B E F O R E T H E H A N D L E R

Slide 41

Slide 41 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Reducing Cold Starts § C1 is a lightweight & fast compiler (no profiling) § C2 is using the profiler to detect “hot” code and produce native code, optimised, but it’s heavier and slower – Also in the Lambda context (not always running), it’s not very useful § Set Tiered Compilation level to 1 with an environment variable U S E T I E R E D C O M P I L A T I O N JAVA_TOOL_OPTIONS=-XX:+TieredCompilation -XX:TieredStopAtLevel=1 https://aws.amazon.com/blogs/compute/increasing-performance-of-java-aws-lambda-functions-using-tiered-compilation/

Slide 42

Slide 42 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Reducing Cold Starts • Avoid runtime dependency injection & reflection as much as possible (eg. Spring, Spring Boot) • Use compile-time dependency injection frameworks (eg. Dagger 2, Micronaut, Quarkus). A V O I D D I A N D R E F L E C T I O N https://github.com/aws-samples/serverless-java-frameworks-samples

Slide 43

Slide 43 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Reducing Cold Starts Compile your application ahead-of-time (AOT) and create Native Images with • https://www.graalvmonlambda.com/ • Requires a custom Lambda Runtime • May not work for all Java applications B U I L D N A T I V E I M A G E S 1 10 100 1000 10000 100000 1 2 3 4 5 6 7 8 9 10 Iteration JVM Native image Same Lambda function with Quarkus invocation savings of more than 95%. https://aws.amazon.com/blogs/architecture/field-notes-optimize-your-java-application-for-aws-lambda-with-quarkus/

Slide 44

Slide 44 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Reducing Cold Starts Right size the memory with Lambda Power Tuning A D J U S T M E M O R Y ( A N D C P U ) https://github.com/alexcasalboni/aws-lambda-power-tuning & use multi-threading

Slide 45

Slide 45 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda I N I T I A L I Z A T I O N O F T H E E X E C U T I O N E N V I R O N M E N T Time 1 Initialization Execution Env. #1 Execution 4 Create Env Download code Start Runtime Initialize Function AWS optimizes this

Slide 46

Slide 46 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Reducing Cold Starts Use Provisioned Concurrency to warm-up ahead of time. Especially important if you’re expecting a big spike in traffic on a short amount of time. P R O V I S I O N E D C O N C U R R E N C Y https://docs.aws.amazon.com/lambda/latest/dg/provisioned-concurrency.html

Slide 47

Slide 47 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Reducing Cold Starts S N A P S T A R T Code execution Invoke Resume Resume Snapshot Post Snapshot Hook (optional) Pre Snapshot Hook (optional) Create Execution Environment Init during deployment Download Code Start Runtime Initialize Function Code Create Snapshot first request https://aws.amazon.com/blogs/compute/starting-up-faster-with-aws-lambda-snapstart/

Slide 48

Slide 48 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Going further…

Slide 49

Slide 49 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Want to try? W O R K S H O P J A V A O N A W S L A M B D A https://s12d.com/java-workshop

Slide 50

Slide 50 text

© 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. Thank you! Jerome Van Der Linden @jeromevdl