Last active
April 19, 2023 10:04
-
-
Save johanforssell/d5fc85c37becafa2865fe77a63c4c895 to your computer and use it in GitHub Desktop.
Sorted list of resources with private IP in a Terraform state file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# This script goes through a terraform state file and looks for private IPs. | |
# It will present them as an IP-sorted list, like so | |
# Private IP ADDRESS | |
# ----------- ------- | |
# 10.16.0.4 aws_eip.zz-prd-nateip-az_a | |
# 10.16.0.4 aws_nat_gateway.zz-prd-natgw-az_a | |
# | |
# There will be duplicate private IPs, that's expected. | |
# Keywords for search: Terraform, jq, ip | |
terraform show -json > state.json | |
echo '' > modules_with_private_ip | |
cat state.json | jq -r '.values.root_module.resources[] | select(.values.private_ip !=null)' \ | |
>> modules_with_private_ip | |
cat state.json | jq -r '.values.root_module.child_modules[].resources[] | select(.values.private_ip !=null)' \ | |
>> modules_with_private_ip | |
rm state.json | |
cat modules_with_private_ip | jq '{ "address":.address, "private_ip":.values.private_ip }' \ | |
| jq -s 'sort_by(.private_ip | split(".") | map(tonumber))' \ | |
| jq -r '["Private IP ","ADDRESS"], ["-----------","-------"], (.[] | [.private_ip, .address]) | @tsv' | |
rm modules_with_private_ip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment