素人が自分のノートとして書いてますが、改善点あれば是非コメントください
例1:DBインスタンスのインスタンスIDとエンジンバージョンを取得・表示する
- 条件
- aws lambda
- python 3.8
- boto3
コード
import json
import boto3
def lambda_handler(event, context):
#rds クライアントを定義
rdsclient = boto3.client('rds')
#describe_db_instances で取得した結果(json)をresponseにいったん投入
response = rdsclient.describe_db_instances()
#結果確認のために表示
print("describe_db_instances=", response)
#response は json形式の配列になっているので、最初の要素:DBinstances の値を抽出し response1 に投入
#実際のDBインスタンスの情報が要素:DBinstancesの値(=response1)として入っている。
response1 = response['DBInstances']
#結果確認のために表示
print("DBinstances from describe_db_instances=", response1)
#response1 はDBインスタンス毎のリストになっているので要素数分for分で回す
#このfor文で記載している”each_instance”は何でもよい。
#response1 に含まれる要素数分回ると勝手に止まってくれる。
for each_instance in response1:
#各要素の['DBInstanceIdentifier']の値を抽出し、dbinstanceid に投入
dbinstanceid = each_instance['DBInstanceIdentifier']
#結果確認のために表示
print("DBinstanceid=", dbinstanceid)
#各要素の['EngineVersion']の値を抽出し、dbinstanceeversion に投入
dbinstanceversion = each_instance['EngineVersion']
#結果確認のために表示
print("DBversion=", dbinstanceversion)
- とにかく print() で取得できている情報を都度確認しつつ、抽出する情報を絞り込んでいくのが重要。