0
1

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 5 years have passed since last update.

aws boto3 で describe した結果から欲しい情報だけ取得・表示する

0
Last updated at Posted at 2021-04-07

素人が自分のノートとして書いてますが、改善点あれば是非コメントください

例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() で取得できている情報を都度確認しつつ、抽出する情報を絞り込んでいくのが重要。
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?