Top Banner
52

Raleigh DevDay 2017: Build a serverless web application in one day workshop

Jan 22, 2018

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Raleigh DevDay 2017: Build a serverless web application in one day workshop
Page 2: Raleigh DevDay 2017: Build a serverless web application in one day workshop

WIFI: awsDevDay | PASS: CodeHappy

U P N E X T :

Building Serverless Web Applications

Page 3: Raleigh DevDay 2017: Build a serverless web application in one day workshop

T H A N K S T O O U R F R I E N D S A T :

Page 4: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Who am I?

• Paras Bhuva ([email protected])

• Solutions Architect at AWS

• Started at Amazon Seller Services in 2012; joined AWS in 2015

• @parasbhuva on twitter

Page 5: Raleigh DevDay 2017: Build a serverless web application in one day workshop

© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Paras Bhuva, Solutions Architect

8/1/2017

Building Serverless Web Applications

Page 6: Raleigh DevDay 2017: Build a serverless web application in one day workshop

AWS compute offerings

VM Task Function

Service EC2 ECS Lambda

H/W OS Runtime

Unit of scale

Level of

abstraction

Page 7: Raleigh DevDay 2017: Build a serverless web application in one day workshop

AWS compute offerings

I want to

configure

servers,

storage,

networking,

and my OS

I want to run

servers,

configure

applications,

and control

scaling

Run my

code when

it’s needed

Service EC2 ECS Lambda

How do I

choose?

Page 8: Raleigh DevDay 2017: Build a serverless web application in one day workshop

What to expect from the session

• Why Serverless

• Design Patterns

• Group activity + Demo

• One more Demo

• Q&A (if time permits)

Page 9: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Why Serverless

Page 10: Raleigh DevDay 2017: Build a serverless web application in one day workshop

ServersHow will the application

handle server hardware failure?

How can I control

access from my servers?

When should I decide to

scale out my servers?

When should I decide to

scale up my servers?

What size servers are

right for my budget?

How much remaining

capacity do my servers have?

(AAHHHHHHHHH!!)

Page 11: Raleigh DevDay 2017: Build a serverless web application in one day workshop

No servers to provision

or manage

Scales with usage

Never pay for idle Availability and fault

tolerance built in

Serverless means…

Page 12: Raleigh DevDay 2017: Build a serverless web application in one day workshop

AWS Lambda

Page 13: Raleigh DevDay 2017: Build a serverless web application in one day workshop

AWS Lambda: Serverless computing

Run code without servers. Pay only for the compute time you consume.

Triggered by events or called from APIs:

• PUT to an Amazon S3 bucket

• Updates to Amazon DynamoDB table

• Call to an Amazon API Gateway endpoint

• Mobile app back-end call

• And many more…

Makes it easy to:

• Perform real-time data processing

• Build scalable back-end services

• Glue and choreograph systems

Page 14: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Serverless application

SERVICES (ANYTHING)

Changes in

data state

Requests to

endpoints

Changes in

resource state

EVENT SOURCE FUNCTION

Page 15: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Using AWS Lambda

Bring your own code

• Node.js, Java, Python,

C#

• Bring your own libraries

(even native ones)

Simple resource model

• Select power rating from

128 MB to 1.5 GB

• CPU and network

allocated proportionately

Flexible use

• Synchronous or

asynchronous

• Integrated with other

AWS services

Flexible authorization

• Securely grant access to

resources and VPCs

• Fine-grained control for

invoking your functions

Page 16: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Using AWS Lambda

Authoring functions

• WYSIWYG editor or

upload packaged .zip

• Third-party plugins

(Eclipse, Visual Studio)

Monitoring and logging

• Metrics for requests,

errors, and throttles

• Built-in logs to Amazon

CloudWatch Logs

Programming model

• Use processes, threads,

/tmp, sockets normally

• AWS SDK built in

(Python and Node.js)

Stateless

• Persist data using

external storage

• No affinity or access to

underlying infrastructure

Page 17: Raleigh DevDay 2017: Build a serverless web application in one day workshop

AWS Lambda Pricing

• Buy compute time in

100 ms increments

• Low request charge

• No hourly, daily, or

monthly minimums

• No per-device fees

Never pay for idle!

Free Tier1 million requests and 400,000 GBs of

compute every month, every customer

Page 18: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Imagine your business with

no cold servers.

No underutilized hardware.

No containers waiting for

work.

…and no one being paid to

worry about problems that

no longer exist.

• Buy compute time in

100 ms increments

• Low request charge

• No hourly, daily, or

monthly minimums

• No per-device fees

Never pay for idle!

Page 19: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Common Serverless Use Cases

Web

Applications

• Static websites

• Complex web

apps

• Packages for

Flask and

Express

Data

Processing

• Real time

• MapReduce

• Batch

Chatbots

• Powering

chatbot logic

Backends

• Apps &

services

• Mobile

• IoT

</></>

Amazon

Alexa

• Powering

voice-enabled

apps

• Alexa Skills

Kit

Autonomous

IT

• Policy engines

• Extending

AWS services

• Infrastructure

management

Page 20: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Amazon

S3

Amazon

DynamoDB

Amazon

Kinesis

AWS

CloudFormation

AWS

CloudTrail

Amazon

CloudWatch

Amazon

Cognito

Amazon

SNSAmazon

SES

Cron

events

DATA STORES ENDPOINTS

CONFIGURATION REPOSITORIES EVENT/MESSAGE SERVICES

Lambda Event Sources

… more on the way!

AWS

CodeCommit

Amazon

API Gateway

Amazon

AlexaAWS

IoT

AWS Step

Functions

Page 21: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Amazon API Gateway

Page 22: Raleigh DevDay 2017: Build a serverless web application in one day workshop

What is Amazon API Gateway?

Amazon API Gateway is a fully managed service that makes it easy for

developers to create, publish, maintain, monitor, and secure APIs at

any scale. “Front door” for applications to access data, business logic

or functionality from your backend services.

Key Benefits:

Low Cost and Efficient

Performance at any scale

Easily Monitor API Activity

Streamline API Development

Run your APIs without servers

Flexible Security controls

Create RESTful Endpoints for Existing Services

Page 23: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Amazon API Gateway

Internet

Mobile Apps

Websites

Services

AWS Lambda

functions

AWS

API Gateway

Cache

Endpoints on

Amazon EC2

All publicly

accessible

endpoints

Amazon

CloudWatch

Monitoring

Amazon

CloudFront

Amazon

API Gateway

Any other

AWS service

Page 24: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Create a unified

API frontend for

multiple micro-

services

Authenticate and

authorize

requests to a

backend

DDoS protection

and throttling for

your backend

Throttle, meter,

and monetize API

usage by 3rd

party developers

Amazon API Gateway

Page 25: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Build, deploy, clone, and roll back

Build APIs with their resources, methods, and settings

Deploy APIs to a stage

• Users can create as many stages as they want, each with its own

throttling, caching, metering, and logging configuration

Clone an existing API to create a new version

• Users can continue working on multiple versions of their APIs

Roll back to previous deployments

• We keep a history of customers’ deployments so they can revert to

a previous deployment

Page 26: Raleigh DevDay 2017: Build a serverless web application in one day workshop

API Gateway pricing

$3.50 per million API Gateway requests

Included in the AWS Free Tier

• 1 million API requests per month for 12 months

Data Transfer Out (standard AWS prices)

• $0.09/GB for the first 10 TB

• $0.085/GB for the next 40 TB

• $0.07/GB for the next 100 TB

• $0.05/GB for the next 350 TB

Pricing as of 7/29/17 for US-East region; subject to change; always refer https://aws.amazon.com/api-gateway/pricing/

Page 27: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Optional – Dedicated cache pricing

Cache memory

size (GB)

Price per hour

(USD)

0.5 $0.020

1.6 $0.038

6.1 $0.200

13.5 $0.250

28.4 $0.500

58.2 $1.000

118 $1.900

237 $3.800

Pricing as of 7/29/17 for US-East region; subject to change; always refer https://aws.amazon.com/api-gateway/pricing/

Page 28: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Design Patterns

Page 29: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Monolithic Application

Page 30: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Drawbacks of Monolithic Architecture

Hard to Iterate Fast CI/CD Time ConsumingHard to Scale Efficiently

Reliability Challenges

Page 31: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Benefits of Microservices Architecture

Increased Agility Easy to Scale Improved Innovation

Reduced Human Errors

Page 32: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Monolithic Serverless Web Application

Page 33: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Monolithic - What does it look like?

GET /pets

PUT /pets

DELETE /pets

GET /describe/pet/$id

PUT /describe/pet/$id

EVENT DRIVEN ONE LARGE LAMBDA FUNCTION

Page 34: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Monolithic - Pros and Cons

• Single Handler

• Handles all GET/PUT/POST/UPDATE/DELETE

• Very Large Lambda Function

• Have to build a routing mechanism

• Larger blast radius

Cons:

Pros:

• Sometimes its easier to comprehend a less

distributed system

• Deployments “could” be faster

Page 35: Raleigh DevDay 2017: Build a serverless web application in one day workshop

How can we make this better?

Page 36: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Moving from Monolithic to Microservices Architecture

Microservices ArchitectureMonolithic

Page 37: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Microservices Serverless Web Application

Page 38: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Microservices - What does it look like?

EVENT DRIVEN ONE LAMBDA PER HTTP METHOD

GET /pets

PUT /pets

DELETE /pets

GET /describe/pet/$id

PUT /describe/pet/$id

Page 39: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Microservices - Pros and Cons

• Can be harder to debug (X-ray can help with this!)

• Multiple Lambda Functions to Manage (Use SAM!!!!)

Cons:

Pros:

• Easier for teams to work Autonomously

• Separation of components

• Fine grained deployments (Integration testing is important)

• Can be easier to debug

• Agile

Page 40: Raleigh DevDay 2017: Build a serverless web application in one day workshop

What does it look like put together?

Amazon

S3Amazon

API Gateway

S3 stores all of your static

content: CSS, JS, Images, etc.

API Gateway handles all of

your application routing.

Lambda runs all of the logic

behind your website. Such as

a Create/Read/Update/Delete

service.

Page 41: Raleigh DevDay 2017: Build a serverless web application in one day workshop

How do I manage it?

MEET SAMUSE SAM TO BUILD TEMPLATES THAT DEFINE

YOUR SERVERLESS APPLICATIONS

DEPLOY YOUR SAM TEMPLATE

WITH AWS CLOUDFORMATION

Page 42: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Meet AWS Serverless Application Model (SAM)

AWS CloudFormation extension optimized for serverless

New serverless resource types: functions, APIs, and tables

Supports anything CloudFormation supports

Open specification (Apache 2.0)

http://bit.ly/AWSSamFarm

Page 43: Raleigh DevDay 2017: Build a serverless web application in one day workshop

SAM template

AWSTemplateFormatVersion: '2010-09-09’

Transform: AWS::Serverless-2016-10-31

Resources:

GetHtmlFunction:

Type: AWS::Serverless::Function

Properties:

CodeUri: s3://sam-demo-bucket/todo_list.zip

Handler: index.gethtml

Runtime: nodejs4.3

Policies: AmazonDynamoDBReadOnlyAccess

Events:

GetHtml:

Type: Api

Properties:

Path: /{proxy+}

Method: ANY

ListTable:

Type: AWS::Serverless::SimpleTable

Page 44: Raleigh DevDay 2017: Build a serverless web application in one day workshop

SAM template

AWSTemplateFormatVersion: '2010-09-09’

Transform: AWS::Serverless-2016-10-31

Resources:

GetHtmlFunction:

Type: AWS::Serverless::Function

Properties:

CodeUri: s3://sam-demo-bucket/todo_list.zip

Handler: index.gethtml

Runtime: nodejs4.3

Policies: AmazonDynamoDBReadOnlyAccess

Events:

GetHtml:

Type: Api

Properties:

Path: /{proxy+}

Method: ANY

ListTable:

Type: AWS::Serverless::SimpleTable

Tells CloudFormation this is a SAM

template it needs to “transform”

Creates a Lambda function with the

referenced managed IAM policy,

runtime, code at the referenced zip

location, and handler as defined.

Also creates an API Gateway and

takes care of all

mapping/permissions necessary

Creates a DynamoDB table with 5

Read & Write units

Page 45: Raleigh DevDay 2017: Build a serverless web application in one day workshop

SAM template

From: https://github.com/awslabs/aws-serverless-samfarm/blob/master/api/saml.yaml

<-THIS

BECOMES THIS->

Page 46: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Frameworks

Chalice

https://aws.amazon.com/serverless/developer-tools

Page 47: Raleigh DevDay 2017: Build a serverless web application in one day workshop

DEMO!

Page 48: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Use case – Data processing – sample reference architecture

10:30 AM talk focused on Real time data processing

The Seattle Times uses AWS Lambda to resize images for viewing on different

devices such as desktop computers, tablets, and smartphones.

Page 49: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Use case – Real time streaming data – sample reference architecture

10:30 AM talk focused on Real time data processing

Localytics processes billions of data points in real-time, and uses Lambda to

process historical and live data stored in S3 or streamed from Kinesis.

Page 50: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Resources

Learning path (step by step guide) – https://aws.amazon.com/getting-started/serverless-web-app/

Serverless page – https://aws.amazon.com/serverless/

Serverless architecture best practices (on YouTube) – https://youtu.be/b7UMoc1iUYw

Serverless Application Model (SAM) deep dive – https://youtu.be/e3lreqpWN0A

AWS Lambda deep dive – https://youtu.be/dB4zJk_fqrU

Developer Tooling – https://aws.amazon.com/serverless/developer-tools/

Page 51: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Thank You!

Page 52: Raleigh DevDay 2017: Build a serverless web application in one day workshop

Don’t Forget Evaluations!