使用 Amazon CLI 命令设置边缘优化的 API
使用 Amazon CLI 设置 API 需要配合使用 create-rest-api、create-resource 或 get-resources、put-method、put-method-response、put-integration 以及 put-integration-response 命令。以下过程显示了如何使用这些 Amazon CLI 命令来创建 HTTP 集成类型的简单 PetStore API。
使用 Amazon CLI 创建简单 PetStore API
-
调用
create-rest-api命令以在特定区域 (RestApi) 中设置us-west-2。aws apigateway create-rest-api --name 'Simple PetStore (Amazon CLI)' --region us-west-2此命令的输出如下:
{ "name": "Simple PetStore (Amazon CLI)", "id": "vaz7da96z6", "createdDate": 1494572809 }记下新创建的
id返回的RestApi。您需要使用它来设置 API 的其他部分。 -
调用
get-resources命令以检索RestApi的根资源标识符。aws apigateway get-resources --rest-api-id vaz7da96z6 --region us-west-2此命令的输出如下:
{ "items": [ { "path": "/", "id": "begaltmsm8" } ] }记下根资源
Id。您需要使用它开始设置 API 的资源树和配置方法与集成。 -
调用
create-resource命令以在根资源 (pets) 下附加子资源 (begaltmsm8):aws apigateway create-resource --rest-api-id vaz7da96z6 \ --region us-west-2 \ --parent-id begaltmsm8 \ --path-part pets此命令的输出如下:
{ "path": "/pets", "pathPart": "pets", "id": "6sxz2j", "parentId": "begaltmsm8" }要在根目录下附加子资源,您可以将根资源
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}'要使某个路径成为路径参数的一部分,请将其括在一对大括号内。如果成功,该命令会返回以下响应:
{ "path": "/pets/{petId}", "pathPart": "{petId}", "id": "rjkmth", "parentId": "6sxz2j" }现在,您创建了两个资源:
/pets(6sxz2j) 和/pets/{petId}(rjkmth),您可以继续为其设置方法。 -
调用
put-method命令为GET资源添加/petsHTTP 方法。这会创建一个具有开放访问权限的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此命令的成功输出如下:
{ "apiKeyRequired": false, "httpMethod": "GET", "authorizationType": "NONE" }此方法支持开放式访问,因为
authorization-type设置为NONE。要仅允许已验证身份的用户调用此方法,您可以使用 IAM 角色和策略、Lambda 授权方(以前称为自定义授权方)或者 Amazon Cognito 用户池。有关更多信息,请参阅 在 API Gateway 中控制和管理对 REST API 的访问。要启用
/pets/{petId}资源 (rjkmth) 的读取访问权限,请为该资源添加GETHTTP 方法,以按照如下所示创建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此命令的成功输出如下:
{ "apiKeyRequired": false, "httpMethod": "GET", "authorizationType": "NONE", "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此命令的输出如下:
{ "httpMethod": "GET", "passthroughBehavior": "WHEN_NO_MATCH", "cacheKeyParameters": [], "type": "HTTP", "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "cacheNamespace": "6sxz2j" }请注意,
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的一部分。如果此处未定义此映射,客户端尝试调用此方法时会收到错误响应。此命令的输出如下:
{ "passthroughBehavior": "WHEN_NO_MATCH", "cacheKeyParameters": [], "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}", "httpMethod": "GET", "cacheNamespace": "rjkmth", "type": "HTTP", "requestParameters": { "integration.request.path.id": "method.request.path.petId" } } -
调用
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此命令的输出如下:
{ "selectionPattern": "", "statusCode": "200" }同样,调用以下
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 的设置,这样让您的客户能够查询 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'
您可以在浏览器中键入 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 }