Skip to content

Instantly share code, notes, and snippets.

@shuson
Created November 16, 2022 09:09
Show Gist options
  • Save shuson/3db6497d11bf2d8488279391e90861ef to your computer and use it in GitHub Desktop.
Save shuson/3db6497d11bf2d8488279391e90861ef to your computer and use it in GitHub Desktop.
based on your ip and return AWS region string, get aws region by ip, find aws region
const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')
const ipRangeCheck = require("ip-range-check")
const getMyAWSRegion = () => {
const ip = execSync('curl https://api.ipify.org -s').toString()
const awsIPFile = path.join(process.env.TMP, "ip-ranges.json")
execSync(`curl -o ${awsIPFile} https://ip-ranges.amazonaws.com/ip-ranges.json`).toString()
const awsIPs = JSON.parse(fs.readFileSync(awsIPFile))['prefixes'] || []
const targets = awsIPs.filter(e => ipRangeCheck(ip, e['ip_prefix'])) || []
if (targets.length > 0) {
return targets[0]['region']
}
return 'ap-southeast-1'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment