lambda and serverless - devops north east jan 2017

34
AWS Lambda and Serverless Look mam, nee servers! Mike Shutlar, Expert Flamebaiter for DevOps North East 2017-01-19

Upload: mike-shutlar

Post on 22-Jan-2018

209 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Lambda and serverless - DevOps North East Jan 2017

AWS Lambda and Serverless

Look mam, nee servers!

Mike Shutlar, Expert Flamebaiterfor DevOps North East 2017-01-19

Page 2: Lambda and serverless - DevOps North East Jan 2017

Lambda

Page 3: Lambda and serverless - DevOps North East Jan 2017

What is AWS Lambda?

A compute service

No infrastructure resources...

● to provision

● to set up

● to manage

● to pay for 24/7 whether in use or not

Scales automatically

Only pay for what is used

Page 4: Lambda and serverless - DevOps North East Jan 2017

What is AWS Lambda?

BYOC - Bring Your Own Code

Available runtimes:

● Node.js v4.3

● Python 2.7

● Java 8

● C# (.NET Core 1.0.1)

Behind the scenes: executes within Amazon Linux containers

Page 5: Lambda and serverless - DevOps North East Jan 2017

Lambda Functions

Confusingly named

Stateless

● Store state in DynamoDB, S3, etc...

● Can cache in /tmp - unreliably

Will run if...

● Configured to act on events (e.g. S3 bucket writes)

● Directly invoked via the API

● Scheduled to

● Configured as an endpoint for API Gateway○ This allows its use in serverless

Page 6: Lambda and serverless - DevOps North East Jan 2017

Lambda Functions

Can call primitive Linux utilities

Can create threads/processes

● These can be frozen

Package function in a zip

● Can include libraries

● Can include native libraries

● Can include native binaries

● Max 50 MB compressed/250 MB uncompressed

Page 7: Lambda and serverless - DevOps North East Jan 2017

Lambda Function Configuration

Allocate memory

● 128 MB → 1536 MB

● Affects CPU resources allocated

Set timeout (up to 300 seconds)

Can VPC-enable with an ENI (so can access private resources)

Can set up dead letter queue (Lambda retries failures twice)

Environment vars, versioning, aliases

Page 8: Lambda and serverless - DevOps North East Jan 2017

Lambda Function Execution

From cold:

1. New container is created

2. Lambda function code copied to it

3. Lambda function is initialised

4. Lambda function is executed

5. Container is frozen when function terminates/times out○ This also can be referred to as “warmed”/”hot” - yay, confusion!

Steps #1 -> #3 can take time, ~1 to 9 seconds

Python least delay from cold, Java longest

Page 9: Lambda and serverless - DevOps North East Jan 2017

Lambda Function Execution

If warm container is used:

1. Container is thawed

2. Lambda function is executed

3. Container is frozen when function terminates/timeout

Step #1 takes < 100ms

Java has least delay when warm

Note: no guarantees that warm container will be used

Page 10: Lambda and serverless - DevOps North East Jan 2017

Lambda in use

Page 11: Lambda and serverless - DevOps North East Jan 2017

Lambda in use in production

Web form serverless backend

Virus scanning & resizing uploaded files

Simple automatic image thumbnailer

Connecting AWS alarms to internal monitoring

Dynamically calculating subnet CIDRs for CloudFormation stacks

Processing config changes & alerting on unsafe actions

...and more

Page 12: Lambda and serverless - DevOps North East Jan 2017

Benefits of Lambda

Quick to create and deploy

Connective tissue for AWS resources

Reduce server maintenance

Potentially unlimited instant scalability

Smaller attack surface

Cheap! (Generous permanent free tier)

● 1m requests/month free, $0.0000002 per request after

● 400,000 GB-seconds compute/month free, $0.00001667 per GB-s after

Page 13: Lambda and serverless - DevOps North East Jan 2017

Serverless

Page 14: Lambda and serverless - DevOps North East Jan 2017

Voting Application

An example serverless application

https://github.com/infectedsoundsystem/lambda-refarch-webapp

Page 15: Lambda and serverless - DevOps North East Jan 2017

Voting Application Components

Page 16: Lambda and serverless - DevOps North East Jan 2017

AWS CloudFormation

To provision the AWS resources

Uses JSON/YAML templates

Allows to define infrastructure as code

Transforms AWS Serverless Application Model definitions

Page 17: Lambda and serverless - DevOps North East Jan 2017

Amazon S3

Object storage

One bucket stores the Lambda code to deploy via CloudFormation

Another bucket stores the website HTML and assets

This bucket is configured for static website hosting

Page 18: Lambda and serverless - DevOps North East Jan 2017

Amazon DynamoDB

NoSQL database PaaS with built-in HA/durability

Two tables, one for votes and one for vote aggregation

DynamoDB Streams enable reading and processing updates to the vote table

Page 19: Lambda and serverless - DevOps North East Jan 2017

Amazon Cognito

Service that provides identity management and data sync

Primarily used for mobile applications

Used here to provide an identity pool to allow browser-side code to run as an

unauthenticated user that has IAM permissions to scan the DynamoDB vote

aggregation table directly

Page 20: Lambda and serverless - DevOps North East Jan 2017

Amazon API Gateway

Allows to create publicly available REST APIs

Done by creating resources, and methods for these

These manage the requests and responses when invoking our Lambda function

One resource used here for voting via the website

Gives a public HTTPS endpoint for a Lambda function to be triggered via an

Ajax request containing the user’s vote

Page 21: Lambda and serverless - DevOps North East Jan 2017

AWS Lambda

Four Lambda functions used here:

One to register votes received via the website

One that listens to a DynamoDB stream from the votes table, and aggregates the votes when

there are changes

One used as CloudFormation custom resource to create/configure our Cognito Identity Pool

One used as CFN custom resource to automatically configure static files with resource values

Page 22: Lambda and serverless - DevOps North East Jan 2017

AWS Serverless Application Model

Used to define serverless applications on AWS

Based on CloudFormation

Defined in CFN templates and deployed as CFN stack

SAM objects simplify definition of common serverless resources

CloudFormation transformation expands these to normal CFN resource

definitions

https://github.com/awslabs/serverless-application-model

Page 23: Lambda and serverless - DevOps North East Jan 2017

Putting it all together...

Page 24: Lambda and serverless - DevOps North East Jan 2017

Demo

Page 25: Lambda and serverless - DevOps North East Jan 2017

Demo

Page 26: Lambda and serverless - DevOps North East Jan 2017

Please go to http://done.1f55.uk

Flame war!

Page 27: Lambda and serverless - DevOps North East Jan 2017

Benefits of Serverless

Page 28: Lambda and serverless - DevOps North East Jan 2017

Over traditional server architectures

Modularity and separation

Isolated deployments and failures

No ongoing server maintenance

Potentially unlimited instant scalability

Smaller attack surface

Cheap!

Page 29: Lambda and serverless - DevOps North East Jan 2017

> 90%Running cost reduction when compared to a traditional HA server architecture.

(That’s before factoring in additional cost savings from reduced man-hours

incurred performing maintenance etc...)

Page 30: Lambda and serverless - DevOps North East Jan 2017

Downsides & Hurdles

Page 31: Lambda and serverless - DevOps North East Jan 2017

Compared to traditional server architectures

Not every web application will be suited for it

Occasional latency spike due to Lambda container creation (additional ~2-3 secs

average for Node.js runtime)

Requires initial learning curve to enable design and development of appropriate

application architectures

Page 32: Lambda and serverless - DevOps North East Jan 2017

One last benefit...

Page 33: Lambda and serverless - DevOps North East Jan 2017

All management of the application can be done in code.

Page 34: Lambda and serverless - DevOps North East Jan 2017

Questions?

@mikeshutlar

linkedin.com/in/MikeShutlar

git.io/vMMm8

Thanks for voting!