2016 - serverless microservices on aws with api gateway and lambda

59
Building Microservices with API Gateway and Lambda @mattjbarlow github.com/ mattjbarlow DevOps Days Austin 2016 Revenge of the Devs

Upload: devopsdaysaustin

Post on 11-Jan-2017

709 views

Category:

Software


0 download

TRANSCRIPT

Page 1: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Building Microservices with API Gateway and Lambda

@mattjbarlowgithub.com/mattjbarlow

DevOps Days Austin 2016Revenge of the Devs

Page 2: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

What we will talk aboutSwagger

API Gateway

Lambda

DynamoDB

Page 3: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

What we will talk aboutSwagger

API Gateway

Lambda

DynamoDB

API Spec File

Proxy / Router / URL

Backend Code

Data

Page 4: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

With Swagger you can auto-generate...Documentation Monitoring Tests

Integration Tests

Client Libraries

Page 5: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

We Never Edit API Gateway Directly We Edit Swagger

which is imported into...

API Gateway Monitoring Docs Tests

Page 6: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Example Microservice

Teammates want the ability to run a Lambda Function exactly one time in the future.

Page 7: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Solution

At Job Microservice

A microservice that mimics the Unix at command for Lambda.

Page 8: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

The At Command

First appears in 1979 as part of Unix Version 7.

Page 9: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Design

Page 10: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

API MethodsPath Operation OperationID Description

/atq GET list_jobs List all jobs

/atq POST create_job Create an at job

/atq/{id} GET describe_job Describe an at job

/atq/{id} DELETE delete_job Delete an at job

A method is a combination of a resource path and an operation.

Page 11: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Objects

Name Type Format Required?

jobid string uuid yes

lambdaArn string arn no

time string dateTime no

atJob

The object will define the response that our API returns.

Page 12: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

CodeOperationID Pseudocode

list_jobs DynamoDB BatchGetItem

describe_job DynamoDB Query on jobid

create_job Create CloudWatch Event and PutItem into DynamoDB.

delete_job Delete CloudWatch Event and DeleteItem out of DynamoDB.

These Operation IDs are defined in Swagger and passed through to Lambda code as part of the event object.

Page 13: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Initialize Directory

Page 14: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

git clone [email protected]:mattjbarlow/microservice-template.git

File Description

service/service.py * Python module that will run in Lambda.

circle.yml Circle CI instructions.

deploy.yml Ansible playbook for provisioning microservice.

destroy.yml Ansible playbook for deleting microservice.

swagger.yml * Spec file that describes your API.

template.json AWS resources required by the microservice.

version.yml Ansible playbook that versions your Lambda code.

* The bulk of your edits will be in these two files.

Page 15: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Update Swagger Paths

Page 16: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Remember these?Path Operation OperationID Description

/atq GET list_jobs List all jobs

/atq POST create_job Create an at job

/atq/{id} GET describe_job Describe an at job

/atq/{id} DELETE delete_job Delete an at job

Page 17: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

We Insert Them Directly Into SwaggerRESOURCE PATH

HTTP OPERATION

Object Definition

Page 18: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Then we define our objects

Page 19: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Deploy

Page 20: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

ansible-playbook -e “prefix=devopsdays” deploy.yml

Page 21: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Ansible Deployment Playbook

1. Zips up Python module and uploads it to S3

2. Creates the API Gateway

3. Provisions AWS resources

a. Lambda

b. DynamoDB

c. IAM Roles and Policies

d. Lambda Permission

4. Adds mapping templates to API Gateway

5. Sets stage variables

6. Exports Swagger file from API Gateway

Page 22: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Import Swagger Into Postman

Page 23: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Open Postman

Page 24: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Click Import

Page 25: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Select Swagger File

Page 26: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Start Sending Requests

Page 27: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Tail Lambda Logs

Page 28: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Logging in Lambda

API Gateway turned our HTTP request data into an Event Object. To start with, we log the entire Event Object.

Page 29: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

pip install awslogs

awslogs allows us to mimic tail -f behavior on our Lambda function’s Log Group.

Page 30: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Event Object Close-Up

Page 31: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Post Body

Headers

Path ParamsQuery Params

Page 32: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

API Gateway Variables

Helpful for filtering logs.

Page 33: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Tracing Our Request

Page 34: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

GET Request Lifecycle

Method Response

Integration Response

Lambda

Method Request

Integration Request

Client

Cloudwatch Logs

Page 35: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

HTTP GET Request Is Sent

GET /dev/v1/atq HTTP/1.1Host: cg4e6xg82i.execute-api.us-east-1.amazonaws.comConnection: keep-aliveCache-Control: no-cacheUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36Postman-Token: 265fcfe1-e196-0114-89bf-442a34c0180dAccept: */*Accept-Encoding: gzip, deflate, sdchAccept-Language: en-US,en;q=0.8

Page 36: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Mapped To Event Object

Page 37: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Event Object Received by Lambda

Page 38: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Logged to CloudWatch

Page 39: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Editing Code

Page 40: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Dev WorkflowEdit Eval

Push

Request

Parse

Page 41: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

create_job function

Page 42: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Push Code To Lambda

Page 43: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

ansible-playbook -e “prefix=dod” version.yml

Page 44: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Test create_jobImportant!

Page 45: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

create_job response

Page 46: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Persisted in DynamoDB

Page 47: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

POST Request Lifecycle

Method Response

Integration Response

Lambda

Method Request

Integration Request

Client

Cloudwatch Logs

EventsDynamo

Page 48: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Removing Lambda

Page 49: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

GET Request With LambdaStep What Happens

Send GET request API Gateway Receives HTTP Request Data from client.

Transform API Gateway Transforms Request Data into Event Object

Proxy API Gateway POSTs Event Object to Lambda

Read Lambda Reads data from DynamoDB

Return API Gateway Receives return values from Lambda

Transform API Gateway Transforms backend data into HTTP Response

Respond API Gateway Responds back to the client.

Page 50: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

GET Request without LambdaStep What Happens

Send GET request API Gateway Receives HTTP Request Data from client.

Transform API Gateway Transforms Request Data into Event Object

Proxy API Gateway POSTs DynamoDB Query to Dynamo

Read Lambda Reads data from DynamoDB

Return API Gateway Receives return values from Dynamo

Transform API Gateway Transforms backend data into HTTP Response

Respond API Gateway Responds back to the client.

Page 51: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Mapping GET Request To DynamoDB

Page 52: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Mapping DynamoDB Response to Client Response

Page 53: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Automating Tests

Page 54: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

CI WorkflowCI

Step 1 Deploy Microservice

Step 2 Validate API Calls

Step 3 Destroy Microservice

git push POST: /project/:tree/:branch

Page 55: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Validating API Calls With Flexschema = load('swagger.awsexport.json')validate_api_call(schema, raw_request=r.request, raw_response=r)

1. Receives Swagger spec file which is our source of truth.2. Makes an HTTP Request to the API Gateway URL.3. Ensures the response matches what we said it would in Swagger.

Loads Swagger spec into memory

Page 56: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

AuthIt has been 1 days since we talked about auth.

Page 57: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Built In Auth OptionsGET /dev/v1/atq HTTP/1.1Host: cg4e6xg82i.execute-api.us-east-1.amazonaws.comConnection: keep-alivex-api-key: bkayZOMvuy8aZOhIgxq94K9Oe7Y70Hw55

Option 2: Signature Version 4 signing with IAM(Powerful, but requires client having AWS API Key)

Option 1: API Keys managed by API Gateway API(Not really useful for user auth in public APIs)

Page 58: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Custom Authorizers

Receive Token from IdentitySend Token to /auth endpoint Pass Through

Responds With JWT

Make request with JWT Custom Authorizer Intercept

Caches temporary policyAllows Request

Generates JWT

Validates JWT

Returns temporary IAM policy

Client Library API Gateway Lambda$:

Validates Identity Token

Page 59: 2016 - Serverless Microservices on AWS with API Gateway and Lambda

Links

https://github.com/mattjbarlow/microservice-template

https://github.com/mattjbarlow/at

http://editor.swagger.io/#/

http://flex-swagger.readthedocs.io/en/latest/