Skip to content

Instantly share code, notes, and snippets.

@palmerj
Last active July 25, 2022 15:45
Show Gist options
  • Save palmerj/e293b443def642431f05775aa4d7f640 to your computer and use it in GitHub Desktop.
Save palmerj/e293b443def642431f05775aa4d7f640 to your computer and use it in GitHub Desktop.
One liner to get AWS S3 IP address ranges for ap-southeast-2 (Sydney)
curl https://ip-ranges.amazonaws.com/ip-ranges.json | jq '.prefixes' | jq 'map(select(.region=="ap-southeast-2"))' | jq 'map(select(.service=="S3"))' | jq 'map(.ip_prefix)'
@brettcave
Copy link

brettcave commented Oct 28, 2021

Suggestion here @palmerj - piping to jq invokes jq multiple times within the shell, you can actually pipe to jq within a single jq process. In addition to that, you can combine multiple pipes with an expanded conditional statement.

curl https://ip-ranges.amazonaws.com/ip-ranges.json | jq '.prefixes | map(select(.region=="ap-southeast-2" and .service=="S3")) | map(.ip_prefix)  | .[]'

The final .[] will return the list directly.

When using with time cat local-ip-ranges.json on a fetched file, with multiple | jq 'expr' format:

real    0m0.054s
user    0m0.119s
sys     0m0.016s

when piping within a single JQ process with pipes:

real    0m0.040s
user    0m0.032s
sys     0m0.010s

p.s. thanks for the gist, i was battling to the get the extractor working, helped a lot.

@palmerj
Copy link
Author

palmerj commented Oct 29, 2021

Thanks, good optimisation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment