Introduction to Amazon API Gateway
Introduction to Amazon API Gateway Overview In this lab, you will create a simple FAQ micro-service. The micro-service will return a JSON object containing a random question and answer pair using an API Gateway endpoint that invokes a Lambda function. Here is the architecture pattern for our simple micro-service: Topics covered By the end of this lab you will be able to: Microservice Architecture "The microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies" – Fowler and Lewis, 2014 The idea of a microservices architecture is to take a large, complex system and break it down into independent, decoupled, easy to manage and extend services. This enables developers to meet their key design goals like extensibility, extendability, availability and maintainability. Amazon API Gateway and AWS Lambda provide the perfect combination of web services to effortlessly build, deliver and maintain a suite of microservices that can be the foundation of complex software systems. In today's lab, you will learn how to develop, deploy and debug a simple microservice that would represent one part of a potentially much larger system. It will consist of two pieces, first is the RESTful API and second is the function that is executed when a user hits the endpoint. Application Programming Interface (API) An application programming interface is a set of instructions that defines how developers interface with an application. The idea behind an API is to create a standardized approach to interfacing the various services provided by an application. An API is designed to be used with software development kits (SDKs). A software development kit is a collection of tools that allows developers to easily create downstream applications based on the API. API-First Strategy Many software organizations are adopting an API-First strategy, where each service within their stack is first and always released as an API. When designing a service, it is hard to know all of the various applications that may want to utilize the service. For instance, the FAQ service we are building today would work great to seed FAQ pages on an external website. However, it is feasible to think that a cloud education company would also want to ingest FAQ within their training materials, for things like flash cards or training documents. If we simply built a static website, the ingestion process for the education company would not be possible, or be very difficult. By providing an API that can be consumed in a standardized format, we are enabling the development of an ecosystem around the service, and use cases that were not initially considered. RESTful API Representational state transfer (REST) refers to architectures that follow six constraints: In the system we are developing today, we are following a RESTful model. Clients send requests to the back—end lambda functions (server). The logic of service is encapsulated within the Lambda function and we are providing a uniform interface for clients to use. Best Practices for Building a RESTful API A key goal of building an API is to help establish an ecosystem of innovation around your set of services. Therefore, it is important to make your API intuitive and easy-to-use. Here is a common naming and method scheme to follow: GET /questions Returns all of the questions. GET /questions/17 Returns the question number 17. POST /questions Creates a new question. PUT /questions/17 Updates question number 17. PATCH /questions/17 Partially updates question number 17. DELETE /questions/17 Deletes question number 17. Note: Notice how to get a specific question, the API endpoint is NOT /question/namebut instead /questions/identifier. This enables the API designer to provide functionality to return groups of questions (could be all questions) with the /questionsendpoint as well as single record responses with the /questions/identifier. For more information, see the additional resources section at the bottom of this lab guide. A few good examples of RESTful API's to look at are: Amazon API Gateway and AWS Lambda A microservice using Amazon API Gateway consists of a defined resource and associated methods (GET, POST, PUT, etc.) in API Gateway as well as the backend target. In today's lab, the backend target will be a Lambda function. However, the backend target could be another HTTP endpoint (a third-party API or listening web server), an AWS service proxy or a mock integration to be used as a placeholder. Amazon API Gateway Introduction "Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale." API Gateway is a managed service provided by AWS that makes creating, deploying and maintaining API's easy. API Gateway includes features to: Amazon API Gateway and AWS Lambda Terminology Resource A resource is represented as a URL endpoint and path. For example, api.mysite.com/questions. You can associate HTTP methods with resources and define different backend targets for each method. In a microservices architecture, a resource would represent a single microservice within your system. Method In API Gateway, a method is identified by the combination of a resource path and an HTTP verb, such as GET, POST, and DELETE. Method Request The method request settings in API gateway store the method's authorization settings and define the URL Query String parameters and HTTP Request Headers that are received from the client. Integration Request The integration request settings define the backend target used with the method. It is also where you can define mapping templates, to transform the incoming request to match what the backend target is expecting. Integration Response The integration response settings is where the mappings are defined between the response from the backend target and the method response in API Gateway. You can also transform the data that is returned from your backend target to fit what your end users / applications are expecting. Method Response The method response settings define the method response types, their headers and content types. Model In API Gateway, a model defines the format, also known as the schema or shape, of some data. You create and use models to make it easier to create mapping templates. Because API Gateway is designed to work primarily with JavaScript Object Notation (JSON)-formatted data, API Gateway uses JSON Schema to define the expected schema of the data. Stage In API Gateway, a stage defines the path through which an API deployment is accessible. This is commonly used to deviate between versions, as well as development vs production endpoints, etc. Blueprint A Lambda blueprint is an example lambda function that can be used as a base to build out new Lambda functions. Build Your Microservice Create a New Lambda Function Using Example Code console.log('Loading event...'); var json = { "service": "lambda", "reference": "https://aws.amazon.com/lambda/faqs/", "questions": [{ "q": "What is AWS Lambda?", "a": "AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no charge when your code is not running. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app." },{ "q":"What is the JVM environment Lambda uses for execution of my function?", "a":"Lambda provides the Amazon Linux build of openjdk 1.8." } ] } exports.handler = function(event, context) { var rand = Math.floor(Math.random() * json.questions.length); console.log("Quote selected: ", rand); var response = { body: JSON.stringify(json.questions[rand]) }; console.log(response); context.succeed(response); }; Your screen should appear as in the following picture: Note: Pricing of lambda function execution is dependent on the time it takes to complete the function and the amount of memory allocated to the function. Congratulations! You have created your API Gateway and Lambda function. Test the Lambda Function Congratulations! You have successfully tested and debugged your Lambda function! Troubleshooting Here are a few common errors and how to remedy them. Conclusion Congratulations! You have completed this lab and have successfully created a microservice with API Gateway and Lambda. You now know how to: Next Steps This lab was intended to give you an introduction into microservices architecture patterns, why they are important and how to setup a basic open API. Now that you have completed the lab, take it a step further! Here are some additional things you could do to extend the functionality of your microservice in your own AWS account: Additional Resources
API Gateway: ycwn10dd4b
https://ycwn10dd4b.execute-api.us-west-2.amazonaws.com/prod/whelambdafaq
Deployment stage: prod Method: ANY
CTRL-Click on your new API URL link for a new browser tab, in which you should see one FAQ entry returned.
<< Home