Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Serverless Architecture - A Gentle Overview

2,841 views

Published on

This presentation provides a gentle overview of serverless architecture. Specific focus is on AWS lambda.

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Serverless Architecture - A Gentle Overview

  1. 1. Serverless Architecture: Overview Ganesh Samarthyam ganesh@codeops.tech Organizer: India Serverless Summit www.inserverless.com
  2. 2. Are you dealing with web-servers, app- servers, session and entity beans, …: in short, are you a stone-age programmer?
  3. 3. Greek characters are scary! The term “lambda” originates from the “lambda calculus” (yes, yes, that same scary math stuff!)
  4. 4. He he, but lambdas are fun, not scary
  5. 5. def main(args): name = args.get("name", "stranger") greeting = "Hello " + name + "!" print(greeting) return {"greeting": greeting} Lambda functions! import com.amazonaws.services.lambda.runtime.Context; public class helloworld { public String handleRequest(String name, Context context) { String greetings = "Hello " + name; context.getLogger().log("Name: " + name); System.out.println(greetings); return greetings; } } function main() { return {payload: 'Hello world'}; } Python Java JavaScript (Node.JS)
  6. 6. But what are lambdas?
  7. 7. Lambdas is just a fancy name for stateless functions without side- effects!
  8. 8. Evolution: Servers to serverless Code Runtime OS Hardware Code Runtime OS Hardware Code Runtime OS Hardware Code Runtime OS Hardware Physical Servers Virtual Machines Containers Serverless
  9. 9. Evolution: Servers to serverless Slow-iteration and deployment Faster-iteration and deployment Fastest-iteration and deployment Rapid iteration and deployment Single tenency Multi-tenency Super multi- tenancy Extreme multi- tenancy Unfriendly for polyglots Somewhat friendly for polyglots Friendly for polyglots Very friendly for polyglots Deploy in weeks Deploy in minutes Deploy in seconds Deploy independently Typically alive for years Typically alive for weeks Typically alive for hours Typically alive for seconds Physical Servers Virtual Machines Containers Serverless Source: https://www.slideshare.net/AmazonWebServices/getting-started-with-serverless-architectures-63429092
  10. 10. What is server-“lesssss…"?
  11. 11. What is serverless? “Serverless architectures refer to applications that significantly depend on third-party services (knows as Backend as a Service or "BaaS") or on custom code that's run in ephemeral containers (Function as a Service or “FaaS”)” - Martin Fowler
  12. 12. – Serverless for Dummies “Serverless: just put your code in cloud and run it”
  13. 13. What is serverless? ❖ Serverless is often referred to as FaaS - Function as a Service. Gartner refers to it as fPaaS - function Platform as a Service. ❖ The model in serverless architecture is this: a distributed system that reacts to events or process workloads dynamically based on demand by spinning up ephemeral (short-lived) containers or computational resources in the cloud.
  14. 14. Serverless - example
  15. 15. On the term “lambda”
  16. 16. On the term “lambda” ❖ The term “lambda” originates from the “lambda calculus”. In 1936, Alonzo Church developed a logic system that was later adopted for computation. ❖ Most languages (including Java, C# and C++) support lambda functions today. ❖ In languages supporting lambda functions, it is an unnamed function that takes input coming variables and returns a value. ❖ A salient characteristic of lambda functions is the lack of side-effects.
  17. 17. Look ma, no servers!
  18. 18. Unix pipes - 1973!
  19. 19. – Doug McIlroy “Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.”
  20. 20. But, but, “Serverless != No servers” Lambdas are executed in servers in cloud - so we still have servers! What serverless means is you don't have to care about servers: no provisioning, maintenance, etc.
  21. 21. Why serverless? – Google Cloud Functions https://cloud.google.com/functions/ “Serve users from zero to planet-scale, all without managing any infrastructure.”
  22. 22. – Gartner "By 2022, most platform as a service (PaaS) offerings will evolve to a fundamentally serverless model, rendering the cloud platform architectures dominating in 2017 as legacy architectures" 
  23. 23. No server upgrades
  24. 24. No provisioning
  25. 25. No procurement approvals
  26. 26. Serverless - characteristics granular payment event-driven self-managed scale on demand
  27. 27. Self-managed
  28. 28. Scale-on-demand
  29. 29. Real pay-as-you-go model
  30. 30. Event driven style
  31. 31. Serverless - some use cases mobile back-ends Real-time analytics data processing static-websites live-video stream processing chatbots
  32. 32. Data processing source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-getting-started-with-serverless-architectures-cmp211
  33. 33. Static websites source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-getting-started-with-serverless-architectures-cmp211
  34. 34. Mobile backends source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-getting-started-with-serverless-architectures-cmp211
  35. 35. Real-time analytics source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-getting-started-with-serverless-architectures-cmp211
  36. 36. Live video-steam processing source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-getting-started-with-serverless-architectures-cmp211
  37. 37. Use case #1: web applications source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-serverless-architectural-patterns-and-best-practices-arc402
  38. 38. Use case #2: batch processing source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-serverless-architectural-patterns-and-best-practices-arc402
  39. 39. Use case #3: stream processing source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-serverless-architectural-patterns-and-best-practices-arc402
  40. 40. Lambda architecture style source: https://www.slideshare.net/AmazonWebServices/aws-reinvent-2016-serverless-architectural-patterns-and-best-practices-arc402
  41. 41. Towards LessOps model? Serverless frameworks move us towards LessOps model - meaning no or few operators are required for monitoring and maintaining the servers and infrastructure.
  42. 42. – Dr Werner Vogels (Amazon CTO) "What we’ve seen is a revolution where complete applications are being stripped of all their servers, and only code is being run. Quite a few companies are ripping out big pieces of their applications and replacing their servers, their VMs and their containers with just code… Perhaps we no longer have to think about servers!”
  43. 43. Popular technologies AWS Lambda Google Cloud Functions IBM/Adobe/ Apache OpenWhisk Azure Functions
  44. 44. Five principles for serverless applications Use a compute service to execute code on demand (no servers) Write single-purpose stateless functions Design push-based, event-driven pipelines Create thicker, more powerful front ends Embrace third-party services Source: Peter Sbarski, “Serverless Architectures on AWS”, Manning, 2017
  45. 45. Using AWS Lambda
  46. 46. AWS Lambda From https://aws.amazon.com/
  47. 47. AWS Lambda ❖ Lambda is one of the earliest technologies for serverless (13 Nov 2014). It is also the most mature and stable platform. ❖ http://aws.amazon.com/lambda ❖ Currently supported (as on June 2017) - .NET Core 1.01. (C#), Java 8, Node.js 4.3, Node.js 6.10.2, Python 2.7 and 3.6.
  48. 48. Three ways to create a function ❖ There are three ways to create the same lambda function: ❖ AWS Lambda console (web UI) ❖ AWS CLI (from your commandline) ❖ AWS SDK
  49. 49. Execution Environment ❖ Public Amazon Linux AMI version (AMI name: amzn-ami- hvm-2016.03.3.x86_64-gp2) ❖ Linux kernel version – 4.4.51-40.60.amzn1.x86_64 ❖ When creating native (64-bit) binaries for executing in Lambda, this information is important - so that code can be compiled to this target ❖ The following libraries are available in execution environment ❖ AWS SDK – AWS SDK for JavaScript version 2.45.0 ❖ AWS SDK for Python (Boto 3) version 1.4.4, Botocore version 1.5.43 ❖ Amazon Linux build of java-1.8.0-openjdk for Java Source: http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
  50. 50. A simple lambda $ cat hello.py def lambda_handler(event, context): print "hello srushit" return 'Hello from srushit' $ zip -r hello-world hello.py $ aws lambda create-function --region us-east-1 --function-name FirstPythonLambda --zip-file fileb:///Users/gsamarthyam/hello-world.zip -- role arn:aws:iam::431635030606:role/LambdaExecuteRole --runtime python2.7 --handler hello::lambda_handler Notes: (a) Once you upload the function from command-line, it takes time to see the updated source code from the AWS console online. (b) In Python, print statements and Logger functions in the logging module are logged - you can check them in CloudWatch logs.
  51. 51. A hello world lambda in Java $ cat Hello.java import com.amazonaws.services.lambda.runtime.Context; public class Hello { public String handleRequest(String name, Context context) { return "Hello " + name; } } http://docs.aws.amazon.com/lambda/latest/dg/java-programming-model.html
  52. 52. A simple lambda hello::handleRequest Input string “Ganesh” Context object Result string “Hello Ganesh” AWS Lambda Execution on a Linux container
  53. 53. Step #1 $ javac -cp ./lib/aws-lambda-java-core-1.0.0.jar:./lib/aws-lambda-java-events-1.0.0.jar Hello.java $ Need to provide the dependencies (Context class in this case) to compile it; dependency details available from: https://github.com/ aws/aws-lambda-java-libs (“Official mirror for interface definitions and helper classes for Java code running on the AWS Lambda platform.”)
  54. 54. Step #2 $ zip -r Hello.zip Hello.class lib/aws-lambda-java-core-1.0.0.jar lib/aws-lambda-java- events-1.0.0.jar adding: Hello.class (deflated 35%) adding: lib/aws-lambda-java-core-1.0.0.jar (deflated 39%) adding: lib/aws-lambda-java-events-1.0.0.jar (deflated 27%) $ ls -lh Hello.zip -rw-r--r-- 1 gsamarthyam staff 14K Jun 19 15:01 Hello.zip $ ls -lh lib -rw-r--r-- 1 gsamarthyam staff 29K Jun 23 16:12 aws-lambda-java-core-1.0.0.jar -rw-r--r--@ 1 gsamarthyam staff 11K Jun 18 16:44 aws-lambda-java-events-1.0.0.jar Create a zip file with the dependencies - in this case, it is just a couple of standard dependencies
  55. 55. Step #3 $ aws lambda create-function --region us-east-1 --function-name FirstJavaLambda --zip-file fileb:///Users/ gsamarthyam/serverless/Hello.zip --role arn:aws:iam::431635030606:role/LambdaExecuteRole --handler Hello::handleRequest --runtime java8 { "TracingConfig": { "Mode": "PassThrough" }, "CodeSha256": "DpRB5zFF1ORVw6tTSIMprA1BgGNmpf+oGBFBy8/APWo=", "FunctionName": "FirstJavaLambda", "CodeSize": 13988, "MemorySize": 128, "FunctionArn": "arn:aws:lambda:us-east-1:431635030606:function:FirstJavaLambda", "Version": "$LATEST", "Role": "arn:aws:iam::431635030606:role/LambdaExecuteRole", "Timeout": 3, "LastModified": "2017-06-23T10:50:02.582+0000", "Handler": "Hello::handleRequest", "Runtime": "java8", "Description": "" }
  56. 56. Step #4 $ aws lambda invoke --invocation-type RequestResponse --function-name FirstJavaLambda --region us-west-2 --log-type Tail --payload '"Ganesh"' outputfile.txt { "LogResult": "U1RBUlQgUmVxdWVzdElkOiBmYzM2N2I2NC01ODAxLTExZTctYjllOS05OTQzNmU4ODEyZmY gVmVyc2lvbjogJExBVEVTVApsb2cgZGF0YSBmcm9tIHN0ZG91dCAKIHRoaXMgaXMgY29udGl udWF0aW9uIG9mIHN5c3RlbS5vdXQKbG9nIGRhdGEgZnJvbSBzdGRlcnIgCiB0aGlzIGlzIGNvbn RpbnVhdGlvbiBvZiBzeXN0ZW0uZXJyCmxvZyBkYXRhIGZyb20gTGFtYmRhTG9nZ2VyIAogdGh pcyBpcyBjb250aW51YXRpb24gb2YgbG9nZ2VyLmxvZ0VORCBSZXF1ZXN0SWQ6IGZjMzY3YjY 0LTU4MDEtMTFlNy1iOWU5LTk5NDM2ZTg4MTJmZgpSRVBPUlQgUmVxdWVzdElkOiBmYzM2 N2I2NC01ODAxLTExZTctYjllOS05OTQzNmU4ODEyZmYJRHVyYXRpb246IDI2MC45MyBtcwlC aWxsZWQgRHVyYXRpb246IDMwMCBtcyAJTWVtb3J5IFNpemU6IDEyOCBNQglNYXggTWVtb3 J5IFVzZWQ6IDQwIE1CCQo=", "StatusCode": 200 } $ cat outputfile.txt "Hello Ganesh. log stream = 2017/06/23/[$LATEST]680db95163b346579ce89f51535db346"
  57. 57. Step #4 $ echo "U1RBUlQgUmVxdWVzdElkOiBmYzM2N2I2NC01ODAxLTExZTctYjllOS05OTQzNmU4ODEyZm YgVmVyc2lvbjogJExBVEVTVApsb2cgZGF0YSBmcm9tIHN0ZG91dCAKIHRoaXMgaXMgY29ud GludWF0aW9uIG9mIHN5c3RlbS5vdXQKbG9nIGRhdGEgZnJvbSBzdGRlcnIgCiB0aGlzIGlzIGNv bnRpbnVhdGlvbiBvZiBzeXN0ZW0uZXJyCmxvZyBkYXRhIGZyb20gTGFtYmRhTG9nZ2VyIAogd GhpcyBpcyBjb250aW51YXRpb24gb2YgbG9nZ2VyLmxvZ0VORCBSZXF1ZXN0SWQ6IGZjMzY3Y jY0LTU4MDEtMTFlNy1iOWU5LTk5NDM2ZTg4MTJmZgpSRVBPUlQgUmVxdWVzdElkOiBmY zM2N2I2NC01ODAxLTExZTctYjllOS05OTQzNmU4ODEyZmYJRHVyYXRpb246IDI2MC45MyBt cwlCaWxsZWQgRHVyYXRpb246IDMwMCBtcyAJTWVtb3J5IFNpemU6IDEyOCBNQglNYXggT WVtb3J5IFVzZWQ6IDQwIE1CCQo=" | base64 --decode START RequestId: fc367b64-5801-11e7-b9e9-99436e8812ff Version: $LATEST log data from stdout this is continuation of system.out log data from stderr this is continuation of system.err log data from LambdaLogger this is continuation of logger.logEND RequestId: fc367b64-5801-11e7-b9e9-99436e8812ff REPORT RequestId: fc367b64-5801-11e7-b9e9-99436e8812ff Duration: 260.93 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 40 MB
  58. 58. Step #4 $ aws lambda get-function --function-name FirstJavaLambda { "Code": { "RepositoryType": "S3", "Location": "https://prod-04-2014-tasks.s3.amazonaws.com/snapshots/431635030606/FirstJavaLambda-961856c8-8808-4d77-aa26- bde4c62745d9?X-Amz-Security- Token=FQoDYXdzENP%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaDJIA1ZghlrarR%2FS8sCK3A9ic4dl6r2QeYmybaLJ%2FR4VxX3e%2BN8413pI vMLfpND96fpnK5g55zaaI4nQkYcW70zwZu9mZ3%2FXrQqObP1PKpY1BBr0uP7BrDn8wF062XuBQG4AMaoxBQ44cSvUC8cr%2FGVU2MpAzFEK0 0kgOtI9fax%2FB6eAt5xzKN2Zt4SpCtFktfaNTq15Vxm2RIk8s30Yw53jte7fPPtaUZPJZT4%2F872zNrylxSfKAfRdOvrv4%2FvGur21evlliheJ0mriQZ NBcRkRsNWIC6MkG48inkiRL4AjWN9BNWXl5JhpPxgHfZTEnb2st1SJuuJdx3SyWs2u8Wh8J%2BV90eyuZSqDeXXRQCmk4QNhSalvLboN6M9oTC VBh7uMdsLDkxje3FOlRdDYMJW9kcyMc%2BcqUOeeeO46m7ky0t%2BKtvDrY887jjziG3PTQjZrFPwmiEHgVImzhfa9RN8eAXb3xcnPqC97rzOM1GZ kAD%2BuJbECu%2B1t7H93rhG5ObQZVBT4llxBq%2BQXpI0FHjqpVE7wOs7hEuqws65gXxVOltBVHv7TwsTIxibH88uai%2BpyRV%2BC6NUK%2FpL QZDF8wCqozh%2B%2FHvmAo2smzygU%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20170623T110025Z&X-Amz- SignedHeaders=host&X-Amz-Expires=600&X-Amz-Credential=ASIAIHFSB5YQLFLCC34Q%2F20170623%2Fus-east-1%2Fs3%2Faws4_request&X- Amz-Signature=af988a347e62ebf6fe89d3593b68ada63be6bcd74f6b12222b96b762c7204924" }, "Configuration": { "TracingConfig": { "Mode": "PassThrough" }, "Version": "$LATEST", "CodeSha256": "DpRB5zFF1ORVw6tTSIMprA1BgGNmpf+oGBFBy8/APWo=", "FunctionName": "FirstJavaLambda", "MemorySize": 128, "CodeSize": 13988, "FunctionArn": "arn:aws:lambda:us-east-1:431635030606:function:FirstJavaLambda", "Handler": "Hello::handleRequest", "Role": "arn:aws:iam::431635030606:role/LambdaExecuteRole", "Timeout": 3, "LastModified": "2017-06-23T10:50:02.582+0000", "Runtime": "java8", "Description": "" } }
  59. 59. How to update the function? $ aws lambda update-function-code --function-name arn:aws:lambda:us- east-1:431635030606:function:FirstJavaLambda --zip-file fileb://Hello.zip { "TracingConfig": { "Mode": "PassThrough" }, "CodeSha256": "DpRB5zFF1ORVw6tTSIMprA1BgGNmpf+oGBFBy8/APWo=", "FunctionName": "FirstJavaLambda", "CodeSize": 13988, "MemorySize": 128, "FunctionArn": "arn:aws:lambda:us-east-1:431635030606:function:FirstJavaLambda", "Version": "$LATEST", "Role": "arn:aws:iam::431635030606:role/LambdaExecuteRole", "Timeout": 3, "LastModified": "2017-06-23T13:34:05.408+0000", "Handler": "Hello::handleRequest", "Runtime": "java8", "Description": "" }
  60. 60. How to delete the function? $ aws lambda delete-function --function-name FirstJavaLambda $ aws lambda list-functions | grep FirstJavaLambda $ You can check that once the lambda is deleted, the list-functions doesn’t list it anymore
  61. 61. Available commands in “aws lambda” • add-permission • create-alias • create-event-source-mapping • create-function • delete-alias • delete-event-source-mapping • delete-function • get-account-settings • get-alias • get-event-source-mapping • get-function • get-function-configuration • get-policy • help • invoke • invoke-async • list-aliases • list-event-source-mappings • list-functions • list-tags • list-versions-by-function • publish-version • remove-permission • tag-resource • untag-resource • update-alias • update-event-source-mapping • update-function-code • update-function-configuration
  62. 62. Using AWS SDK to create lambdas $ cat testing.py def lambda_handler(event, context): return 'Hello from Lambda' $ cat invokelambda.py import boto3 import json client = boto3.client('lambda') response = client.invoke( FunctionName='arn:aws:lambda:us-east-1:431635030606:function:testing', InvocationType='RequestResponse', LogType='Tail', Payload=json.dumps({}) ) result = response['Payload'].read() print(result) $ python invokelambda.py "Hello from Lambda" $
  63. 63. Kinds of lambda invocations • Event based lambdas are invoked based on triggers; example: when a file is dropped in a S3 bucket, a lambda may be invoked • Async in nature • No response sent back to the invoker Event RequestResponse • Synchronous - when invoked, it executes and waits till completion and returns something to the invoker • Happens when invoked through API gateway, CLI or AWS console
  64. 64. Specifying invocation types in CLI Help description for —invocation-type option for “aws lambda invoke” from AWS CLI
  65. 65. Using base64 ❖ Sometimes it is convenient to use base64 whenever you want to send data to the lambda or return from the lambda, or get that data again as context. this avoids adding unicode characters etc. ❖ Logs in lambdas often use base64. You can use the “base64 --decode” command (mac/linux) to read the log file
  66. 66. Using base64 - example $ cat logresult.txt { "LogResult": "U1RBUlQgUmVxdWVzdElkOiAyZGRmMjQ3Ni01ODE3LTExZTctYWI4YS02ZGQwZWMxYTFlYmMgVmVyc2lvbjogJExBVEVTVApsb2c gZGF0YSBmcm9tIHN0ZG91dCAKIHRoaXMgaXMgY29udGludWF0aW9uIG9mIHN5c3RlbS5vdXQKbG9nIGRhdGEgZnJvbSBzdGRlcnI gCiB0aGlzIGlzIGNvbnRpbnVhdGlvbiBvZiBzeXN0ZW0uZXJyCmxvZyBkYXRhIGZyb20gTGFtYmRhTG9nZ2VyIAogdGhpcyBpcyB jb250aW51YXRpb24gb2YgbG9nZ2VyLmxvZ0VORCBSZXF1ZXN0SWQ6IDJkZGYyNDc2LTU4MTctMTFlNy1hYjhhLTZkZDBlYzFhMWV iYwpSRVBPUlQgUmVxdWVzdElkOiAyZGRmMjQ3Ni01ODE3LTExZTctYWI4YS02ZGQwZWMxYTFlYmMJRHVyYXRpb246IDUuMDIgbXM JQmlsbGVkIER1cmF0aW9uOiAxMDAgbXMgCU1lbW9yeSBTaXplOiAxMjggTUIJTWF4IE1lbW9yeSBVc2VkOiA0NiBNQgkK", "StatusCode": 200 } $ echo "U1RBUlQgUmVxdWVzdElkOiAyZGRmMjQ3Ni01ODE3LTExZTctYWI4YS02ZGQwZWMxYTFlYmMgVmVyc2lvbjogJExBVEVTVApsb2c gZGF0YSBmcm9tIHN0ZG91dCAKIHRoaXMgaXMgY29udGludWF0aW9uIG9mIHN5c3RlbS5vdXQKbG9nIGRhdGEgZnJvbSBzdGRlcnI gCiB0aGlzIGlzIGNvbnRpbnVhdGlvbiBvZiBzeXN0ZW0uZXJyCmxvZyBkYXRhIGZyb20gTGFtYmRhTG9nZ2VyIAogdGhpcyBpcyB jb250aW51YXRpb24gb2YgbG9nZ2VyLmxvZ0VORCBSZXF1ZXN0SWQ6IDJkZGYyNDc2LTU4MTctMTFlNy1hYjhhLTZkZDBlYzFhMWV iYwpSRVBPUlQgUmVxdWVzdElkOiAyZGRmMjQ3Ni01ODE3LTExZTctYWI4YS02ZGQwZWMxYTFlYmMJRHVyYXRpb246IDUuMDIgbXM JQmlsbGVkIER1cmF0aW9uOiAxMDAgbXMgCU1lbW9yeSBTaXplOiAxMjggTUIJTWF4IE1lbW9yeSBVc2VkOiA0NiBNQgkK" | base64 --decode START RequestId: 2ddf2476-5817-11e7-ab8a-6dd0ec1a1ebc Version: $LATEST log data from stdout this is continuation of system.out log data from stderr this is continuation of system.err log data from LambdaLogger this is continuation of logger.logEND RequestId: 2ddf2476-5817-11e7-ab8a-6dd0ec1a1ebc REPORT RequestId: 2ddf2476-5817-11e7-ab8a-6dd0ec1a1ebcDuration: 5.02 msBilled Duration: 100 ms Memory Size: 128 MB Max Memory Used: 46 MB $
  67. 67. Understanding the “Context” object $ cat ContextInfo.java import com.amazonaws.services.lambda.runtime.Context; public class ContextInfo { public Context details(Object input, Context context) { System.out.println("Memory limit in MBs: " + context.getMemoryLimitInMB()); System.out.println("Function name: " + context.getFunctionName()); System.out.println("AWS Request ID: " + context.getAwsRequestId()); System.out.println("Log Stream Name: " + context.getLogStreamName()); System.out.println("Log Group Name: " + context.getLogGroupName()); System.out.println("Client Context: " + context.getClientContext()); System.out.println("Identity: " + context.getIdentity()); System.out.println("Remaining time (in milli seconds): "+ context.getRemainingTimeInMillis()); return context; } }
  68. 68. Compiling and running the lambda $ cat lamdafy.sh #!/bin/bash set -x #echo on rm logresult.txt rm outputfile.txt javac -cp ./lib/aws-lambda-java-core-1.0.0.jar:./lib/aws-lambda-java-events-1.0.0.jar ContextInfo.java zip -r ContextInfo.zip lib/aws-lambda-java-core-1.0.0.jar lib/aws-lambda-java-events-1.0.0.jar ContextInfo.class aws s3 cp ContextInfo.zip s3://myplasticbucketforlambda aws lambda update-function-code --function-name ContextInfoLambda --s3-bucket myplasticbucketforlambda --s3-key ContextInfo.zip aws lambda invoke --invocation-type RequestResponse --function-name ContextInfoLambda --region us-east-1 --log-type Tail --payload file://inputfile.txt outputfile.txt >> logresult.txt jq -r .LogResult logresult.txt | base64 --decode cat outputfile.txt echo "" jq . outputfile.txt
  69. 69. Understanding the “Context” object Memory limit in MBs: 128 Function name: ContextInfoLambda AWS Request ID: 32320904-5805-11e7-8904-ebe1abfa66b8 Log Stream Name: 2017/06/23/[$LATEST]acf60f48492b4df59ef8ebe534fffe00 Log Group Name: /aws/lambda/ContextInfoLambda Client Context: null Identity: lambdainternal.api.LambdaCognitoIdentity@589838eb Remaining time (in milli seconds): 2778
  70. 70. Understanding the “Context” object getFunctionName() Name of the lambda function being executed (e.g., FirstJavaLambda) getFunctionVersion() Version of the function (or its alias if it points to an alias) getInvokedFunctionArn() Get the ARN (Amazon Resource Name), for example, arn:aws:lambda:us-west-2:431635030606:function:HelloWorld getLogStreamName() Returns the CloudWatch log stream name getLogger() Returns the logger associated with the given context object getMemoryLimitInMB() Amount of memory set as limit when creating the lambda function (value returned in MBs) getRemainingTimeInMillis() Returns the amount of time remaining for execution (based on the time limit set during creating the function)
  71. 71. Watching CloudWatch logs To watch with CloudWatch, create a new role with following accesses: arn:aws:iam::aws:policy/AWSLambdaExecute arn:aws:iam::aws:policy/AWSLambdaFullAccess Streams stdout and stderr are redirected to CloudWatch logs; can be viewed from AWS console
  72. 72. Using the /tmp directory ❖ Applications can use “/tmp” directory ❖ If the underlying container gets recycled, the files in the “/tmp” directory will be gone! ❖ Also, /tmp is limited to 500 MB size - that is a limiting factor for many kinds of applications. ❖ For example, if we want to convert a video from one format to another, then this limit matters.
  73. 73. Dealing with code dependencies ❖ External libraries and dependencies must be bundled with the lambda function as zip files (or jar files depending on the context) ❖ There is limitation to the size of the dependencies that we can upload, currently it is 30MB. ❖ That is quite small for real-world requirement where the dependent libraries can be large. ❖ A workaround for this limitation is to upload it into S3 bucket, put it in to /tmp directory for the lambda and then use it.
  74. 74. Using lambdas with AWS services ❖ For real-world uses, its practically not feasible to use lambda without the AWS eco-system: ❖ S3 can be used for processing images, converting document formats, etc from the lambdas. Large dependencies for the lambda can be stored in S3 and processed from the lambdas ❖ SNS (Simple Notification Service) can be used for event processing ❖ DynamoDB can be used for persistence and data processing ❖ Alexa can be used for automated voice responses with Amazon Echo for example ❖ Lex can be used for chatbots with lambda as the back-end code
  75. 75. How does a lambda terminate? ❖ How does a lambda terminate? (A) Timeout - once the given maximum duration is reached, the function will terminate (B) Normal termination - returning from the lambda - when the function returns, the lambda terminates (C)Abnormal termination - forcefully terminating the lambda with calling functions (e.g. process.exit() in nodejs)
  76. 76. Lambda resource limits Source: http://docs.aws.amazon.com/lambda/latest/dg/limits.html
  77. 77. Lambda deployment size limits Source: http://docs.aws.amazon.com/lambda/latest/dg/limits.html
  78. 78. AWS Serverless Stack ❖ API Gateway ❖ Lambda ❖ Greengrass ❖ Step Functions ❖ SQS ❖ DynamoDB ❖ …
  79. 79. Costing aspects ❖ There are two factors for pricing calculation 1. Request costs: refers to number of executions - it is calculated at 100ms slices 2. Execution costs: these are variable costs; change the memory size and the execution cost will be calculated accordingly ❖ There is no cost for idle time ❖ You can use the online pricing calculator to estimate how much it may cost you: https://s3.amazonaws.com/lambda-tools/ pricing-calculator.html
  80. 80. Pricing calculation example
  81. 81. Pricing calculation example
  82. 82. Costing aspects ❖ In AWS lambda, the increase in the memory is associated with increased CPU and network I/O speeds. ❖ In free tier, it makes sense to increase it to the max MBs. ❖ Note that the cost is not based on actual consumption, but on the max. cap. ❖ Lots of calls execute only for short duration, like 30 ms. However billing is done for 100ms minimum. Hence, one way is to design applications that make fewer but longer calls.
  83. 83. Serverless != Always Lower Costs! ❖ It is a myth that adopting serverless will always reduce your costs. It is NOT true. The key in cost savings is in how you utilise serverless. For example, serverless is suitable if you have spiky workloads (or idle scenarios). It is NOT suitable in scenarios where your demand is stable.
  84. 84. How does AWS Lambda work?
  85. 85. Internally, it uses containers! ❖ Internally, AWS Lambda uses Linux Containers ❖ When a lambda function is invoked, it launches a container (with the provided config settings) ❖ When a function is called for the first time (or after it is updated), spawning the container adds some latency ❖ Container is terminated and is “frozen” for reuse ❖ If called again “soon”, it is “thawed” and invoked again Based on: http://docs.aws.amazon.com/lambda/latest/dg/lambda-introduction.html
  86. 86. Freeze/Thaw cycle freeze/thaw cycle - how the execution gets frozen up and is warmed up again
  87. 87. Freeze/Thaw cycle freeze/thaw cycle - how the execution gets frozen up and is warmed up again Execution Terminate Freeze Thaw Cold start
  88. 88. How AWS Lambda works ❖ Handlers are executed on workers. Time for starting-up of a worker is around 1 to 2 seconds. ❖ In case of a “load burst”, the Lambda load balancer sends them to new workers - that will incur latencies. ❖ For a container to execute the handler immediately, it must be in running state. ❖ However, if handlers are called only once in a while, then the container goes to paused or stopped state and it takes time to get back to running state again.
  89. 89. Cold vs. warm invocation ❖ Cold invocation - requires warm-up time (e.g., when starting for the first time) - the latency may be perceivable to the users ❖ Actions like code redeployment or making configuration changes can force redeployment of the lambda that make it cold invocation ❖ Warm invocation - lambda already deployed and ready to serve as soon as it is triggered or called
  90. 90. How to keep your container “warm”? ❖ You can use a “warming trigger” to ensure that the lambda function doesn’t go cold. Trigger it within every few minutes. ❖ Schedule CloudWatch events to fire every few minutes to keep the container “warm” ❖ In serverless framework, there is a plugin that keeps the lambda warm by sending an event every 5 minutes or so: https:// github.com/FidelLimited/ serverless-plugin-warmup
  91. 91. Best Practice Don’t depend on container reuse, but make best use of it Example #1: copy files from s3 and use it from /tmp, but check if the files in /tmp exist before trying to access them! Example #2: Instantiate AWS client, establish database connects, etc outside the handler - they will not be initialised again if container is reused
  92. 92. How about other languages? ❖ Lambda currently supports limited set of languages (JavaScript - with Node.js, Python, Java and C#) ❖ What if you want to use some other language, say Go? ❖ You can still run Go by following a few steps: ❖ http://blog.0x82.com/2014/11/24/aws-lambda- functions-in-go/ ❖ https://serifandsemaphore.io/go-amazon- lambda-7e95a147cec8
  93. 93. Security ❖ IAM (Identity & Access Management) provides the security for your lambdas ❖ Specific accesses are often required; for example, to watch lambda logs with CloudWatch, create a new role with following accesses: ❖ arn:aws:iam::aws:policy/AWSLambdaExecute ❖ arn:aws:iam::aws:policy/AWSLambdaFullAccess ❖ Due to security and legal aspects, serverless functions may need to run privately. In this case, lambda functions can be executed on AWS VPC (Virtual Private Cloud) inside a private network.
  94. 94. Using “aws iam” - example $ aws iam list-roles | jq -r .Roles[].Arn arn:aws:iam::431635030606:role/helloworld-dev arn:aws:iam::431635030606:role/service-role/javatesting arn:aws:iam::431635030606:role/LambdaExecuteRole $ aws iam list-roles | jq -r .Roles[].Arn | grep ExecuteRole arn:aws:iam::431635030606:role/LambdaExecuteRole
  95. 95. Serverless != Always Lower Costs! ❖ It is a myth that adopting serverless will always reduce your costs. It is NOT true. The key in cost savings is in how you utilise serverless. For example, serverless is suitable if you have spiky workloads (or idle scenarios). It is NOT suitable in scenarios where your demand is stable.
  96. 96. Related topics
  97. 97. How to use physical servers?
  98. 98. What’s not suitable ❖ Extremely long running jobs ❖ Jobs that require extensive communication between each other ❖ Constant loads and well-known costs ❖ Responses with very low-latency
  99. 99. Rearchitecting to serverless functions ❖ Moving to serverless is not as easy as flipping a switch: for existing applications, it may even require re-architecting/re- writing. ❖ This is because serverless computation architecture model is quite different from the way traditionally software is written and deployed.
  100. 100. Costing in Google Cloud Functions * Includes both Background and HTTP Functions. Source: https://cloud.google.com/functions/
  101. 101. So, what’s the catch? ❖ Runtime and resource limitations (memory, dependency sizes) ❖ Vendor lock-in (because non- trivial lambdas will use services from providers) ❖ Lambdas are still low-level “primitives” ❖ Tooling yet to mature (for testing, deploying, monitoring, …) ❖ …
  102. 102. Serverless looks awesome - let’s explore!
  103. 103. Links for serverless technologies ❖ AWS Lambda: https://aws.amazon.com/lambda/ ❖ Azure Functions: https://functions.azure.com/ ❖ Google Cloud Functions: https://cloud.google.com/functions/   ❖ Apache OpenWhisk: https://github.com/openwhisk ❖ Serverless framework: https://github.com/serverless/serverless ❖ Fission: https://github.com/fission/fission ❖ Hyper.sh: https://github.com/hyperhq / ❖ Funktion: https://funktion.fabric8.io/ ❖ Kubeless: http://kubeless.io/  
  104. 104. Tools to Explore
  105. 105. Tools to simplify using AWS Lambda ❖ Most frameworks available today in serverless domain try to simplify AWS! Here is a partial list (all of the for AWS): ❖ Chalice (Python) ❖ ClaudiaJS (NodeJS) ❖ Dawson (Node) ❖ Lambada Framework (Java) ❖ Shep (Node) ❖ Sparta (Go) ❖ Zappa (Python)
  106. 106. Node Lambda (for Node.js) ❖ This is a “command line tool to locally run and deploy your node.js application to Amazon Lambda” ❖ Why? Instead of testing your Lambda in AWS, you can write it locally and run it and test it; when everything is good, then deploy it in AWS Lambda ❖ URL: https://github.com/motdotla/node-lambda
  107. 107. ❖ “The Standard Library for Functions as a Service. Discover pre-built APIs, compose your own, build apps, and move your business faster than ever with new "server-less" technology.” ❖ Aims to be the “standard library for the web”! ❖ Check out https://stdlib.com/ and https:// github.com/stdlib/lib
  108. 108. Books to Read
  109. 109. Book Recommendations ❖ Has many interesting examples ❖ The supporting website has demos that you can try out: https://eventdrivenapps.com/ ❖ URL: https:// www.manning.com/books/ aws-lambda-in-action
  110. 110. Book Recommendations ❖ Focus on & examples using AWS Lambda ❖ Covers serverless patterns and architectures ❖ Written by Dr. Peter Sbarski, head of Serverlessconf ❖ Serverless Architectures on AWS, Peter Sbarski, Manning Publications, 376 pages, April 2017. URL: https://www.amazon.com/ Serverless-Architectures-AWS- examples-Lambda/dp/1617293822
  111. 111. Book Recommendations ❖ Serverless: Patterns of Modern Application Design Using Microservices (Amazon Web Services Edition), Obie Fernandez, LeanPub, 2016. https://leanpub.com/ serverless
  112. 112. Book Recommendations ❖ Comprehensive - free - guide on AWS Lambda is available here: http:// docs.aws.amazon.com/ lambda/latest/dg/lambda- dg.pdf
  113. 113. Links ❖ M. Fowler’s early article on serverless http:// martinfowler.com/articles/serverless.html ❖ AWS Lambda local runner: https://github.com/ cagataygurturk/aws-lambda-local-runner ❖ Java on AWS Lambda: https://www.infoq.com/news/ 2017/06/fearless-aws-lambda ❖ The twelve-factor app: https://12factor.net/
  114. 114. Video Resources ❖ Serverless Architectures: What, why, why not, and where next? - Mike Roberts ❖ Serverless: the Future of Software Architecture - Peter Sbarski ❖ Serverless computing options with Google Cloud Platform - Bret McGowen
  115. 115. Serverless Summit India’s first conference on serverless technologies!
  116. 116. Image Credits ❖ http://www.nag.co.za/wp-content/uploads/2013/04/Stone-Age-PC.jpg ❖ http://cdn2.hubspot.net/hub/148740/file-1853309085-jpg/RAM_-_stoneage.jpg ❖ https://upload.wikimedia.org/wikipedia/commons/thumb/9/97/WernerVogels.JPG/220px-WernerVogels.JPG ❖ https://image.slidesharecdn.com/devopssummit-openkeynote-noops-141105113330-conversion-gate01/95/noops-no- operations-2-638.jpg?cb=1420567266 ❖ http://f4.bcbits.com/img/a2377654591_10.jpg ❖ https://cdn.meme.am/cache/instances/folder981/500x/75497981/roll-safee-servers-cant-crash-if-there-are-no-servers.jpg ❖ https://www.commitstrip.com/wp-content/uploads/2017/04/Strip-Severless-650-finalenglishV2.jpg ❖ https://www.trovaweb.net/images/pizzeria-tavola-calda-barbecue-self-service-free-e-mangia-palermo/pizzeria-tavola-calda-barbecue- self-service-free-e-mangia-palermo-11.JPG ❖ http://2ndwatch.com/wp-content/uploads/2016/05/AWS-Lambda.png ❖ http://wendyonline.nl/wp-content/uploads/2015/11/eekhoorn-nootjes-1024x656.jpg ❖ https://innovationcentre.com.au/wp-content/uploads/2017/03/michievent.jpg ❖ http://orig00.deviantart.net/da2f/f/2013/034/f/4/alonzo_and_lambda_by_kinow-d5tqvau.png ❖ https://travistation.files.wordpress.com/2012/04/david-hasselhoff-as-michael-knight-in-knightrider-thumbs-up-263w_350h.jpg ❖ http://www.otona-beauty.com/wp-content/uploads/2015/08/150803_06.jpg ❖ https://s-media-cache-ak0.pinimg.com/736x/d7/7c/f7/d77cf70f4e8b56a834b9f8475c7ba5e2.jpg
  117. 117. Image Credits ❖ https://www.incimages.com/uploaded_files/image/970x450/getty_dv778060_45540.jpg ❖ https://s3.amazonaws.com/s3.timetoast.com/public/uploads/photos/8221934/download_%285.jpg? 1478393447 ❖ http://cliparts.co/cliparts/ATb/rkr/ATbrkrMXc.jpg ❖ http://bloggingwizard.adamconnell1.netdna-cdn.com/wp-content/uploads/2015/07/How-To-Grow-Your- Blogs-Audience-S.png ❖ http://www.esosolutions.com/images/home_analytics.svg ❖ https://images-na.ssl-images-amazon.com/images/I/51SPHnoK%2BrL.jpg ❖ http://cdn.wonderfulengineering.com/wp-content/uploads/2016/01/Here%E2%80%99s-How-You-Can- Combat-The-Exhaustion-You-Feel-All-The-Time-4.png ❖ http://awscomputeblogmedia.s3.amazonaws.com/ zombie_high_level_architecture_of_survivor_serverless_chat_app.png ❖ https://s-media-cache-ak0.pinimg.com/236x/99/00/51/990051c22c8abba5fbd6dc21786e91f3--fire-and-ice- crps.jpg ❖ http://pngimg.com/uploads/snake/snake_PNG4075.png
  118. 118. reachus@codeops.tech www.codeops.tech @CodeOpsTech +91 98801 64463

×