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

Building Microservices on AWS with the Serverless Framework

Building Microservices on AWS with the Serverless Framework

Addam Hardy

April 06, 2018
Tweet

More Decks by Addam Hardy

Other Decks in Programming

Transcript

  1. What is it? Serverless (The architecture design pattern) Deploy applications

    to an event driven function execution platform hosted and managed by a cloud provider. Use the infrastructure and power of the best cloud providers. AWS can server way better than I can server. Pay for what you use. Continuous and elastic scaling. 4 / 27
  2. Cost Savings If you allocated 512MB of memory to your

    function, executed it 3 million times in one month, and it ran for 1 second each time: Compute Costs: The monthly compute price is $0.00001667 per GB-s and the free tier provides 400,000 GB-s . Total compute (seconds) = 3M * (1s) = 3,000,000 seconds Total compute (GB-s) = 3,000,000 * 512MB/1024 = 1,500,000 GB-s Total compute – Free tier compute = Monthly billable compute GB- s 1,500,000 GB-s – 400,000 free tier GB-s = 1,100,000 GB-s Monthly compute charges = 1,100,000 * $0.00001667 = $18.34 7 / 27
  3. Cost Savings If you allocated 512MB of memory to your

    function, executed it 3 million times in one month, and it ran for 1 second each time: Request Costs: The monthly request price is $0.20 per 1 million requests and the free tier provides 1M requests per month. Total requests – Free tier requests = Monthly billable requests 3M requests – 1M free tier requests = 2M Monthly billable requests Monthly request charges = 2M * $0.2/M = $0.40 8 / 27
  4. Cost Savings If you allocated 512MB of memory to your

    function, executed it 3 million times in one month, and it ran for 1 second each time: Total Costs: Monthly compute charges = 1,100,000 * $0.00001667 = $18.34 Monthly request charges = 2M * $0.2/M = $0.40 Compute charges + Request charges = $18.34 + $0.40 = $18.74 per month 9 / 27
  5. Cost Savings // EC2 Comparison M4 General Purpose Large EC2

    Instance in North Virginia (us-east-1) region, 8Gb Memory, 2 vCPUs = $45.26 per month If you have 500,000 requests to an application per month that take 1 second each: App on m4.large on EC2: ~$45.26 App on lambda with 512mb memory: ~$1.67 If you have 0 requests to an application per month: App on m4.large on EC2: ~$45.26 App on lambda with 512mb memory: $0.00 Note: Lambda has an inflection point where it becomes cheaper to use EC2 than Lambda. You should also always use EC2 if your functions are long running functions. Anything over a few seconds should be considered for EC2, but you have a 5 minute limit available in Lambda. 10 / 27
  6. What is it? Serverless (The framework) Open-source CLI for building

    serverless architectures. Provider Agnostic. Can deploy to AWS, Azure, Google Cloud, IBM OpenWhist Open source and very extensible plugin based system Multi-lingual. Write code in any language the platform you are deploying to supports Build REST APIs, Scheduled Tasks, Alexa Skills, Etc. 12 / 27
  7. What is it? It's not just AWS Lambda. Serverless on

    AWS creates and connects several services on AWS Example of AWS services created when you deploy a Serverless API: S3 - Local code is zipped and uploaded to an S3 bucket Lambda - Lambda function is created with the zipped code in S3 API Gateway - A REST API is created along with HTTP endpoints that are used as triggers to initiate the Lambda function Cloudwatch - Logs from API Gateway and Lambda function are sent to Cloudwatch for log storage Cloudformation - This is the mechanism that creates and manages all of the created infrastructure as a single unit/stack From now on when I'm talking about the Serverless Framework, I'm going to refer to it as Serverless 13 / 27
  8. Available AWS Runtimes: aws-nodejs, aws-nodejs-typescript, aws-nodejs-ecma-script aws-python, aws-python3 aws-kotlin-jvm-maven, aws-kotlin-nodejs-gradle

    aws-groovy-gradle aws-java-gradle, aws-java-maven aws-scala-sbt aws-csharp aws-fsharp aws-go # Create service with nodeJS template in the folder ./quick-service ➜ serverless create --template aws-nodejs --path quick-service Serverless: Generating boilerplate... Serverless: Generating boilerplate in "/Users/addam/dev/richcontext/presentations/ _______ __ | _ .-----.----.--.--.-----.----| .-----.-----.-----. | |___| -__| _| | | -__| _| | -__|__ --|__ --| |____ |_____|__| \___/|_____|__| |__|_____|_____|_____| | | | The Serverless Application Framework | | serverless.com, v1.26.0 -------' Serverless: Successfully generated boilerplate for template: "aws-nodejs" 15 / 27
  9. What happened? serverless create creates a directory for you with

    2 basic files for you to get started. ➜ quick-service tree . ├── handler.js └── serverless.yml 0 directories, 2 files It has not deployed anything yet or created any AWS services. 16 / 27
  10. serverless.yml serverless.yml is a yaml file that defines the infrastructure

    and services you want to create on your remote provider. It contains: service configuration that defines a service One or more functions in the service The provider it will be deployed to Events that trigger functions in the service Resources required by functions in the service (e.g DynamoDB, ElasticCache, etc) 17 / 27
  11. serverless.yml service: quick-service provider: name: aws runtime: nodejs6.10 functions: hello:

    handler: handler.hello events: - http: path: hello method: get This creates an endpoint at /hello that triggers the hello function inside of the handler file. After deployment, Serverless* gives you the associated domain name and lists routes. Note: The file created contains many commented out lines to give examples of usage that I removed for clarity here. 18 / 27
  12. handler.js 'use strict'; module.exports.hello = (event, context, callback) => {

    const response = { statusCode: 200, body: JSON.stringify({ message: 'Hi. ' }), }; callback(null, response); }; statusCode sets the HTTP status code you want the response to have body sets the response body to the JSON string Yes, that's really it. 19 / 27
  13. Deploy It ➜ quick-service serverless deploy Serverless: Packaging service... Serverless:

    Excluding development dependencies... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading artifacts... Serverless: Uploading service .zip file to S3 (526 B)... Serverless: Validating template... Serverless: Updating Stack... Serverless: Checking Stack update progress... .................... Serverless: Stack update finished... Service Information service: quick-service stage: dev region: us-east-1 stack: quick-service-dev api keys: None endpoints: GET - https://j6p1mch0gi.execute-api.us-east-1.amazonaws.com/dev/hello functions: hello: quick-service-dev-hello Note: You need to have your AWS credentials setup on your machine. They can be in ENV variables or set with the serverless config credentials --provider aws command. If you already have aws-cli setup on your machine, that will work as well. 20 / 27
  14. Test It ➜ quick-service curl -s \ > https://j6p1mch0gi.execute-api.us-east-1.amazonaws.com/dev/hello |

    jq { "message": "Hi. " } How complicated would this be with a traditional web framework? Even in the easiest of web frameworks you still have to provision servers and build deployment scripts. 21 / 27
  15. Scheduled Jobs Scenario: We have an endpoint setup that checks

    the stock of an item. We want that same code to be run every hour. Traditional Solution: Cron function to call API endpoint every hour. Serverless Solution: Add a scheduled event to our function definition. Before: service: stockChecker provider: name: aws runtime: nodejs6.10 functions: hello: handler: handler.checkStock events: - http: path: hello method: get 22 / 27
  16. Scheduled Jobs Scenario: We have an endpoint setup that checks

    the stock of an item. We want that same code to be run every hour. Traditional Solution: Cron function to call API endpoint every hour. Serverless Solution: Add a scheduled event to our function definition. After: service: stockChecker provider: name: aws runtime: nodejs6.10 functions: hello: handler: handler.checkStock events: - http: path: hello method: get - schedule: rate(1 hours) Only had to add schedule: rate(1 hours) to events definition and redeploy. It took under a minute. 23 / 27
  17. Creating New Environments Production serverless.yml : service: stockChecker provider: name:

    aws stage: production runtime: nodejs6.10 functions: hello: handler: handler.checkStock events: - http: path: hello method: get 24 / 27
  18. Creating New Environments Change stage to a different name service:

    stockChecker provider: name: aws stage: staging runtime: nodejs6.10 functions: hello: handler: handler.checkStock events: - http: path: hello method: get Deploy ➜ serverless deploy There's now a mirrored environment with a new URL ProTip: Create as many environments as you want or need. They only cost money if you use them. 25 / 27
  19. Let's see how fast we can create a live API

    on AWS Requirements: takes an input of two numbers, a and b , and returns a * b . Click here for code 26 / 27