iTranslated by AI
Find Public IPs Previously Assigned to an ALB Using Athena
1. Introduction
You may sometimes want to know the public IP addresses that were previously assigned to an ALB for audit or security investigations.
ALB's public IPs change due to scaling or maintenance, but ALB access logs and connection logs do not include public IP information.
However, I found a relatively simple way to investigate the public IPs previously assigned to an ALB and analyze them with Athena, so I will share that method.
2. ALB Logs
ALB has three types of logs: access logs, connection logs, and health check logs.
For details on what each log contains, please see the following:
- Application Load Balancer Access Logs
- Application Load Balancer Connection Logs
- Application Load Balancer Health Check Logs
The access logs have columns such as "client:port" and "target:port", which refer to the IP address and port of the client that sent the request and the IP address and port of the target (e.g., EC2) that processed the request, respectively.
Similarly, the client_ip in the connection logs is the IP address of the client that sent the request.
Therefore, the logs themselves do not include the public IP of the node that processed the request.
However, the public IP is included in the log file name.
The format of the access log file name is as follows:
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
In this format, "ip_address" corresponds to the public IP of the node that processed the request.
3. Athena's $path Metadata Column
Athena provides a metadata column called $path that allows you to retrieve the file name for each row.
The Trino documentation on which Athena is based states the following:
$path: Full file system path name of the file for this row
By using $path, you can query the public IP of the node that processed the ALB request on a per-request basis.
4. Querying Public IPs with Athena
First, enable ALB access logs and create an Athena table for the access logs.
When resolving the DNS name of the test ALB, two public IPs are currently assigned: 54.249.49.147 and 54.95.54.112.
$ nslookup accesslogTest-alb-468524003.ap-northeast-1.elb.amazonaws.com
# (abbreviated)
Name: accesslogTest-alb-468524003.ap-northeast-1.elb.amazonaws.com
Addresses: 54.249.49.147
54.95.54.112
After accessing the ALB several times from a browser, log files were created in S3. The ip-address is one of the resolved IPs: 54.249.49.147.

Also, query the access logs from Athena.
Using "$path", you can retrieve the file name for each row, and by processing it appropriately, you can obtain the public IP.
In the following example, to handle cases where the S3 prefix contains underscores, we split by underscore and take the second-to-last element to get the public IP.
SELECT element_at(split("$path", '_'), -2) public_ip, *
FROM "default"."alb_access_logs"

Also, by querying as shown below, you can find out when the public IP assigned to the ALB changed. In the image below, you can see that on March 20, one of the node's public IPs changed from 57.180.110.16 to 54.249.49.147.
However, since this is based on access logs, it assumes that requests actually reached each node during the relevant period (IPs of nodes that received no access cannot be retrieved).
SELECT DISTINCT day, element_at(split("$path", '_'), -2) public_ip
FROM "default"."alb_access_logs"
ORDER BY day, element_at(split("$path", '_'), -2)

5. Summary
The public IPs previously assigned to an ALB are not included in the log content, but they are included in the log file names.
Also, by using Athena's $path metadata column, you can investigate the public IPs that were previously assigned to an ALB!
If you ever need to know the public IPs previously assigned to an ALB, I hope this helps.
Discussion