はじめに
AWS API Gateway の設計書を作ることになり、画面見ながら設定値をコピペしていたら意識が無になって挫折したので、lambdaで取得したjsonを設計書ということにしました
公式資料ではどれがどの設定かわかりづらいし、qiitaにも無いっぽいので自分でので1つ1つ調べました
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/apigateway.html#client
※APIGatewayV2に気付くのが遅れたのでAPIGatewayで作ってます
こちらの続きです
AWS S3の設定情報をlambda(python + boto3)で取得した時のメモ
https://qiita.com/lunar_sword3/items/5df4122dd797bb23da79
本文
設定情報取得に利用したメソッドリスト
引数は全てバケット名になります
import boto3
client = boto3.client('apigateway', region)
メソッド名 | 説明 |
---|---|
get_rest_apis() | APIのリストを取得 |
get_resources(restApiId=restApiId) | リソースの情報を取得 restApiIdはget_rest_apisで |
client.get_method( restApiId=restApiId, resourceId=resourceId, httpMethod=httpMethod) |
メソッドの情報を取得 restApiId、resourceId、httpMethodはget_resourcesで |
get_stages(restApiId=restApiId) | ステージの情報を取得 restApiIdはget_rest_apisで |
コード
ココ
# API Gatewayの設計書用の設定情報取得lambda
# テストで実行した結果が取得情報になる
import json
import boto3
from datetime import date, datetime, timedelta
# 設定
# リージョン
region_list = [
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
"ca-central-1",
"eu-central-1",
"eu-west-1",
"eu-west-2",
"eu-west-3",
"ap-northeast-1",
"ap-northeast-2",
# "ap-northeast-3", #ないっぽい
"ap-southeast-1",
"ap-southeast-2",
"ap-south-1",
"sa-east-1"
]
def lambda_handler(event, context):
region_lambda_list = []
#リージョンごとに取得
for region in region_list:
client = boto3.client('apigateway', region)
# API Gatewayのリストを取得
responce = {}
responce = client.get_rest_apis()
restApi_items = responce['items']
for idx, restApi in enumerate(restApi_items):
res_resources = {}
res_stages = {}
# api id
restApiId = restApi['id']
# リソースの情報を取得
res_resources = client.get_resources(restApiId=restApiId)
res_resources_items = res_resources['items']
# メソッドの情報を取得
for idxr, res_resources_item in enumerate(res_resources_items):
resourceId = res_resources_item['id']
# methodがあれば情報取得
if ('resourceMethods' in res_resources_item):
for httpMethod in res_resources_item['resourceMethods'].keys():
res_method = client.get_method(
restApiId=restApiId,
resourceId=resourceId,
httpMethod=httpMethod
)
# メソッドごとに情報をまとめる
res_resources['items'][idxr]['resourceMethods'][httpMethod]['response_method'] = res_method
# リソースごとに情報をまとめる
responce['items'][idx]['resources'] = res_resources
# ステージの情報を取得
res_stages = client.get_stages(restApiId=restApiId)
for idxs,stageItem in enumerate(res_stages['item']):
res_stage = client.get_stage(
restApiId=restApiId,
stageName=stageItem['stageName']
)
res_stages['item'][idxs]['stage_info']=res_stage
# ステージごとに情報をまとめる
responce['items'][idx]['stages'] = res_stages
region_lambda_list.append({region: responce})
return json.dumps(region_lambda_list, default=json_serial)
def json_serial(obj):
if isinstance(obj, (datetime, date)):
# 時差の修正9時間
obj = obj - timedelta(hours=-9)
return obj.strftime('%Y/%m/%d %H:%M')
raise TypeError("Type %s not serializable" % type(obj))
おわりに
これで設計書の最新化がとても楽になりました
あと同じAPI Gateway作るのもこの値とboto3のメソッド使って作れるはず・・・
残りのできたらやりたい予定
cloudfrontの設定情報をlambda(python + boto3)で取得した時のメモ
lambda の設定情報をlambda(python + boto3)で取得した時のメモ