API stands for “Application Programming Interface”. Amazon API gateway is the front door for your APIs on AWS cloud. An API Gateway provides a set of API endpoints and encapsulates the internal structure without revealing how it is being constructed.
API代表“應用程序編程接口” 。 Amazon API網關是AWS雲上API的前門。 API網關提供了一組API端點並封裝了內部結構,而沒有透露其構造方式。
Amazon API gateway process and handles the request and response from thousands and millions of concurrent API calls. Similar to other services on AWS cloud, it is also build to massively scale.
Amazon API網關處理並處理來自成千上萬並發API調用的請求和響應。 與AWS雲上的其他服務類似,它也可以大規模構建。
AWS offers Amazon API Gateway service on a very reasonable cost and also available in AWS free tier. It allows you 1 million API calls in a month for free in the free tier and for 12 months in a row. If your application use exceeds the tiers, you simply pay standard, pay-as-you-go service rates.
AWS以非常合理的價格提供了Amazon API Gateway服務,並且還提供AWS免費套餐。 它允許您每月免費獲得一次免費的API調用,並且可以連續12個月進行一百萬次API調用。 如果您的應用程序使用超出了這些級別,則只需支付標准的即付即用服務費。
什么是API網關? (What is API Gateway?)
To understand and use Amazon API Gateway, you need to know what is an API Gateway and its applications.
要了解和使用Amazon API Gateway,您需要知道什么是API Gateway及其應用程序。
In a large application, you may have more than one microservice running and all of them exposes some of the API endpoints. You can connect to these API endpoints from the microservice url. But think about when you consume these API endpoints in your UI or give them to some third party. You will end up having many URLs. And for each of the service, you have to manage cache, throttling, etc separately.
在大型應用程序中,您可能正在運行多個微服務,並且所有微服務都公開了一些API端點。 您可以從微服務網址連接到這些API端點。 但是請考慮一下何時在用戶界面中使用這些API終結點或將它們提供給第三方。 您最終將擁有許多URL。 對於每項服務,您都必須分別管理緩存,限制等。
API gateway removes having several URLs for REST services and provides you a front door for your APIs. It encapsulates the internal structure of your services. And restricts the direct access to your APIs which makes the APIs secure.
API網關刪除了用於REST服務的多個URL,並為您的API提供了前門。 它封裝了服務的內部結構。 並限制對API的直接訪問,這使API安全。
Mostly, API Gateways are a proxy based application which routes the request to the right resources. And take care of all the front door activities like throttling, security, load balancing, logging, authentication, and authorization, etc.
通常,API網關是基於代理的應用程序,可將請求路由到正確的資源。 並注意所有前門活動,例如節流,安全性,負載平衡,日志記錄,身份驗證和授權等。
Amazon API網關 (Amazon API Gateway)
Amazon API Gateway is an AWS service that is the front door for all your APIs running of AWS cloud. In addition, it offers service for creating, publishing, maintaining, monitoring, and securing REST and WebSocket APIs at any scale.
Amazon API Gateway是一項AWS服務,是運行AWS雲的所有API的前門。 此外,它還提供用於創建,發布,維護,監視和保護任意規模的REST和WebSocket API的服務。
You can create a REST API with the help of Amazon API Gateway and deploy it instantly. The API you can use in your application or can also make it available for the third party.
您可以在Amazon API Gateway的幫助下創建REST API並立即進行部署。 您可以在應用程序中使用的API或也可以將其提供給第三方。
Let’s take an example of Amazon API Gateway application to understand it (Source: Amazon Web services):
讓我們以Amazon API Gateway應用程序為例來理解它(來源:Amazon Web服務):

Amazon Api Gateway Application
Amazon Api網關應用程序
The example above explains the Amazon API Gateway and how it fits well with other services. So, in this application, the APIs are built on API Gateway and connected to invoke the lambda functions.
上面的示例說明了Amazon API Gateway以及它如何與其他服務很好地融合在一起。 因此,在此應用程序中,API建立在API網關上,並已連接以調用lambda函數。
If you don’t know about Amazon S3 and how to host public site then please read our post on:
如果您不了解Amazon S3以及如何托管公共站點,請閱讀以下文章:
- Amazon S3 – AWS Simple Storage Service
- Host a host a public static website on Amazon S3?
Note: We will build this application in our next post to give you an end-to-end application overview of Amazon API Gateway, AWS Lambda, and Amazon S3.
注意:我們將在下一篇文章中構建此應用程序,以向您提供Amazon API Gateway,AWS Lambda和Amazon S3的端到端應用程序概述。
建築 (Architecture)
The architecture of Amazon API gateway is quite simple. API Gateway is the internet facing service which connects the request to the specific API calls. Below diagram demonstrate how API Gateway works:
Amazon API網關的架構非常簡單。 API網關是面向互聯網的服務,可將請求連接到特定的API調用。 下圖展示了API Gateway的工作方式:

Amazon Aws Architecture (Source: Amazon Web Services)
Amazon Aws架構(來源:Amazon Web服務)
為什么選擇Amazon API Gateway? (Why Amazon API Gateway?)
Amazon API Gateway offers a number of benefits which makes it one of the highly used services on AWS Cloud platform. Let’s discuss some of many reasons of why use Amazon API Gateway?
Amazon API Gateway具有許多優勢,使其成為AWS Cloud平台上使用率很高的服務之一。 讓我們討論為什么使用Amazon API Gateway的許多原因中的一些原因。
REST端點 (REST Endpoints)
Instead of creating an application which exposes some RESTFul endpoints and deploying it, Amazon API Gateway offers to create REST endpoints without worrying about the application. Create resource-based APIs and use API Gateway’s data transformation capabilities to generate the requests in the language target services expect.
Amazon API Gateway可以創建REST端點,而無需擔心該應用程序,而不是創建公開一些RESTFul端點的應用程序並進行部署。 創建基於資源的API,並使用API Gateway的數據轉換功能以目標服務期望的語言生成請求。
You can just create the API endpoint and connect them to the Lambda functions.
您可以只創建API端點並將其連接到Lambda函數。
安全控制 (Security Controls)
You can make your API highly secure with Amazon Cognito, Identity and Identity Access Management (IAM). You can execute authorization flow from API Gateway which internally executes the Lambda.
您可以使用Amazon Cognito,身份和身份訪問管理(IAM)使您的API高度安全。 您可以從內部執行Lambda的API網關執行授權流程。
API版本控制 (API Versioning)
You can deploy multiple versions of the same API and can run them simultaneously with the help of Amazon API Gateway. The best thing is that you only pay for the usage and not for the multiple deployments.
您可以部署同一API的多個版本,並可以在Amazon API Gateway的幫助下同時運行它們。 最好的事情是,您只需為使用付費,而無需為多個部署付費。
性能規模,流量限制和緩存 (Performance Scale, Traffic Throttle and Cache)
Similar to other services Amazon API Gateway offers high scalability with very low latency. Like AWS CloudFront, it takes advantage of the global network of edge location. Read about Amazon CloudFront & Edge Locations.
與其他服務類似,Amazon API Gateway提供了很高的可擴展性和非常低的延遲。 像AWS CloudFront一樣,它利用了邊緣位置的全球網絡。 了解有關Amazon CloudFront和Edge位置的信息 。
Traffic Throttle and Cache makes sure that backend APIs are not called unnecessarily. Which makes it highly reliable and available.
流量限制和緩存可確保不會不必要地調用后端API。 這使其高度可靠且可用。
監控方式 (Monitoring)
Monitoring the API is an integral part of the Amazon API Gateway. A number of metrics available on Amazon CloudWatch to monitor your APIs. You can create dashboards and set alarms and notification in CloudWatch.
監視API是Amazon API Gateway不可或缺的一部分。 Amazon CloudWatch上有許多指標可用於監視您的API。 您可以創建儀表板,並在CloudWatch中設置警報和通知。
Amazon API Gateway的功能 (Features of Amazon API Gateway)
Amazon API Gateway offers a number of features. Some of them we have already discussed in Why Amazon API Gateway” section.
Amazon API Gateway提供了許多功能。 我們在“ 為什么使用Amazon API Gateway”一節中已經討論了其中的一些。
Amazon API gateway Offers features such as the following:
Amazon API網關提供以下功能:
- CloudWatch Access logging and monitoring which cinludes creation of Alarms and notifications.
- Supports both stateless and statefull APIs. For example – Stateful WebSocket and Stateless RESTFul APIs
- Supports custom domain names for your Amazon API gateway – Instead of using the default API gateway URL, you can set and use your domain.
- Allow multiple version of the same API to run simultaneously
- Ability to use CloudFormation templates to create and do the whole deployent without doing a single manual configuration
- Provides Authorization and Authentication mechanism with Conginito and Lambda funcions
- Caching and request throtteling
- Single point access to all of your APIs
使用示例REST API設置API網關 (Setup API Gateway with sample REST API)
Let’s create an API gateway and a REST API.
讓我們創建一個API網關和一個REST API。
先決條件 (Prerequisite)
- Login to the AWS management console (https://console.aws.amazon.com/)
- Search for “API Gateway” in services
- Click on the “API Gateway” service and you will see the below screen to get started with Amazon API gateway
Get Started With Amazon Api Gateway
Amazon Api Gateway入門
開始使用 (Get Started)
Click on “Get Started” button and you will see the below screen with a sample API specs in Swagger 2.0
單擊“入門”按鈕,您將看到以下屏幕,其中包含Swagger 2.0中的API樣本示例

Create Example Api
創建示例Api
Click on “OK” to import the selected API.
單擊“確定”以導入所選的API。
選擇創建 (Choose to create)
- Choose the protocol – Select whether you would like to create a REST API or a WebSocket API. We will choose “REST” option for the example.
- Create new API – In Amazon API Gateway, a REST API refers to a collection of resources and methods that can be invoked through HTTPS endpoints.
We will choose the “Example API”. You can also create a New API or import from the Swagger Json for existing API.
我們將選擇“ Example API” 。 您還可以創建新API或從Swagger Json導入現有API。 - Example API – The sample API Json that will be used to crate your first API.
You may ignore rest of the options for now and click on “Import” button. See the screen below:
您現在可以忽略其余選項,然后單擊“導入”按鈕。 請參閱以下屏幕:

Import Example Api
導入示例Api
部署API ( Deploy the API)
AWS will take few seconds to create API and Gateway. Once done, you will see the below screen:
AWS將花費幾秒鍾來創建API和網關。 完成后,您將看到以下屏幕:

First Restful Api
第一個寧靜的Api
If you want to add more API end points then you can do so. Click on the “Action”
button and you will see the below options:
如果要添加更多的API端點,則可以這樣做。 點擊“動作”
按鈕,您將看到以下選項:

Api Actions
Api動作
In the API Sections, click on “Deploy API” to deploy the API. The below window will prompt to enter the stage that you want to deploy.
在“ API”部分中,單擊“部署API”以部署API。 下面的窗口將提示您輸入要部署的階段。

Deploy Api
部署Api
查看已部署的API (View Deployed API)
Once you click on the “Deploy” button, AWS will take few seconds/minutes based on the number of API endpoints to deploy, please wait while deployment completes. Once done, you will see the below screen:
一旦單擊“部署”按鈕,AWS將根據要部署的API端點數花費幾秒鍾/幾分鍾,請等待部署完成。 完成后,您將看到以下屏幕:

Api Gateway And Api
Api網關和Api
You can see the URL for your API: https://2ikmujgy4g.execute-api.us-east-1.amazonaws.com/stage
.
Click on the in the new browser window and you will see the below documentation:
您可以看到您的API的URL: https://2ikmujgy4g.execute-api.us-east-1.amazonaws.com/stage
: https://2ikmujgy4g.execute-api.us-east-1.amazonaws.com/stage
。
在新的瀏覽器窗口中單擊,您將看到以下文檔:

Your First Api
你的第一個Api
連接到您的API (Connect to Your API)
You can use postman to connect to your API OR just connect through the curl command.
您可以使用郵遞員連接到您的API,也可以只通過curl命令連接。

Connect To Api
連接到Api