本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Amazon CLI 命令设置边缘优化的 API
使用设置 API Amazon CLI 需要使用create-rest-api
、create-resource
或get-resources
、put-method
、put-method-response
put-integration
、和put-integration-response
命令。以下过程说明如何使用这些 Amazon CLI 命令来创建HTTP
集成类型的简单 PetStore API。
要创建简单的 PetStore API,请使用 Amazon CLI
-
调用
create-rest-api
命令以在特定区域 (RestApi
) 中设置us-west-2
。aws apigateway create-rest-api --name 'Simple PetStore (Amazon CLI)' --region us-west-2
此命令的输出如下:
{ "id": "vaz7da96z6", "name": "Simple PetStore (Amazon CLI)", "createdDate": "2022-12-15T08:07:04-08:00", "apiKeySource": "HEADER", "endpointConfiguration": { "types": [ "EDGE" ] }, "disableExecuteApiEndpoint": false }
记下新创建的
id
返回的RestApi
。您需要使用它来设置 API 的其他部分。 -
调用
get-resources
命令以检索RestApi
的根资源标识符。aws apigateway get-resources --rest-api-id vaz7da96z6 --region us-west-2
此命令的输出如下:
{ "items": [ { "id": "begaltmsm8", "path": "/" } ] }
记下根资源
Id
。您需要使用它开始设置 API 的资源树和配置方法与集成。 -
调用
create-resource
命令以在根资源 (pets
) 下附加子资源 (begaltmsm8
):aws apigateway create-resource --rest-api-id vaz7da96z6 \ --region us-west-2 \ --parent-id begaltmsm8 \ --path-part pets
此命令的输出如下:
{ "id": "6sxz2j", "parentId": "begaltmsm8", "pathPart": "pets", "path": "/pets" }
要在根目录下附加子资源,您可以将根资源
Id
指定为parentId
属性值。同样,要在pets
资源下附加子资源,请重复上述步骤,同时用parent-id
的pets
资源id
替换6sxz2j
值:aws apigateway create-resource --rest-api-id vaz7da96z6 \ --region us-west-2 \ --parent-id 6sxz2j \ --path-part '{petId}'
要使某个路径成为路径参数的一部分,请将其括在一对大括号内。如果成功,该命令会返回以下响应:
{ "id": "rjkmth", "parentId": "6sxz2j", "path": "/pets/{petId}", "pathPart": "{petId}" }
现在,您创建了两个资源:
/pets
(6sxz2j
) 和/pets/{petId}
(rjkmth
),您可以继续为其设置方法。 -
调用
put-method
命令为GET
资源添加/pets
HTTP 方法。这会创建一个具有开放访问权限的Method
的 APIGET /pets
,该方法通过其 ID 值/pets
引用6sxz2j
资源。aws apigateway put-method --rest-api-id vaz7da96z6 \ --resource-id 6sxz2j \ --http-method GET \ --authorization-type "NONE" \ --region us-west-2
此命令的成功输出如下:
{ "httpMethod": "GET", "authorizationType": "NONE", "apiKeyRequired": false }
此方法支持开放式访问,因为
authorization-type
设置为NONE
。要仅允许已验证身份的用户调用此方法,您可以使用 IAM 角色和策略、Lambda 授权方(以前称为自定义授权方)或者 Amazon Cognito 用户池。有关更多信息,请参阅 在 API Gateway 中控制和管理对 REST API 的访问。要启用
/pets/{petId}
资源 (rjkmth
) 的读取访问权限,请为该资源添加GET
HTTP 方法,以按照如下所示创建Method
的 APIGET /pets/{petId}
。aws apigateway put-method --rest-api-id vaz7da96z6 \ --resource-id rjkmth --http-method GET \ --authorization-type "NONE" \ --region us-west-2 \ --request-parameters method.request.path.petId=true
此命令的成功输出如下:
{ "httpMethod": "GET", "authorizationType": "NONE", "apiKeyRequired": false, "requestParameters": { "method.request.path.petId": true } }
请注意,必须将该方法请求路径参数
petId
指定为必需的请求参数,以便将其动态设置值映射到相应的集成请求参数并传递到后端。 -
调用
put-method-response
命令以设置GET /pets
方法的 200 OK 响应,通过其 ID 值/pets
指定6sxz2j
资源。aws apigateway put-method-response --rest-api-id vaz7da96z6 \ --resource-id 6sxz2j --http-method GET \ --status-code 200 --region us-west-2
此命令的输出如下:
{ "statusCode": "200" }
同样,要设置
GET /pets/{petId}
方法的 200 OK 响应,请执行以下操作,通过其资源 ID 值/pets/{petId}
指定rjkmth
资源。aws apigateway put-method-response --rest-api-id vaz7da96z6 \ --resource-id rjkmth --http-method GET \ --status-code 200 --region us-west-2
为 API 设置简单的客户端接口之后,您可以继续设置 API 方法与后端的集成。
-
调用
put-integration
命令以便为Integration
方法设置与指定 HTTP 终端节点的GET /pets
。/pets
资源使用其资源 Id6sxz2j
进行标识:aws apigateway put-integration --rest-api-id vaz7da96z6 \ --resource-id 6sxz2j --http-method GET --type HTTP \ --integration-http-method GET \ --uri 'http://petstore-demo-endpoint.execute-api.com/petstore/pets' \ --region us-west-2
此命令的输出如下:
{ "type": "HTTP", "httpMethod": "GET", "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "connectionType": "INTERNET", "passthroughBehavior": "WHEN_NO_MATCH", "timeoutInMillis": 29000, "cacheNamespace": "6sxz2j", "cacheKeyParameters": [] }
请注意,
uri
的集成http://petstore-demo-endpoint.execute-api.com/petstore/pets
指定了GET /pets
方法的集成终端节点。同样,您将按照以下所示为
GET /pets/{petId}
方法创建集成请求:aws apigateway put-integration \ --rest-api-id vaz7da96z6 \ --resource-id rjkmth \ --http-method GET \ --type HTTP \ --integration-http-method GET \ --uri 'http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}' \ --request-parameters '{"integration.request.path.id":"method.request.path.petId"}' \ --region us-west-2
其中,
uri
的集成终端节点http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}
也使用路径参数 (id
)。其值从{petId}
的相应方法请求路径参数进行映射。该映射被定义为request-parameters
的一部分。如果此处未定义此映射,客户端尝试调用此方法时会收到错误响应。此命令的输出如下:
{ "type": "HTTP", "httpMethod": "GET", "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}", "connectionType": "INTERNET", "requestParameters": { "integration.request.path.id": "method.request.path.petId" }, "passthroughBehavior": "WHEN_NO_MATCH", "timeoutInMillis": 29000, "cacheNamespace": "rjkmth", "cacheKeyParameters": [] }
-
调用
put-integration-response
命令以创建与 HTTP 后端集成的IntegrationResponse
方法的GET /pets
。aws apigateway put-integration-response --rest-api-id vaz7da96z6 \ --resource-id 6sxz2j --http-method GET \ --status-code 200 --selection-pattern "" \ --region us-west-2
此命令的输出如下:
{ "statusCode": "200", "selectionPattern": "" }
同样,调用以下
put-integration-response
命令以创建IntegrationResponse
方法的GET /pets/{petId}
。aws apigateway put-integration-response --rest-api-id vaz7da96z6 \ --resource-id rjkmth --http-method GET --status-code 200 --selection-pattern "" --region us-west-2
通过上述步骤,您完成了一个简单的 API 的设置,该API允许您的客户在 PetStore 网站上查询可用的宠物,并查看具有指定标识符的单只宠物。为了让您的客户能够调用该 API,您必须部署该 API。
-
将 API 部署到某个
stage
阶段 (例如通过调用create-deployment
):aws apigateway create-deployment --rest-api-id vaz7da96z6 \ --region us-west-2 \ --stage-name test \ --stage-description 'Test stage' \ --description 'First deployment'
此命令的输出如下:
{ "id": "ab1c1d", "description": "First deployment", "createdDate": "2022-12-15T08:44:13-08:00" }
您可以在浏览器中键入 https://vaz7da96z6.execute-api.us-west-2.amazonaws.com/test/pets
URL 并将 vaz7da96z6
替换为您的 API 的标识符,从而测试此 API。预期的输出应如下所示:
[ { "id": 1, "type": "dog", "price": 249.99 }, { "id": 2, "type": "cat", "price": 124.99 }, { "id": 3, "type": "fish", "price": 0.99 } ]
要测试 GET /pets/{petId}
方法,请在浏览器中键入 https://vaz7da96z6.execute-api.us-west-2.amazonaws.com/test/pets/3
。您应该会收到以下响应:
{ "id": 3, "type": "fish", "price": 0.99 }