Skip to content

Instantly share code, notes, and snippets.

@miyataka
Created June 14, 2021 06:45
Show Gist options
  • Save miyataka/2b4bcecc77984df61c6dbae43cffe7df to your computer and use it in GitHub Desktop.
Save miyataka/2b4bcecc77984df61c6dbae43cffe7df to your computer and use it in GitHub Desktop.
one liner: generate prefix list array from aws ip range json
# https://docs.aws.amazon.com/ja_jp/general/latest/gr/aws-ip-ranges.html#aws-ip-egress-control
# requirements
# - curl
# - jq
require 'json'
require 'yaml'
REGION='ap-northeast-1'
iprange_strings = `curl https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '[.prefixes[] | select(.service=="AMAZON" and .region=="#{REGION}").ip_prefix] - [.prefixes[] | select(.service=="EC2").ip_prefix] | .[]'`
ipranges = iprange_strings.split("\n")
result = ipranges.map do |cidr|
puts cidr
{ "Cidr" => cidr }
end
puts "json version"
puts result.to_json
puts "yaml version"
puts result.to_yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment