Share
Embed
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
Transcript
41 slides
-
© 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 -
© 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 -
© 2020, Amazon Web Services, Inc. or its affiliates. All
rights reserved. Clean Code -
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 -
© 2020, Amazon Web Services, Inc. or its affiliates. All
rights reserved. Single Responsibility Principle -
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 -
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 -
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 -
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 -
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 -
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" ] } } -
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… -
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 -
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 -
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 -
{ "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 -
© 2020, Amazon Web Services, Inc. or its affiliates. All
rights reserved. You Ain’t Gonna Need It -
© 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 -
© 2020, Amazon Web Services, Inc. or its affiliates. All
rights reserved. "No function is easier to manage than 'no function' " Me Solutions Architect AWS -
Common serverless architecture Amazon API Gateway Amazon DynamoDB AWS Lambda
// Validate input // CRUD // (return) -
Functionless architecture Amazon API Gateway Amazon DynamoDB // Validate input
// CRUD // (return) -
Amazon API Gateway Service Proxy Amazon API Gateway Amazon DynamoDB
https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-aws-proxy.html -
Amazon API Gateway Service Proxy Amazon API Gateway Amazon DynamoDB
https://velocity.apache.org/engine/devel/vtl-reference.html -
Functionless architecture Amazon API Gateway Amazon DynamoDB // Validate input
// CRUD // (return) -
Functionless architecture AWS AppSync Amazon DynamoDB Amazon Aurora Serverless Amazon
Elasticsearch Service -
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 -
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 -
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 -
© 2020, Amazon Web Services, Inc. or its affiliates. All
rights reserved. Keep it Simple Stupid aka Conclusion -
© 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 -
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 -
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 -
Keep it… or leave it is not always needed You
can save costs and optimize performance when functions are just passthrough or mapping -
Thank you! © 2020, Amazon Web Services, Inc. or its
affiliates. All rights reserved. Jérôme Van Der Linden AWS Solutions Architect -
© 2020, Amazon Web Services, Inc. or its affiliates. All
rights reserved.