getting started with aws lambda and the serverless cloud

55
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Dr. Tim Wagner General Manager, AWS Lambda and Amazon API Gateway AWS New York Summit, August 11, 2016 Getting Started with AWS Lambda, Amazon API Gateway, and the Serverless Cloud

Upload: amazon-web-services

Post on 16-Apr-2017

1.650 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Getting Started with AWS Lambda and the Serverless Cloud

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

Dr. Tim Wagner

General Manager, AWS Lambda and Amazon API Gateway

AWS New York Summit, August 11, 2016

Getting Started with AWS Lambda,

Amazon API Gateway,

and the Serverless Cloud

Page 2: Getting Started with AWS Lambda and the Serverless Cloud

Democratized Scale

The cloud is a supercomputer.

Serverless lets us program it.

Page 3: Getting Started with AWS Lambda and the Serverless Cloud

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

Amazon ECS

Amazon EC2

AWS Lambda

Page 4: Getting Started with AWS Lambda and the Serverless Cloud

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”

ECS

EC2

Lambda

Page 5: Getting Started with AWS Lambda and the Serverless Cloud

Microservices

AWS Lambda + Amazon API Gateway is the

easiest way to create microservices

• Event handlers one function per event type

• Serverless backends one function per API / path

• Data processing one function per data type

Page 6: Getting Started with AWS Lambda and the Serverless Cloud

Agenda

Overview of AWS Lambda and Amazon API Gateway

New and recent launches

Serverless use cases and best practices

Page 7: Getting Started with AWS Lambda and the Serverless Cloud

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 8: Getting Started with AWS Lambda and the Serverless Cloud

Continuous

scaling

No servers to

manage

Never pay for idle

– no cold servers

(only happy

accountants)

Benefits of AWS Lambda

Page 9: Getting Started with AWS Lambda and the Serverless Cloud

AWS Lambda Programming Model

Bring your own code

• Node.js, Java, Python

• 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

• Reports actual usage

Programming model

• AWS SDK built in (Python

and Node.js)

• Lambda is the “webserver”

• Use processes, threads,

/tmp, sockets normally

Stateless

• Persist data using Amazon

DynamoDB, S3, or Amazon

ElastiCache

• No affinity to infrastructure

(can’t “log in to the box”)

Page 10: Getting Started with AWS Lambda and the Serverless Cloud

Using AWS Lambda

Authoring functions

• Author directly using the

console WYSIWYG editor

• Package code as a .zip and

upload to Lambda or S3

• Plugins for Eclipse and

Visual Studio

• Command line tools

Monitoring and logging

• Built-in metrics for requests,

errors, latency, and throttles

• Built-in logs in Amazon

CloudWatch Logs

Flexible authorization

• Securely grant access to

resources, including VPCs

• Fine-grained control over

who can call your functions

Flexible use

• Call or send events

• Integrated with other AWS

services

• Build whole serverless

ecosystems

Page 11: Getting Started with AWS Lambda and the Serverless Cloud

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 12: Getting Started with AWS Lambda and the Serverless Cloud

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 13: Getting Started with AWS Lambda and the Serverless Cloud

Amazon API Gateway: Serverless APIs

Internet

Mobile

apps

Websites

Partner

Services

AWS Lambda

functions

API

Gateway

response

cache

Endpoints on

Amazon EC2

Any publicly

accessible

endpoint

Amazon

CloudWatch

Amazon

CloudFront

API

Gateway

Page 14: Getting Started with AWS Lambda and the Serverless Cloud

Benefits of Amazon API Gateway

Create a unified API

front end for

multiple

microservices

DDoS protection

and throttling for

back-end systems

Authenticate and

authorize requests

Page 15: Getting Started with AWS Lambda and the Serverless Cloud

API Authorization: 3 Options

Page 16: Getting Started with AWS Lambda and the Serverless Cloud

Auth option #1: SigV4 / IAM

Internet

Mobile

apps

Partner

Services

AWS Lambda

functions

Endpoints on

Amazon EC2

Amazon

CloudFront

API

Gateway

Amazon

Cognito

AWS Identity & Access Management

(IAM)IAM user / role

acquisition

SigV4 credentials

Page 17: Getting Started with AWS Lambda and the Serverless Cloud

Auth option #2: Custom Lambda authorizer

Internet

Mobile

apps

Websites

Partner

Services

AWS Lambda

functions

Policy

cache

Endpoints on

Amazon EC2

Any publicly

accessible

endpoint

Amazon

CloudFront

API

Gateway

Lambda custom

Auth functionOAuth

provider

403

Page 18: Getting Started with AWS Lambda and the Serverless Cloud

Auth option #3: Amazon Cognito User Pools

Internet

Mobile

apps

Partner

Services

AWS Lambda

functions

Endpoints on

Amazon EC2

Amazon

CloudFront

API

Gateway

Amazon

Cognito

Websites

User login

Built-in auth

check

OIDC token

OIDC token

Any publicly

accessible

endpoint

Page 19: Getting Started with AWS Lambda and the Serverless Cloud

More new and recent launches

Page 20: Getting Started with AWS Lambda and the Serverless Cloud

Recent region launches: Singapore, Sydney

Available regions (7)

Page 21: Getting Started with AWS Lambda and the Serverless Cloud

Launch: Amazon API Gateway usage plans

New API Management features:

• Define groups of consumers (partners, apps)

• Set throttles and quotas for each group

• Track each group’s usage

Helps you:

• Manage and monetize your APIs

• Apply business policies across your consumers

Page 22: Getting Started with AWS Lambda and the Serverless Cloud

Throttle

Usage plans: Throttle specific consumers

Internet

Mobile

apps

Websites

Partner

Services

AWS Lambda

functions

API

Gateway

response

cache

Endpoints on

Amazon EC2

Any publicly

accessible

endpoint

Amazon

CloudWatch

Amazon

CloudFront

API

Gateway

Page 23: Getting Started with AWS Lambda and the Serverless Cloud

Usage Plans: Throttling

• Prevents one customer from consuming all your

backend system’s capacity

• Let’s you decide how to allocate capacity among your

API consumers. Sample plan:

• Professional plan users: 10 TPS

• Premium plan users: 100 TPS

• Enterprise plan users: 500 TPS

Page 24: Getting Started with AWS Lambda and the Serverless Cloud

Set daily

quota

Usage plans: Enforce per-consumer quotas

Internet

Mobile

apps

Websites

Partner

Services

AWS Lambda

functions

API

Gateway

response

cache

Endpoints on

Amazon EC2

Any publicly

accessible

endpoint

Amazon

CloudWatch

Amazon

CloudFront

API

Gateway

Page 25: Getting Started with AWS Lambda and the Serverless Cloud

Usage Plans: Quotas

• Allows you to define and enforce usage limits by API

consumer (or groups of consumers)

• Sample plan:

• Professional plan: up to 100 calls / day

• Premium plan: up to 1000 calls / day

• Enterprise plan: no limit on calls

Page 26: Getting Started with AWS Lambda and the Serverless Cloud

Track usage

Usage plans: Track API usage

Internet

Mobile

apps

Websites

Partner

Services

AWS Lambda

functions

API

Gateway

response

cache

Endpoints on

Amazon EC2

Any publicly

accessible

endpoint

Amazon

CloudWatch

Amazon

CloudFront

API

Gateway

Page 27: Getting Started with AWS Lambda and the Serverless Cloud

Usage Plans: Usage reporting

• Allows you to export usage reports by consumer (per

API key)

• Facilitates billing, analytics, and monitoring

Page 28: Getting Started with AWS Lambda and the Serverless Cloud

API Management Demo(and a bonus demo)

Page 29: Getting Started with AWS Lambda and the Serverless Cloud

Use cases

Page 30: Getting Started with AWS Lambda and the Serverless Cloud

Use cases

Serverless app

ecosystems

Data processing Back ends

Page 31: Getting Started with AWS Lambda and the Serverless Cloud

Use case: Serverless data processing

Example: Amazon S3 bucket triggers

Amazon S3 bucket events

Original objectCompressed object

1

2

3

AWS Lambda

Page 32: Getting Started with AWS Lambda and the Serverless Cloud

Use case: Serverless web apps

1. Amazon S3 for serving static content

2. AWS Lambda for dynamic content

3. Amazon API Gateway for https access

4. Amazon DynamoDB for NoSQL data storage

Dynamic content

in AWS Lambda

Data stored in

Amazon

DynamoDB

API GatewayStatic content in

Amazon S3Browser

Page 33: Getting Started with AWS Lambda and the Serverless Cloud

Serverless Python Web Framework

Easy way to create serverless web apps in Python

Flask-like, easy command routing

CLI deployment and setup for both APIs and functions

Intelligent policy generator

Page 34: Getting Started with AWS Lambda and the Serverless Cloud

Use case: Serverless mobile and IoT apps

1. Pick one:a. Mobile apps: AWS Mobile SDK + Amazon Cognito (authorization)

b. IoT devices: AWS IoT

2. AWS Lambda’s “Mobile Backend” blueprint

3. Amazon DynamoDB for data storage

AWS LambdaAmazon

DynamoDB

Page 35: Getting Started with AWS Lambda and the Serverless Cloud

Use case: Serverless app ecosystems

Alexa, tell Slack to

send, “I’m giving the

demo now.”

Message retrieval through scheduled

polling

Kevin says,

“Break a leg!”

Message upload

(via Slack API)

Team

(channel users)

Slack

Page 36: Getting Started with AWS Lambda and the Serverless Cloud

Growing Serverless Ecosystem

Logging and Monitoring Applications and Deployment

Build and CI/CD

Page 37: Getting Started with AWS Lambda and the Serverless Cloud

Tips and best practices

Page 38: Getting Started with AWS Lambda and the Serverless Cloud

AWS Lambda VPC: Best practices

VPC is optional – don’t turn in on unless you need it.

Functions configured for VPC access lose internet access…

• Unless you have managed NAT or a NAT instance in the VPC

The ENIs used by Lambda’s VPC feature count against your quota.

Ensure you have enough to match your peak concurrency levels

(we’ll consolidate where we can).

DO NOT delete or rename these ENIs!

Ensure your subnets have enough IPs for those ENIs.

Specify at least one subnet in each Availability Zone

Otherwise, Lambda will obey, but can’t be as fault-tolerant.

Page 39: Getting Started with AWS Lambda and the Serverless Cloud

Function Scheduling: Tips

How can I keep a function warm (no cold starts)?

Schedule [multiple copies of] it.

How can I poll a queue (like Amazon SQS)?

Schedule a function to read the queue.

How can I get more timers?

Have one scheduled function async invoke other functions.

How can I get granularity finer than 1 minute?

Run a background timer in your scheduled function.

Page 40: Getting Started with AWS Lambda and the Serverless Cloud

Stage Variables in API Gateway: Basics

• Stage variables act like environment variables

• Use stage variables to store configuration values

• Stage variables are available in the $context object

• Values are accessible from most fields in API Gateway

• Lambda function ARN

• HTTP endpoint

• Custom authorizer function name

• Parameter mappings

Page 41: Getting Started with AWS Lambda and the Serverless Cloud

Stage variables: Best practices

Using Stage Variables in API Gateway together with

Lambda function aliases:

MyLambdaFunction

1

2

3 = prod

4

5

6 = beta

7

8 = dev

MyAPI

Stage variable = lambdaAlias

Prod

lambdaAlias = prod

Beta

lambdaAlias = beta

Dev

lambdaAlias = dev

Page 42: Getting Started with AWS Lambda and the Serverless Cloud

Managing Microservices

Page 43: Getting Started with AWS Lambda and the Serverless Cloud

Serverless app model

A single function is easy to

deploy and manage:

Page 44: Getting Started with AWS Lambda and the Serverless Cloud

Serverless app model

But what happens when you

have an entire app made up of

many functions?

Page 45: Getting Started with AWS Lambda and the Serverless Cloud

Serverless app model

And then many different apps?

Page 46: Getting Started with AWS Lambda and the Serverless Cloud

Serverless app model

Project Flourish provides an

application model for

serverless apps.

• Organize related functions

• Retain ability to deploy and

manage them individually

Page 47: Getting Started with AWS Lambda and the Serverless Cloud

Project Flourish Demo

Page 48: Getting Started with AWS Lambda and the Serverless Cloud

AWS Serverless Chatbot Competition

• Create a bot for Slack that runs on AWS

Lambda and Amazon API Gateway

• Sponsored by AWS and Slack

• Win tickets to AWS re:Invent and more

• Winners receive mentions in the

Serverless Keynote during re:Invent

• Submissions due by September 29

• Get started: awschatbot.devpost.com

Page 49: Getting Started with AWS Lambda and the Serverless Cloud

Join the serverless

revolution today!

Page 50: Getting Started with AWS Lambda and the Serverless Cloud

Go to the AWS Lambda console,

create a function, and run it.(The first million invokes are on us!)

Page 51: Getting Started with AWS Lambda and the Serverless Cloud

Go to the Amazon API Gateway

console and create a PetStore

API.

Page 52: Getting Started with AWS Lambda and the Serverless Cloud

Hook up your API to your

Lambda function and set some

usage plans on it.

Page 53: Getting Started with AWS Lambda and the Serverless Cloud

Remember to complete

your evaluations in the

Guidebook mobile app.

Page 54: Getting Started with AWS Lambda and the Serverless Cloud

AWS Lambda and

Amazon API Gateway links:

aws.amazon.com/blogs/compute

aws.amazon.com/lambda

AWS Lambda forum

me: @timallenwagner

Page 55: Getting Started with AWS Lambda and the Serverless Cloud