ELB(ALB)のIPアドレス変更歴を辿る

動的に変わるELB(ALB)に割り当てられたIPアドレスの履歴を確認する方法です。
アクセスログが取得されていることを前提とします。

ALBのアクセスログ

docs.aws.amazon.com

S3に保存されたアクセスログのファイル名は以下のフォーマットに従います。
load-balancer-id 及び ip-address の箇所を参照することで、ログ保存時点に使用していたIPアドレスを特定することができます。

bucket[/prefix]/AWSLogs/aws-account-id/elasticloadbalancing/region/yyyy/mm/dd/aws-account-id_elasticloadbalancing_region_app.load-balancer-id_end-time_ip-address_random-string.log.gz
  • load-balancer-id: The resource ID of the load balancer. If the resource ID contains any forward slashes (/), they are replaced with periods (.).

  • ip-address: The IP address of the load balancer node that handled the request. For an internal load balancer, this is a private IP address.

IPアドレス一覧を取得するコマンド

S3のコンソールから確認するのは辛いので、CLIで取得したバケット一覧からIPアドレスを抽出します。

ls — AWS CLI 1.22.86 Command Reference

ALB my-load-balancer が2022年に使用したIPアドレスの一覧を取得する例です。
ALBの名前を LB_NAME に、ログを格納したバケットURIS3_URI に指定してください。
第一列には当該IPが初めて使用された時のログ作成時刻を出力しました。

$ LB_NAME=my-load-balancer
$ S3_URI=s3://my-bucket/AWSLogs/123456789012/elasticloadbalancing/ap-northeast-1/2022/
$ aws s3 ls $S3_URI --recursive --summarize |
> grep $LB_NAME |
> awk '{print $1"_"$2"_"$4}' |
> awk -F'_' '{print $1,$2,$8}' |
> awk '!arr[$3]++ {print $1,$2,$3}'

2022-01-01 09:00:02 aaa.aaa.aaa.aaa
2022-01-01 09:00:03 bbb.bbb.bbb.bbb
2022-01-01 09:00:11 ccc.ccc.ccc.ccc
2022-02-02 19:25:06 ddd.ddd.ddd.ddd
2022-02-02 19:35:08 eee.eee.eee.eee
2022-02-02 19:50:03 fff.fff.fff.fff
2022-02-04 10:45:04 ggg.ggg.ggg.ggg
2022-02-04 10:50:04 hhh.hhh.hhh.hhh
2022-02-04 11:10:10 iii.iii.iii.iii