Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Do more with less code in Serverless

Do more with less code in Serverless

re:Invent 2020 session explaining how to leverage serverless services to reduce the amount of code and number of lambda functions

Jérôme Van Der Linden

December 22, 2020
Tweet

More Decks by Jérôme Van Der Linden

Other Decks in Technology

Transcript

  1. © 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
  2. © 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
  3. 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
  4. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Single Responsibility Principle
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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" ] } }
  11. 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…
  12. 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
  13. 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
  14. 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
  15. { "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
  16. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. You Ain’t Gonna Need It
  17. © 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
  18. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. "No function is easier to manage than 'no function' " Me Solutions Architect AWS
  19. Amazon API Gateway Service Proxy Amazon API Gateway Amazon DynamoDB

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

    https://velocity.apache.org/engine/devel/vtl-reference.html
  21. 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
  22. 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
  23. 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
  24. © 2020, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Keep it Simple Stupid aka Conclusion
  25. © 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
  26. 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
  27. 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
  28. Keep it… or leave it is not always needed You

    can save costs and optimize performance when functions are just passthrough or mapping
  29. Thank you! © 2020, Amazon Web Services, Inc. or its

    affiliates. All rights reserved. Jérôme Van Der Linden AWS Solutions Architect