{APIGateway}API キャッシュを有効にして応答性を強化する

 

https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/api-gateway-caching.html

https://qiita.com/leomaro7/items/c13c5cee168ed56ccc38

API キャッシュを有効にすると追加料金が発生します。
キャッシュの作成または削除は、API Gateway が完了するまで約 4 分かかります。

 


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

aws --version

-- 1.2 jqインストール
sudo yum -y install jq

 

-- 2. API を作成する

aws apigateway create-rest-api \
--name api01 \
--description "api01" \
--endpoint-configuration '{"types": ["REGIONAL"]}'

aws apigateway get-rest-apis

aws apigateway get-rest-api \
--rest-api-id 1111111111

 

-- 3. API でリソースを作成する
aws apigateway get-resources \
--rest-api-id 1111111111

aws apigateway create-resource \
--rest-api-id 1111111111 \
--parent-id 2222222222 \
--path-part "{proxy+}"


-- 4. リソースにメソッドを作成する


aws apigateway put-method \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method ANY \
--authorization-type NONE \
--no-api-key-required \
--request-parameters '{
        "method.request.path.proxy": true
    }'


aws apigateway put-integration \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method ANY \
--type HTTP_PROXY \
--uri "http://petstore-demo-endpoint.execute-api.com/{proxy}" \
--connection-type "INTERNET" \
--request-parameters '{
        "integration.request.path.proxy": "method.request.path.proxy"
    }' \
--passthrough-behavior "WHEN_NO_MATCH" \
--cache-namespace "333333" \
--cache-key-parameters  "method.request.path.proxy" \
--timeout-in-millis 29000 \
--integration-http-method ANY

 


aws apigateway put-integration-response \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method ANY \
--status-code 200 \
--response-templates '{"application/json": ""}'

 

aws apigateway get-method \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method ANY

 


-- 5. デプロイ前にAPI をテストする

aws apigateway test-invoke-method \
--rest-api-id 1111111111 \
--resource-id 333333 \
--http-method GET \
--path-with-query-string 'petstore/pets?type=fish'

 

 

-- 6. API をデプロイする


aws apigateway get-deployments \
--rest-api-id 1111111111

aws apigateway get-stages \
--rest-api-id 1111111111


aws apigateway create-deployment \
--rest-api-id 1111111111

aws apigateway create-stage \
--rest-api-id 1111111111 \
--stage-name stage01 \
--deployment-id 444444


-- 7. 動作確認(APIキャッシュ有効化前)


time curl -v -X GET https://1111111111.execute-api.ap-northeast-1.amazonaws.com/stage01/petstore/pets?type=fish

real    0m0.335s
user    0m0.011s
sys     0m0.003s


-- 8. APIキャッシュ有効化

aws apigateway get-stages \
--rest-api-id 1111111111


GUIから実施。
CLIで実行したが、GUIから実施した場合と同じ設定を再現できなかった。


-- 9. 動作確認(APIキャッシュ有効化後)


time curl -v -X GET https://1111111111.execute-api.ap-northeast-1.amazonaws.com/stage01/petstore/pets?type=fish

real    0m0.068s
user    0m0.012s
sys     0m0.003s

 

-- 10. クリーンアップ


-- API削除

aws apigateway get-rest-apis

aws apigateway delete-rest-api \
--rest-api-id 1111111111