Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

aws cli で API Gateway の API を作成する

Last updated at Posted at 2017-10-16

次のページの手順をなぞってみました。
AWS CLI コマンドを使用して API を作成する

異なるのは、リージョンと、テストに curl を使う点です。

次のリソースにアクセスするAPI を作成します。

http://petstore-demo-endpoint.execute-api.com/petstore/pets

  1. RestApi を作成。出力にrest-api-id が表示される
create_api.sh
aws apigateway create-rest-api --name 'Sample AAA' \
	--region ap-northeast-1 > out01.txt
  1. RestApi のルートリソース ID を取得
get_resources.sh
#
TMP_ID=$(jq .id out01.txt)
echo $TMP_ID
REST_API_ID=${TMP_ID//\"/}
echo $REST_API_ID
#
aws apigateway get-resources --rest-api-id $REST_API_ID \
	--region ap-northeast-1 > out02.txt
#
  1. GET / の Method リクエストを作成
put_method.sh
#
#	put_method.sh
#
TMP_ID=$(jq .id out01.txt)
echo $TMP_ID
REST_API_ID=${TMP_ID//\"/}
echo $REST_API_ID
#
TMP_ID=$(jq .items[0].id out02.txt)
echo $TMP_ID
RESOURCE_ID=${TMP_ID//\"/}
echo $RESOURCE_ID
#
aws apigateway put-method \
	--rest-api-id $REST_API_ID \
	--resource-id $RESOURCE_ID \
	--http-method GET \
	--authorization-type "NONE" \
	--region ap-northeast-1
#
  1. GET / メソッドの MethodResponse を作成
put_method_response.sh
#
#	put_method_response.sh
#
TMP_ID=$(jq .id out01.txt)
echo $TMP_ID
REST_API_ID=${TMP_ID//\"/}
echo $REST_API_ID
#
TMP_ID=$(jq .items[0].id out02.txt)
echo $TMP_ID
RESOURCE_ID=${TMP_ID//\"/}
echo $RESOURCE_ID
#
aws apigateway put-method-response \
	--rest-api-id $REST_API_ID \
	--resource-id $RESOURCE_ID \
	--http-method GET \
	--status-code 200 \
	--region ap-northeast-1
#
#
  1. Integration を作成
put_integration.sh
#
#	put_integration.sh
#
TMP_ID=$(jq .id out01.txt)
echo $TMP_ID
REST_API_ID=${TMP_ID//\"/}
echo $REST_API_ID
#
TMP_ID=$(jq .items[0].id out02.txt)
echo $TMP_ID
RESOURCE_ID=${TMP_ID//\"/}
echo $RESOURCE_ID
#
aws apigateway put-integration \
	--rest-api-id $REST_API_ID \
	--resource-id $RESOURCE_ID \
	--http-method GET \
	--type HTTP --integration-http-method GET \
	--uri 'http://petstore-demo-endpoint.execute-api.com/petstore/pets' \
	--region ap-northeast-1
#

ここの http://petstore-demo-endpoint.execute-api.com/petstore/pets
を変更すれば、出力が変わります。

  1. IntegrationResponse を作成
put_integration_response.sh
#
#	put_integration_response.sh
#
TMP_ID=$(jq .id out01.txt)
echo $TMP_ID
REST_API_ID=${TMP_ID//\"/}
echo $REST_API_ID
#
TMP_ID=$(jq .items[0].id out02.txt)
echo $TMP_ID
RESOURCE_ID=${TMP_ID//\"/}
echo $RESOURCE_ID
#
aws apigateway put-integration-response \
	--rest-api-id $REST_API_ID \
	--resource-id $RESOURCE_ID \
	--http-method GET \
	--status-code 200 --selection-pattern "" \
	--region ap-northeast-1
#
  1. デプロイ
create_deploy.sh
#
#	cretate_deploy.sh
#
TMP_ID=$(jq .id out01.txt)
echo $TMP_ID
REST_API_ID=${TMP_ID//\"/}
echo $REST_API_ID
#
#
aws apigateway create-deployment \
	--rest-api-id $REST_API_ID \
	--stage-name test \
	--stage-description 'Test stage' \
	--description 'First deployment'
#
  1. テスト
curl https://1awvlqh2bb.execute-api.ap-northeast-1.amazonaws.com/test

テスト結果

[
  {
    "id": 1,
    "type": "dog",
    "price": 249.99
  },
  {
    "id": 2,
    "type": "cat",
    "price": 124.99
  },
  {
    "id": 3,
    "type": "fish",
    "price": 0.99
  }
]

これは、次で得られる結果と同じです。

curl http://petstore-demo-endpoint.execute-api.com/petstore/pets



9) 作成された API の一覧

```bash
aws apigateway get-rest-apis
  1. 作成した API の削除
delete_api.sh
TMP_ID=$(jq .id out01.txt)
echo $TMP_ID
REST_API_ID=${TMP_ID//\"/}
echo $REST_API_ID
#
aws apigateway delete-rest-api --rest-api-id $REST_API_ID

次のバージョンで確認しました。

$ aws --version
aws-cli/2.2.41 Python/3.8.8 Linux/5.14.7-arch1-1 exe/x86_64.arch prompt/off
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?