Skip to main content

Amazon ECR: レジストリのプル数を取得する

レジストリが使われているかどうかを確認するため、RepositoryPullCount メトリクスを参照して、ECR レジストリのプル数を取得します。

例) aws-ecr-pull-counts.py

def get_ecr_pull_count(repository_name: str, start_datetime: datetime, end_datetime: datetime) -> int:
cw_client = boto3.client("cloudwatch", config=Config(retries={"mode": "standard"}))
response = cw_client.get_metric_statistics(
Namespace="AWS/ECR",
MetricName="RepositoryPullCount",
Dimensions=[
{"Name": "RepositoryName", "Value": repository_name},
],
Statistics=["Sum"],
StartTime=start_datetime,
EndTime=end_datetime,
Period=3600, # hourly
)
total = 0
for datapoint in response["Datapoints"]:
total += datapoint["Sum"]
return int(total)