45

Raleigh DevDay 2017: Building serverless web applications

Embed Size (px)

Citation preview

WIFI: awsDevDay | PASS: CodeHappy

U P N E X T :

Building Serverless Web Applications

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

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

Paras Bhuva, Solutions Architect

8/1/2017

Building Serverless Web Applications

What to expect from the session

• Why Serverless

• Design Patterns

• Group activity + Demo

• One more Demo

• Q&A (if time permits)

AWS compute offerings

VM Task Function

Service EC2 ECS Lambda

H/W OS Runtime

Unit of scale

Level of

abstraction

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?

Why Serverless

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

No servers to provision

or manage

Scales with usage

Never pay for idle Availability and fault

tolerance built in

Serverless means…

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

Serverless application

SERVICES (ANYTHING)

Changes in

data state

Requests to

endpoints

Changes in

resource state

EVENT SOURCE FUNCTION

Node.js

Python

Java

C#

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

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

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

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

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

Design Patterns

Monolithic Application

Drawbacks of Monolithic Architecture

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

Reliability Challenges

Benefits of Microservices Architecture

Increased Agility Easy to Scale Improved Innovation

Reduced Human Errors

Monolithic Serverless Web Application

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

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

How can we make this better?

Moving from Monolithic to Microservices Architecture

Microservices ArchitectureMonolithic

Microservices Serverless Web Application

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

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

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.

How do I manage it?

MEET SAMUSE SAM TO BUILD TEMPLATES THAT DEFINE

YOUR SERVERLESS APPLICATIONS

DEPLOY YOUR SAM TEMPLATE

WITH AWS CLOUDFORMATION

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

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

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

SAM template

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

<-THIS

BECOMES THIS->

Frameworks

Chalice

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

DEMO!Please vote for your favorite drink here…

http://tinyurl.com/votingdemo

DEMO!One more Demo

(and then we’ll talk advanced architecture patterns)

Architecture patterns – Building and Analyzing Alexa skills at scale

AWS

Lambda

Amazon

DynamoDB

Amazon

CloudWatchStreams

Your skill

Alexa

Skills KitAWS

Lambda

AWS

Lambda

Alexa

Skills KitAWS

Lambda

Amazon

CloudWatchAmazon

Kinesis

Firehose

Amazon

QuickSightAmazon

Redshift

S3

Use case – Data processing – sample reference architecture

10:30 AM talk focused on Real time data processing

Use case – Real time streaming data – sample reference architecture

10:30 AM talk focused on Real time data processing

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

Thank You!

Don’t Forget Evaluations!