素人が自分のノートとして書いてますが、改善点あれば是非コメントください
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)