Created
November 16, 2022 09:09
-
-
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
This file contains hidden or 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
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