APIs are changing the way that developers build their applications, from simple websites and mobile apps to Internet of Things applications. At the same time, many developers are moving away from traditional monolithic application architectures to creating smaller microservices, each with its own API. Building microservices has benefits like increased speed to market, reduced potential for errors when deploying updates, and greater flexibility in application design. In this session, we’ll talk about how you can get started building API-based microservices by using Amazon API Gateway. We’ll cover API Gateway’s core features, including how it can help you create standardized APIs and its authentication and authorization features. By the end, you’ll have a foundation for how you can build public or private APIs for backends running on server-based or container-based applications and serverless applications.
15. API Gateway integrations
Internet
Mobile Apps
Websites
Services
AWS Lambda
functions
AWS
API Gateway
Cache
Endpoints on
Amazon EC2
Amazon
CloudWatch
Monitoring
Amazon
CloudFront
Any other AWS
service
YOUR VPC
Endpoints in
Your VPC
Regional API Endpoints
All publicly accessible
endpoints
AWS Lambda
functions
17. Internet
Mobile Apps
Websites
Services
AWS Lambda
functions
AWS
API Gateway
Cache
Endpoints on
Amazon EC2
Amazon
CloudWatch
Monitoring
Amazon
CloudFront
Any other AWS
service
YOUR VPC
Endpoints in
Your VPC
Regional API Endpoints
All publicly accessible
endpoints
AWS Lambda
functions
API Gateway backend integrations
29. API Gateway Lambda Authorizers
Use an AWS Lambda function to invoke whatever authorizer
logic/method you want!
For example this HTTP Basic Auth example which can be found in the
AWS Serverless Application Repository:
45. Metrics and logging are a universal right!
CloudWatch Logs:
• API Gateway Logging
• 2 Levels of logging, ERROR and INFO
• Optionally log method request/body content
• Set globally in stage, or override per method
• Lambda Logging
• Logging directly from your code with your language’s equivalent
of console.log()
• Basic request information included
• Log Pivots
• Build metrics based on log filters
• Jump to logs that generated metrics
• Export logs to AWS ElastiCache or S3
• Explore with Kibana or Athena/QuickSight
46. Metrics and logging are a universal right!
CloudWatch Logs:
• API Gateway Logging
• 2 Levels of logging, ERROR and INFO
• Optionally log method request/body content
• Set globally in stage, or override per method
• Lambda Logging
• Logging directly from your code with your language’s equivalent
of console.log()
• Basic request information included
• Log Pivots
• Build metrics based on log filters
• Jump to logs that generated metrics
• Export logs to AWS ElastiCache or S3
• Explore with Kibana or Athena/QuickSight
54. Node.js framework for deploying projects
to AWS Lambda and Amazon API
Gateway
• Has sub projects for microservices,
chat bots and APIs
• Simplified deployment with a single
command
• Use standard NPM packages, no need
to learn swagger
• Manage multiple versions
https://claudiajs.com
https://github.com/claudiajs/claudia
app.js:
var ApiBuilder = require('claudia-api-
builder')
var api = new ApiBuilder();
module.exports = api;
api.get('/hello', function () {
return 'hello world';
});
$ claudia create --region us-east-1 --api-module app
ClaudiaJS
55. Chalice
Python serverless “microframework” for
AWS Lambda and Amazon API Gateway
• A command line tool for creating,
deploying, and managing your app
• A familiar and easy to use API for
declaring views in python code
• Automatic Amazon IAM policy
generation
https://github.com/aws/chalice
https://chalice.readthedocs.io
app.py:
from chalice import Chalice
app = Chalice(app_name="helloworld")
@app.route("/")
def index():
return {"hello": "world"}
$chalice deploy
Chalice
60. SAM template
AWSTemplateFormatVersion: '2010-09-09’
Transform: AWS::Serverless-2016-10-31
Resources:
GetHtmlFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/todo_list.zip
Handler: index.gethtml
Runtime: nodejs4.3
Policies: AmazonDynamoDBReadOnlyAccess
Events:
GetHtml:
Type: Api
Properties:
Path: /{proxy+}
Method: ANY
ListTable:
Type: AWS::Serverless::SimpleTable
Tells CloudFormation this is a SAM
template it needs to “transform”
Creates a Lambda function with the
referenced managed IAM policy,
runtime, code at the referenced zip
location, and handler as defined.
Also creates an API Gateway and
takes care of all
mapping/permissions necessary
Creates a DynamoDB table with 5
Read & Write units
62. AWS SAM CLI SAM Local
Relaunched/GA’d on May 8th!
CLI tool for local building, validating, testing of
serverless apps
Works with Lambda functions and “proxy-style”
APIs
Response object and function logs available on
your local machine
Uses open source docker-lambda images to mimic
Lambda’s execution environment:
• Emulates timeout, memory limits, runtimes
https://github.com/awslabs/aws-sam-cli
https://www.flickr.com/photos/finklez/5650569363/
Marc Andreessen
Steve Willmott - https://www.linkedin.com/in/stevenwillmott/ (head of API Infra for RedHat)
Using Swagger we can configure our API Gateway to understand our API entrypoints. We can then use API Gateway to create a client SDK, say in node.js if we had other microservices that were in node. We could then turn that into an NPM, put it in a private NPM repo, and make it available for internal clients to use.
Now lets say we want to introduce new client features, without breaking old ones. We can now again do our swagger import, setup our API gateway, generate an SDK, take that SDK and turn it into an NPM, and upload it to our node.js repo for this other service client to use. All without the need to force the old client to change.
SAM Local is a CLI tool that allows customers to test their SAM-based Lambda functions locally, before deploying code to Lambda. SAM Local currently supports functions in Node.js, Java and Python. If your function is fronted by API Gateway, SAM Local will allow you to ping APIs to invoke your function. Alternatively, you can use SAM Local’s “event payload generator” to quickly create a mock event to invoke your function with. After your function executes, you can view the response or examine the logs, all on your local machine. SAM local automatically executes your functions in a sandboxed environment that mimics Lambda’s execution environment, by leveraging docker-lambda Docker images.