AWS CLI コマンドを使用してエッジ最適化 API をセットアップする
AWS CLI を使用して API をセットアップするには、create-rest-api
、create-resource
、get-resources
、put-method
、put-method-response
、put-integration
、および put-integration-response
コマンドを使用する必要があります。以下の手順では、これらの AWS CLI コマンドを使用して HTTP
統合タイプのシンプルな PetStore API を作成する方法を示します。
AWS CLI を使用してシンプルな PetStore API を作成するには
-
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 }
返された値である新しく作成した
id
のRestApi
をメモしておきます。API の他の部分でこれをセットアップする必要があります。 -
get-resources
コマンドを呼び出して、RestApi
のルートリソース ID を取得します。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}" }
2 つのリソース (
/pets
(6sxz2j
) と/pets/{petId}
(rjkmth
)) を作成したので、次はこれらのメソッドをセットアップします。 -
put-method
コマンドを呼び出し、GET
HTTP メソッドを/pets
リソースに追加します。これにより、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
メソッドの指定したエンドポイントで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
前のステップでは、顧客が PetStore ウェブサイトで購入可能なペットを検索して指定の識別子で各ペットを表示できるようにする簡単な API のセットアップを完了しました。顧客がこれを呼び出せるようにするには API をデプロイする必要があります。
-
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 }