AWS DataPipelineで利用しているAMIのImage IDを調べるために使ったスクリプトです。
import boto3
from time import sleep
session = boto3.Session(profile_name="{プロファイル名}")
client = session.client("datapipeline")
def main():
for x in list_all_pipelines():
for i in get_ec2_resources(x):
print(x, i)
def list_all_pipelines():
"""markerを指定しないと途中で終わってしまう
"""
result = client.list_pipelines()
pipelines = [x["id"] for x in result["pipelineIdList"]]
marker = result["marker"]
while marker is not None:
y = client.list_pipelines(marker=marker)
pipelines += [x["id"] for x in y["pipelineIdList"]]
marker = y.get("marker", None)
return pipelines
def get_ec2_resources(pipeline_id):
resources = []
result = client.get_pipeline_definition(pipelineId=pipeline_id)
for x in result["pipelineObjects"]:
if x["name"] == "Ec2":
for y in x["fields"]:
if y["key"] == "imageId":
resources.append(y["stringValue"])
return resources
if __name__ == "__main__":
main()