บันทึกวิธีหา resource เจ้าของ IP address ใน AWS VPC ด้วย AWS CLI และ jq

Nont Banditwong
1 min readFeb 13, 2024

--

สมมติเราพบ IP address ใน access log ของ application บน aws และอยากรู้ว่า IP address ใน log นั้นมาจาก resource ไหนบน AWS เราสามารถใช้คำสั่ง aws ec2 describe-network-interfaces ร่วมกับ jq เพื่อ query ข้อมูลของ resource นั้นออกมา โดยเป็นข้อมูลของ ENI ซึ่งจะพอทราบคร่าวๆแล้วว่ามาจาก resource ตัวไหน ยกตัวอย่างถ้าเราเห็น IP aaa.bbb.ccc.ddd ใน access log ให้ลองใช้คำสั่งด้านล่างเพื่อ query ดู

aws ec2 describe-network-interfaces | jq '.NetworkInterfaces.[] | select(.PrivateIpAddress == "aaa.bbb.ccc.ddd")'

ตัวอย่าง output

{
"Attachment": {
"AttachmentId": "ela-attach-0d03id00e",
"DeleteOnTermination": false,
"DeviceIndex": 1,
"InstanceOwnerId": "amazon-aws",
"Status": "attached"
},
"AvailabilityZone": "ap-southeast-1a",
"Description": "ELB net/my-nlb/d03kd0d0e",
"Groups": [],
"InterfaceType": "network_load_balancer",
"Ipv6Addresses": [],
"MacAddress": "01:1e:11:22:33:33",
"NetworkInterfaceId": "eni-d0eke0d0eo",
"OwnerId": "351736453330",
"PrivateDnsName": "ip-192-168-1-101.ap-southeast-1.compute.internal",
"PrivateIpAddress": "192.168.1.101",
"PrivateIpAddresses": [
{
"Primary": true,
"PrivateDnsName": "ip-192-168-1-101.ap-southeast-1.compute.internal",
"PrivateIpAddress": "192.168.1.101"
}
],
"RequesterId": "03030494940",
"RequesterManaged": true,
"SourceDestCheck": false,
"Status": "in-use",
"SubnetId": "subnet-0vff0rjjfff",
"TagSet": [],
"VpcId": "vpc-0r0e00f0f0f0r"
}

--

--

Nont Banditwong

Cloud Engineering Specialist, Software Developer, System Engineer, Photographer and Learner