Slide 1

Slide 1 text

© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. Do more with less code in Serverless Jérôme Van Der Linden Solutions Architect AWS S V S 3 5 2 - G

Slide 2

Slide 2 text

© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. Clean Code Single Responsibility Principle You Ain’t Gonna Need It Keep It Simple Stupid Agenda

Slide 3

Slide 3 text

© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. Clean Code

Slide 4

Slide 4 text

Software Craftsmanship & Clean Code

Slide 5

Slide 5 text

Software Craftsmanship & Clean Code

Slide 6

Slide 6 text

SOLID - Single Responsibility Principle YAGNI - You Ain’t Gonna Need It KISS - Keep it Simple Stupid Some clean code principles https://www.amazon.es/Wenger-19201-Navaja-suiza/dp/B000R0JDSI

Slide 7

Slide 7 text

© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. Single Responsibility Principle

Slide 8

Slide 8 text

Single Responsibility Principle Do’s Don’ts * - Input validation - Business logic - Transform data - Return result - Event/Input Filtering - Transport data - Orchestration & long transactions * Most of the time

Slide 9

Slide 9 text

Single Responsibility Principle Do’s Don’ts * - Input validation - Business logic - Transform data - Return result - Event/Input Filtering - Transport data - Orchestration & long transactions * Most of the time

Slide 10

Slide 10 text

Serverless is not only AWS Lambda COMPUTE DATA STORES INTEGRATION Amazon DynamoDB Amazon Aurora Serverless Amazon S3 AWS Lambda AWS Fargate Amazon EventBridge Amazon API Gateway Amazon SQS Amazon SNS AWS AppSync AWS Step Functions

Slide 11

Slide 11 text

Event Filtering – the wrong way SNS Topic Publisher if event_type == 'order_created’: lambda_client.invoke('OrderCreation', ...) elif event_type == 'order_placed’: lambda_client.invoke('OrderPlacement', ...) elif event_type == 'order_cancelled’: lambda_client.invoke('OrderCancelation', ...) OrderCreation OrderPlacement OrderCancellation { "event_type": "order_placed", "order": { "id": "232134", "amount": "2341,45", "stock_ref": "AMZN” } } function code Input event OrderFiltering

Slide 12

Slide 12 text

Event Filtering with Amazon SNS SNS Topic Publisher OrderCreation OrderPlacement OrderCancellation OrderCreationEvent: Type: AWS::SNS::Subscription Properties: TopicArn: 'arn:aws:sns:eu-central-1:123456789:OrdersTopic' Protocol: lambda Endpoint: 'arn:aws:lambda:eu-central-1:123456789:function:OrderCreation' FilterPolicy: event_type: - order_created { "event_type": [ "order_created" ] } Filter policy Cloudformation template https://docs.aws.amazon.com/sns/latest/dg/sns-subscription-filter-policies.html

Slide 13

Slide 13 text

Event Filtering with Amazon EventBridge https://docs.aws.amazon.com/eventbridge/latest/userguide/content-filtering-with-event-patterns.html { "Source": "custom.myOrderapp", "EventBusName": "default", "DetailType": "order", "Time": "Wed Jan 29 2020 08:03:18 GMT-0500", "Detail":{ "event_type": "order_created", "order": { "id": "232134", "amount": "2341.45", "stock_ref": "AMZN", "quantity": "3", "result": "approved" } } } EventBridge Event Bus Rules Targets { "source": [ "custom.myOrderapp" ], "detail-type": [ "order" ], "detail": { "event_type": [ "order_created" ], "amount": [ { "numeric": [ ">", 2000 ] } ], "result": [ "approved" ] } }

Slide 14

Slide 14 text

Orchestration – the wrong way invoke invoke if (… ) invoke // else if (success) Notify with SNS n * invoke if (failure) Enqueue error notify with SNS need approval App…

Slide 15

Slide 15 text

Simple orchestration with Lambda destinations invoke invoke if (… ) invoke // else if (success) Notify with SNS n * invoke if (failure) Enqueue error notify with SNS need approval App… Lambda Destinations https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html#invocation-async-destinations

Slide 16

Slide 16 text

Simple orchestration with Lambda destinations Amazon SNS Amazon EventBridge Amazon Cloudwatch Logs Amazon S3 Amazon SES AWS Config Amazon CloudFormation AWS CodeCommit A S Y N C "DestinationConfig": { "onSuccess": { "Destination": "arn:aws:sns:..." }, "onFailure": { "Destination": "arn:aws:sqs:..." } } Cloudformation template Amazon SNS Amazon EventBridge Amazon SQS AWS Lambda if success: return {...} else: raise Exception(‘Failure', {...}) function code Lambda function A S Y N C

Slide 17

Slide 17 text

Advanced orchestration with AWS Step Functions invoke invoke if (… ) invoke // else if (success) Notify with SNS n * invoke if (failure) Enqueue error notify with SNS need approval App… https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html

Slide 18

Slide 18 text

{ "StartAt": "SimpleInvocation", "States": { "SimpleInvocation": { "Type": "Task", "Resource": "arn:aws:lambda:eu-central-1:123456789 "Next": "Choose1or2" }, "Choose1or2": { "Type": "Choice", "Choices": [ { "Variable": "$.foo", "NumericEquals": 1, "Next": "Lambda1" }, { "Variable": "$.foo", "NumericEquals": 2, "Next": "ParallelInvocation" } ], "Default": "Unmatched" }, "Lambda1": { "Type": "Task", "Resource": "arn:aws:lambda:eu-central-1:123456789 Advanced orchestration with AWS Step Functions

Slide 19

Slide 19 text

© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. You Ain’t Gonna Need It

Slide 20

Slide 20 text

© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. "No server is easier to manage than 'no server' " Werner Vogels VP & CTO Amazon.com

Slide 21

Slide 21 text

© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. "No function is easier to manage than 'no function' " Me Solutions Architect AWS

Slide 22

Slide 22 text

Common architecture API Database Backend

Slide 23

Slide 23 text

Common serverless architecture Amazon API Gateway Amazon DynamoDB AWS Lambda

Slide 24

Slide 24 text

Common serverless architecture Amazon API Gateway Amazon DynamoDB AWS Lambda // Validate input // CRUD // (return)

Slide 25

Slide 25 text

Functionless architecture Amazon API Gateway Amazon DynamoDB // Validate input // CRUD // (return)

Slide 26

Slide 26 text

Amazon API Gateway Service Proxy Amazon API Gateway Amazon DynamoDB https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-aws-proxy.html

Slide 27

Slide 27 text

Amazon API Gateway Service Proxy Amazon API Gateway Amazon DynamoDB https://velocity.apache.org/engine/devel/vtl-reference.html

Slide 28

Slide 28 text

Amazon API Gateway Request Validation https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-request-validation.html Amazon API Gateway Amazon DynamoDB

Slide 29

Slide 29 text

Amazon API Gateway Request Validation https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-request-validation.html Amazon API Gateway Amazon DynamoDB

Slide 30

Slide 30 text

Functionless architecture Amazon API Gateway Amazon DynamoDB // Validate input // CRUD // (return)

Slide 31

Slide 31 text

Functionless architecture AWS AppSync Amazon DynamoDB Amazon Aurora Serverless Amazon Elasticsearch Service

Slide 32

Slide 32 text

AWS Step Functions integrations AWS Lambda AWS Batch Amazon DynamoDB Amazon ECS / Fargate Amazon EMR AWS Glue Amazon SageMaker Amazon SNS Amazon SQS AWS Step Functions AWS CodeBuild https://docs.aws.amazon.com/step-functions/latest/dg/concepts-service-integrations.html

Slide 33

Slide 33 text

AWS Step Functions integrations "QueueError": { "Type": "Task", "Resource": "arn:aws:states:::sqs:sendMessage", "Parameters": { "QueueUrl": "https://sqs.eu-central-1.amazonaws.com/123456789012/myQueue", "MessageBody.$": "$.input.message" }, "End": true }, "SendNotification": { "Type": "Task", "Resource": "arn:aws:states:::sns:publish", "Parameters": { "TopicARN": "arn:aws:sns:eu-central- 1:123456789012:myTopic", "Subject": "Lambda1 has successfully finish its job", "Message.$" : "$.input.message" }, "End": true }, SQS SNS

Slide 34

Slide 34 text

Amazon EventBridge integrations https://docs.aws.amazon.com/eventbridge/latest/userguide/eventbridge-targets.html EventBridge Event Bus Rules Targets AWS Batch Amazon EC2 Instance Amazon ECS Task AWS Step Functions Amazon SNS Amazon SQS Amazon Kinesis Data Streams Amazon Kinesis Data Firehose Amazon EventBridge AWS CodeBuild AWS CodePipeline Amazon API Gateway SSM Run Command SSM Automation Amazon Redshift

Slide 35

Slide 35 text

© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. Keep it Simple Stupid aka Conclusion

Slide 36

Slide 36 text

© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” Tony Hoare Former Professor at Oxford

Slide 37

Slide 37 text

Keep it Simple Simpler functions = Easier to test and maintain Faster to start: less cold start More scalable: higher throughput More secure: less permission needed

Slide 38

Slide 38 text

Keep your code focused on the business Keep it Stupid Business code is the clever part of the function Orchestration is not the responsibility of a Clever

Slide 39

Slide 39 text

Keep it… or leave it is not always needed You can save costs and optimize performance when functions are just passthrough or mapping

Slide 40

Slide 40 text

Thank you! © 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved. Jérôme Van Der Linden AWS Solutions Architect

Slide 41

Slide 41 text

© 2020, Amazon Web Services, Inc. or its affiliates. All rights reserved.