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