使用 Amazon CLI 命令设置边缘优化的 API - Amazon API Gateway
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon CLI 命令设置边缘优化的 API

使用设置 API Amazon CLI 需要使用create-rest-apicreate-resourceget-resourcesput-methodput-method-responseput-integration、和put-integration-response命令。以下过程说明如何使用这些 Amazon CLI 命令来创建HTTP集成类型的简单 PetStore API。

要创建简单的 PetStore API,请使用 Amazon CLI
  1. 调用 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 的其他部分。

  2. 调用 get-resources 命令以检索 RestApi 的根资源标识符。

    aws apigateway get-resources --rest-api-id vaz7da96z6 --region us-west-2

    此命令的输出如下:

    { "items": [ { "id": "begaltmsm8", "path": "/" } ] }

    记下根资源 Id。您需要使用它开始设置 API 的资源树和配置方法与集成。

  3. 调用 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-idpets 资源 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),您可以继续为其设置方法。

  4. 调用 put-method 命令为 GET 资源添加 /pets HTTP 方法。这会创建一个具有开放访问权限的 Method 的 API GET /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 的 API GET /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 指定为必需的请求参数,以便将其动态设置值映射到相应的集成请求参数并传递到后端。

  5. 调用 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 方法与后端的集成。

  6. 调用 put-integration 命令以便为 Integration 方法设置与指定 HTTP 终端节点的 GET /pets/pets 资源使用其资源 Id 6sxz2j 进行标识:

    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": [] }
  7. 调用 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。

  8. 将 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 }