104
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Jeremy Edberg, Founder and CEO, MinOps December 2016 CMP211 Getting Started with Serverless Architectures

AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Embed Size (px)

Citation preview

Page 1: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

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

Jeremy Edberg, Founder and CEO, MinOps

December 2016

CMP211

Getting Started

with Serverless Architectures

Page 2: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 3: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Test and prod are different

Prod is in need of constant

updates

Slow iteration and deployment

Polyglot unfriendly

Deploy in weeks, live for years

Physical Servers

Page 4: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Prod is immutable

Rapid iteration and deployment

Multi-tenancy

Polyglot friendly

Deploy in minutes, live for weeks

Virtual Machines

Page 5: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Test and prod are the same

Prod is immutable

Rapid(er) iteration and deployment

High multi-tenancy

Polyglot friendly

Deploy in seconds, live for hours

Containers

Page 6: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Smallest unit of compute

Super scalable

Rapid iteration

Extreme multi-tenancy

Very polyglot friendly

Easier to collaborate

Deploy independently, live for

seconds

Serverless

Page 7: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

A whole lot of choices

Amazon ecosystemHodgepodge of services

Page 8: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

A whole lot of choices

Page 9: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

AWS Serverless

Ecosystem

Lambda

SNS

DynamoDB

SQS

S3

Amazon Kinesis

Page 10: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Continuous

scaling

No servers to

manage

Never pay for

idle – No cold

servers (only

happy

accountants)

Benefits of AWS Lambda

Page 11: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Lambda is a core

component of modern

software

Page 12: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

What is serverless

anyway?

• There are still servers, you just

don’t manage them anymore

• It also means you don’t access

them anymore

• So you don’t need to (or get to)

optimize them.

Page 13: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

What is Serverless

Computing?

• VMs

• Machine as the unit of scale

• Abstracts the hardware

• Containers

• Application as the unit of scale

• Abstracts the OS

• Serverless

• Functions as the unit of scale

• Abstracts the language runtime

EC2

ECS

Lambda

Page 14: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

How do I choose?

• VMs

• “I want to configure machines,

storage, networking, and my OS”

• Containers

• “I want to run servers, configure

applications, and control scaling”

• Serverless

• “Run my code when it’s needed”

EC2

ECS

Lambda

Page 15: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

What if I buy my own

software?

Lambda is so flexible you can add it in or around existing software

Page 16: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Serverless computing is

all about speeding up

development by allowing

rapid iteration and

removing management

overhead

Page 17: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

A brief journey through history

Page 18: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

80’s Mainframe Cycle

• Cost $1MM to $100MM

• 1 to 5 years to execute

• Usually had to bet the whole

company

• Cost of failure -- Bankruptcy

Page 19: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

90‘s Client/Server Cycle

• Cost $100K to $10MM

• 3 to 6 months to execute

• Usually had to bet a product or

division

• Cost of failure -- Revenue hit, CXX

job

Page 20: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

00‘s Agile Cycle

• Cost $10K to $1MM

• 3 to 6 weeks to execute

• Bet a product feature

• Cost of failure -- product manager

reputation

Page 21: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Product Manager

Developer

QA

Ops

BI / Analytics

Page 22: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Cost and size

of risk

decreased

Rate of

change

increased

Page 23: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

ReleaseTestBuild

Developer Deployment Pain: High

Page 24: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

ReleaseTestBuild

ReleaseTestBuild

ReleaseTestBuild

ReleaseTestBuild

ReleaseTestBuild

Developer Deployment Pain: Medium

Page 25: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

ReleaseTestBuild

ReleaseTestBuild

ReleaseTestBuild

ReleaseTestBuild

ReleaseTestBuild

Developer Deployment Pain: Low

Page 26: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Multiple services

Internal Microservices Platform

Monolithic

Success follows a standard

pattern

Page 27: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Why Microservices?

Page 28: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Distributed Computing and a

Distributed Workforce

• The two go hand in hand

when you have a good

distributed systems

culture

• Microservices and micro

teams

Page 29: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Cloud Native

10s of thousands of instances,

thousands created and removed

daily

Thousands of storage nodes,

petabytes of data, nodes can

be removed without harm

(Some folks call this microservices)

Page 30: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Movie Ratings

Personalization Engine User Info

Movie Metadata

Similar Movies

ReviewsA/B Test Engine

Discovery

API

Streaming

API

Page 31: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Movie Ratings

Personalization Engine User Info

Movie Metadata

Similar Movies

Reviews

A/B Test Engine

Discovery

API

Streaming

API

Content

EncodingCDN

Management

QOS

LoggingDRM

OpenConnect

Edge

Locations

Browse

Play

Watch

Page 32: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

• Services are built by different

teams who work together to

figure out what each service

will provide.

• The service owner publishes

an API that anyone can use

and returns proper response

codes

Highly aligned,

loosely coupled

Page 33: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Developers own their product

from beginning to end

If the customer isn’t happy,

the developer shouldn’t be happy

Freedom and

Responsibility

Page 34: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Proper

Microservices

Architecture

Service

and

Resource

Discovery

Network

and

Traffic

Config

Automated

Testing

Continuous

DeploymentSecurity

Monitoring

and

Alerting

Page 35: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Mature

companies

spend 25% of

their engineering

resources on

their internal

platform

Page 36: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

And they’re all building the

same thing…

Wasting time and effort they could

be spending on product innovation

Page 37: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Proper

Microservices

Architecture

Service

and

Resource

Discovery

Network

and

Traffic

Config

Automated

Testing

Continuous

DeploymentSecurity

Monitoring

and

Alerting

Page 38: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

And when you’re done, it is

only “good enough”

Building an internal

microservices platform is hard

Page 39: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

So why go Serverless?

Page 40: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Proper

Microservices

Architecture

Service

and

Resource

Discovery

Network

and

Traffic

Config

Automated

Testing

Continuous

DeploymentSecurity

Monitoring

and

Alerting

Page 41: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Proper

Microservices

Architecture

Automated

Testing

Continuous

DeploymentSecurity

Page 42: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Security

• Shorter TTL ==

less chance for

an attack to take

hold

Page 43: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

The Microservices Architecture

Page 44: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Microservices Management

Tools

Web servers

Code Libraries

Frameworks

Config Management

API Management

Deployment, CI/CDContainers

etc.

etc.

etc.

Page 45: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

What do all the parts of

microservices have in common?

Page 46: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Servers

Capacity planning

Right-sizing

Automatic scaling

Load and performance

Patches

Tuning

Configuration

Utilization

Access control

Packages and AMIs

Page 47: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Serverless

Right-sizing

Automatic scaling

Load and performance

Patches

Tuning

Configuration

Utilization

Access control

Packages and AMIs

Fully managed

Continuous Scaling

Function is the deployment unit

Capacity planning

Page 48: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Serverless ==

Microservices -

ManagementProvisioning Scaling

Page 49: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Microservice problems we still have

Queue length

Persistent storage

Fallbacks and graceful

degradation

Monitoring and alerting

Integration and deployment

Testing

Page 50: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

What does Lambda do for you?

• Scales server capacity

automatically

• API to trigger execution

• Ensures function is executed in

parallel and at scale

• Logging, monitoring, etc.

• Easy pricing

Page 51: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Monitoring• Everything is in CloudWatch or CloudWatch logs

Page 52: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Pricing

• Choose your RAM

from 128 MB to

1500 MB

• CPU and network

scaled based on

RAM

Page 53: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Cost Comparison

Page 54: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Cost Comparison

There’s about 2.5M seconds in a month, so 3M requests is about 1.2 per second

The T2.Small is $18.98 a month, more than Lambda already

Page 55: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Pay per Request

• Buy compute in 100ms

increments

• Low request charge

• No minimums

• No per-device fees

Never pay for idle time!

Free Tier

1,000,000 requests and 400,000

GB every month, every

customer

Page 56: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

How does one use ?

Page 57: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Author your code

• AWS SDK built-in

• Access to /tmp,

processes,

threads, etc.

• Node.js, Python

• Java and any JVM

language

• Use any library you

want

Page 58: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Choose an event source

• S3

• Amazon

Kinesis

• SNS

• DynamoDB

• CloudWatch

• Config Rules

• Amazon Echo

• API Gateway

• Lambda

functions

• SES

• Cognito

• CloudFormation

• CodeCommit

• Echo

• Scheduled

events

Page 59: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Choose your resources

• Select from 128 MB to 1.5 GB

• CPU and network are chosen

automatically

• Pay only for what you actually

use

Page 60: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Choose your

authorization model

• Securely grant access to

resources and VPC

• Fine-grained control over

who calls your functions

with IAM

Page 61: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Choose a Network

• Default

• All functions have

internet access

• No access to your

other VPCs

• Still in a VPC

• Customer VPC

• Private communication

within your VPC

• Same configuration as

your other VPCs (ENI,

route table, security

groups, NAT)

Page 62: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Deploy your code

• Use the GUI or

• Upload to S3

or Lambda

directly

• Use a third

party tool

Page 63: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Write the function

Create the IAM role

Add extra permissions for a push model

Zip the function and dependencies

Test the function with mock data

Add an event source to the function

View the output of the live function

Page 64: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Write the function

Create the IAM role

Add extra permissions for a push model

Zip the function and dependencies

Test the function with mock data

Add an event source to the function

View the output of the live function minops.com/oss

Use a tool

Page 65: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

PolishDeep

Integration

Language

Support

API Gateway

SupportNotes

Kappa Meh Best Meh No

Best for

Python, same

author as boto

Apex Great Ok Best NoTies into

Terraform

Serverless

(Jaws)Great Good Good Yes

Uses Cloudformation

Chalice Great Ok Only Python YesGreat for

REST APIs

Serverless Ex

pressGreat

Only w/ API

GatewayOnly NodeJS Yes Built by AWS

Page 66: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Create a unified API

frontend for multiple

microservices

DDoS protection and

throttling for backend

systems

Authenticate and

authorize requests

Benefits of Amazon API

Gateway

Page 67: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Lambda Use Cases

• Application Backend

• Data Processing

• Command and Control

• Any functional or event

based system

Page 68: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Data Processing

Upload

to S3

S3 Bucket Lambda

S3 Notification

Compressed

Image

Compress Image

Upload back to S3

Page 69: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Serverless Website

Static

Content

Dynamic

Content

API Gateway Lambda DynamoDB

Page 70: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Internet

Mobile Apps

Websites

Services

AWS Lambda

functions

API Gateway

Cache

Endpoints on

Amazon EC2

Any other publicly

accessible endpointAmazon

CloudWatch

Amazon

CloudFront

Amazon

API Gateway

Serverless API w/ API

Gateway

Page 71: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

API Gateway Lambda SNS

Mobile Backend

Page 72: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Kinesis Lambda DynamoDB

Real-Time Analytics Engine

Page 73: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Live video stream processing

CloudFront S3

Ingest

Lambda functions

HQ Copy

480p

Transcode

360p

Transcode

Audio Only

Transcode

S3

Playback

CloudFront

streaming

Thumbnails

Page 74: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Word Generator

• Generate some number of English

looking words using an ngram

database

• Allow a prefix to be specified

https://github.com/jedberg/wordgen

Page 75: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 76: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 77: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 78: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 79: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 80: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 81: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 82: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Alexa and Lambda Demo

Page 83: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

From conception to prod

in 5 minutes

Page 84: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 85: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Codebase

Structure

Page 86: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 87: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)
Page 88: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Lambda lets you manage

your code and infrastructure

in the same place

Page 89: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Tips and Tricks

Page 90: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Immutable Data

• If you can, write your

software such that

everything in the cache is

immutable.

Page 91: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Moving data is

the single biggest cost

your distributed system

will incur

Page 92: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

But you need

to move data for

reliability,

so it’s a tradeoff

Use queues

as often as possible

Page 93: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

0

10

20

30

40

50

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Cumulative Flow Diagram

Items

Seconds

Arrivals

Departures

Page 94: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Avoiding Infinite Loops

• With a distributed

team, this is an easy

mistake to make

• To avoid it, pass a call

stack and check for

self in the stack

Page 95: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Store your data properly

• No local instance

access

• Store everything in

S3, DynamoDB, or

ElastiCache

DynamoDB S3

Page 96: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Function Schedules• Schedule a function to keep it warm

if traffic is low

• Schedule a function to poll your

SQS queues or use a DynamoDB

table as a queue and trigger on put

• Schedule a function with a more

granular timer if one minute isn’t

granular enough

• Have one function call another to

get more timers

Page 97: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Function Versions

• Use the environment variable

service for mutable

configuration

• To rollback, switch to an old

alias or an old API Gateway

collection

• Use a traffic shaping function

for Blue / Green deployments

• Point directly at an ARN to

lock to a particular version

Page 98: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Lambda VPC Basics• VPC is always on, but with

sane defaults

• Lambda functions can

access your private

resources if added to your

VPC

• But they will lose internet

access unless you have a

managed NAT, regardless

of your internet gateway or

permissive security group

rules

Page 99: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Lambda VPC Basics

• Make sure you have

enough ENIs for max

concurrency

• And enough IPs in your

pool

• And enough per AZ

Page 100: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Amazon API Gateway

Best Practices• Use request/response

mapping templates

everywhere within reason,

not passthrough.

• Take ownership of HTTP

response codes

• Use Swagger

import/export for cross-

account sharing

• Use Mock integrations

• Combine with Cognito for

managed end user-based

access control.

• Use stage variables (inject API

config values into Lambda

functions for logging, behavior)

Page 101: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Additional Best

Practices• Use strategic, consumable

naming conventions (Lambda

function names, IAM roles, API

names, API stage names, etc.)

• Use naming conventions and

versioning to create

automation.

• Externalize authorization to

IAM roles whenever possible

• Least privilege and separate IAM roles

• Externalize configuration –DynamoDB is great for this.

• Contact AWS Support before known large scaling events

• Be aware of service throttling, engage AWS Support if so.

Page 102: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Thank you!

Page 103: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Remember to complete

your evaluations!

Page 104: AWS re:Invent 2016: Getting Started with Serverless Architectures (CMP211)

Questions?

Email: [email protected]

Twitter: @jedberg

Web: www.jedberg.net

Facebook: facebook.com/jedberg

Linkedin:

www.linkedin.com/in/jedberg

Company: minops.com