$30 off During Our Annual Pro Sale. View Details »

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

    View Slide

  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

    View Slide

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

    View Slide

  4. Software Craftsmanship & Clean Code

    View Slide

  5. Software Craftsmanship & Clean Code

    View Slide

  6. 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

    View Slide

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

    View Slide

  8. 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

    View Slide

  9. 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

    View Slide

  10. 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

    View Slide

  11. 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

    View Slide

  12. 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

    View Slide

  13. 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" ]
    }
    }

    View Slide

  14. 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…

    View Slide

  15. 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

    View Slide

  16. 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

    View Slide

  17. 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

    View Slide

  18. {
    "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

    View Slide

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

    View Slide

  20. © 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

    View Slide

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

    View Slide

  22. Common architecture
    API Database
    Backend

    View Slide

  23. Common serverless architecture
    Amazon
    API Gateway
    Amazon
    DynamoDB
    AWS
    Lambda

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  32. 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

    View Slide

  33. 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

    View Slide

  34. 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

    View Slide

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

    View Slide

  36. © 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

    View Slide

  37. 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

    View Slide

  38. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide