using aws lambda and api gateway from a web context (.pdf)

33
Using AWS Lambda and API Gateway from a web context

Upload: truongcong

Post on 13-Feb-2017

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using AWS Lambda and API Gateway from a web context (.pdf)

Using AWS Lambda and API Gateway from a web context

Page 2: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

About meOliver Gutperl

Background: Software Dev

now: Kapitän @

past:

CTO @ parku,

VP, Technology @ StayFriends

Author: „Nginx richtig konfigurieren“

2

Page 3: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

Web Context

A Web-Browser makes an HTTP-Request

3

Page 4: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

AWS Lambda

Serverless

Lambda is to logic what S3 is to data

Supports JavaScript(Node.js), Java, Python

4

Page 5: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

Minimal Lambda function (Node.js)

exports.handler = function(event, context, callback) { console.log(event); callback(null, { output: 'Hello World' });};

5

Page 6: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

💰Pricing1 million invocations: 0.20 USD

1 GB/second computing time: 0.00001667 USD

• smallest units (128 MB/100 msec)

• Node.js: 128 MB are possible, runtime ~ 300 msec

Human readable estimate

• 1 million Invocations ~ 1.20 USD

Free per month

• 1 million invocations, 400 000 GB/s

6

Page 7: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

🗜Limits

„None“

100 simultaneous invocations per account and region

• only for safety

• AWS support will remove it

7

Page 8: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

🏞Environment

Default: outside VPC

Can run inside existing VPC

• VPC IP addresses required for invocations

• RDS (MySQL, etc.) access

😎ProTip: Existing Webserver on EC2 as proxy to other AWS Services

8

Page 9: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

🚀Deployment

zip -r lambda-function.zip lambda-function.js && aws lambda update-function-code --function-name lambda-function --zip-file fileb://lambda-function.zip —publish

require() works

• include npm-packages in zip file

aws-sdk already available

9

Page 10: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

🛎Invocation

From various AWS services

Function name

Versioning available

• Version or Alias

IAM

• Roles

10

Page 11: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

🐞Debugging

Logs go into CloudWatch

Easy to mock

exports.lambda({ input : 'Hello Lambda' }, null, function(err, value) { if (err) { console.log('Error', err); } else { console.log(value); }});

11

Page 12: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

😎ProTips

Cold start can take 3-5 seconds

Function goes stale after x minutes

Provide No-OP for Kickstart

• Call from 1st user interaction

• Use CloudWatch Event Rule to call periodically (cron)

12

Page 13: Using AWS Lambda and API Gateway from a web context (.pdf)

Demo

Page 14: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

API Gateway

HTTP Frontend for Lambda

Focus on REST/JSON

Point-and-click REST API creator 😨

Swagger import/export

14

Page 15: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

💰Pricing

3.50 USD per million incoming requests

0.09 USD per GB of outgoing traffic

15

Page 16: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

🔩Building Blocks

Resources/methods

Stages

Models

16

Page 17: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

API definition

17

Page 18: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

API definition

18

Page 19: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

API definition

19

Page 20: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

Integration Request

Based on Content-Type

Used to pass request metadata to Lambda function

Uses Apache Velocity Template Language (VTL) 😨

20

Page 21: Using AWS Lambda and API Gateway from a web context (.pdf)

Digitalkapitaen

#set($allParams = $input.params()){"body-json" : $input.json('$'),"params" : {#foreach($type in $allParams.keySet()) #set($params = $allParams.get($type))"$type" : { #foreach($paramName in $params.keySet()) "$paramName" : "$util.escapeJavaScript($params.get($paramName))" #if($foreach.hasNext),#end #end} #if($foreach.hasNext),#end#end},"stage-variables" : {#foreach($key in $stageVariables.keySet())"$key" : "$util.escapeJavaScript($stageVariables.get($key))" #if($foreach.hasNext),#end#end},"context" : { "account-id" : „$context.identity.accountId", ... }}

21

Page 22: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

API definition

22

Page 23: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

Integration ResponseDefine Model for JSON Response

• in JSON-Schema

VTL (again) to map from Lambda output to API Gateway Response 🤔

HTTP Response Status (other than 200)

• derived from Regular Expression on Lambda error output 😳

Headers need to be defined in advance

23

Page 24: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

DeploymentStages („prod“, „test“, …)

Stage variables

• passed to Lambda function

Create SDKs

• Android, iOS, JavaScript

API Keys

• Quotas

• Throttling

24

Page 25: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

🎭API Gateway shortcomings

Plain HTML output is difficult

Status codes, headers difficult to define

URL-Space design is limited (only placeholders)

Limits Lambda Functions to REST/JSON

25

Page 26: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

☄WishlistManage URL-Space

• not just REST

Process POST forms

• e.g. contact form to email

Set headers easily

• 301, 302, 304, …

Generate HTML

• not just JSON

26

Page 27: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

An alternative

Lambda Proxy

• Very thin proxy

• Connects web server to Lambda function

• Open Source

• (PHP version available)

27

Page 28: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

Installation

# sudo npm install lambda-proxy -g# lambda-proxy &

28

Page 29: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

nginx.conf server { set $lambdaregion 'eu-west-1'; set $lambdaqualifier ''; set $lambdaparameters '';

proxy_set_header 'lambda-proxy-region' '$lambdaregion'; proxy_set_header 'lambda-proxy-qualifier' '$lambdaqualifier'; proxy_set_header 'lambda-proxy-function' '$lambdafunction'; proxy_set_header 'lambda-proxy-parameters' '$lambdaparameters'; proxy_set_header 'lambda-proxy-scheme' '$scheme'; proxy_set_header 'lambda-proxy-host' '$host';

location /some/location { set $lambdafunction 'handle-form'; proxy_pass http://localhost:8080; } }

29

Page 30: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

Minimal Lambda function

exports.lambda = function(event, context, callback) {   console.log(event);   callback(null, {     status: 200, headers: { 'Content-Type': 'text/plain' },    body: 'Hello World'   }); }

30

Page 31: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

Defined event structure{   method: 'POST',   scheme: 'https',   host: 'www.digital-sailors.de',  url: '/some/url',  httpVersion: '1.0',   parameters: {     'from_webserver': 'private value'  },   headers: {     'Set-Cookie': 'ACookie=MadeOfChocolate'  },   body: 'parameter=value' }

31

Page 32: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

🏄Try it

https://github.com/digital-sailors/lambda-proxy

Contributors welcome!

32

Page 33: Using AWS Lambda and API Gateway from a web context (.pdf)

@Digitalkapitaen

Questions?

[email protected]

@Digitalkapitaen

🌍 https://www.digital-sailors.de

33