AWS CLI コマンドを使用してエッジ最適化 API をセットアップする - Amazon API Gateway

AWS CLI コマンドを使用してエッジ最適化 API をセットアップする

AWS CLI を使用して API をセットアップするには、create-rest-apicreate-resourceget-resourcesput-methodput-method-responseput-integration、および put-integration-response コマンドを使用する必要があります。以下の手順では、これらの AWS CLI コマンドを使用して HTTP 統合タイプのシンプルな PetStore API を作成する方法を示します。

AWS CLI を使用してシンプルな PetStore API を作成するには
  1. create-rest-api コマンドを呼び出して特定のリージョン (RestApi) に us-west-2 をセットアップします。

    aws apigateway create-rest-api --name 'Simple PetStore (AWS CLI)' --region us-west-2

    このコマンドの出力は次のとおりです。

    { "id": "vaz7da96z6", "name": "Simple PetStore (AWS CLI)", "createdDate": "2022-12-15T08:07:04-08:00", "apiKeySource": "HEADER", "endpointConfiguration": { "types": [ "EDGE" ] }, "disableExecuteApiEndpoint": false }

    返された値である新しく作成した idRestApi をメモしておきます。API の他の部分でこれをセットアップする必要があります。

  2. get-resources コマンドを呼び出して、RestApi のルートリソース ID を取得します。

    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" }

    ルート下に子リソースを追加するには、ルートリソース IdparentId プロパティ値として指定します。同様に、pets リソースの下に子リソースを追加するには、parent-id の値を pets リソース id6sxz2j と置き換えて前のステップを繰り返します。

    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}" }

    2 つのリソース (/pets (6sxz2j) と /pets/{petId} (rjkmth)) を作成したので、次はこれらのメソッドをセットアップします。

  4. put-method コマンドを呼び出し、GET HTTP メソッドを /pets リソースに追加します。これにより、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-typeNONE に設定されているため、メソッドはオープンアクセスのものです。認証されたユーザーにのみ、メソッドの呼び出しを許可するには、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 値 /pets6sxz2j リソースを指定します。

    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 メソッドの指定したエンドポイントで 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

    ここでは、統合エンドポイント (urihttp://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

    前のステップでは、顧客が PetStore ウェブサイトで購入可能なペットを検索して指定の識別子で各ペットを表示できるようにする簡単な API のセットアップを完了しました。顧客がこれを呼び出せるようにするには API をデプロイする必要があります。

  8. stage を呼び出して、API を 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" }

この API をテストするには、https://vaz7da96z6.execute-api.us-west-2.amazonaws.com/test/pets URL をブラウザに入力し、vaz7da96z6 を 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 }