LoginSignup
0
1

More than 1 year has passed since last update.

EC2 を台数分リストする

Last updated at Posted at 2021-05-18

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

EC2 を台数分リストします。

aws-lambda-python38
import boto3
import json

def lambda_handler(event, context):

    #EC2クライアント定義
    ec2_client = boto3.client('ec2')

    #EC2インスタンスのリストを取得するため、describe。
    response = ec2_client.describe_instances()

    #インスタンス台数分 for 文で回す
    for reservation in response["Reservations"]:
        for instance in reservation['Instances']:
            instanceid = instance['InstanceId']
            #表示
            print("instanceid=", instnaceid)

たとえばAvailabilityZoneの情報を追加したい場合は、以下のような感じで追加。
['Placement']が増えているのは、describe した JSON の結果として、{Placement}の配下にあるから。
追加したい情報ごとに、都度都度 describe の JSON 結果を見ながら調整してくださいませ。

aws-lambda-python38
            availabilityzone = instance['Placement']['AvailabilityZone']
            print("AvailabilityZone=", availabilityzone)
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