EC2 인스턴스 내에서 지역 찾기
인스턴스 내에서 인스턴스 영역을 찾는 방법이 있습니까?
인스턴스 id 를 찾는 방법과 비슷한 것을 찾고 있습니다.
해당 URL ( http://169.254.169.254/latest/dynamic/instance-identity/document )이 더 이상 작동하지 않는 것 같습니다. 사용하려고하면 404가 나타납니다. 그래도 작동하는 것으로 보이는 다음 코드가 있습니다.
EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed 's/[a-z]$//'`"
도움이 되었기를 바랍니다.
편집 :sed
의견을 기반으로 개선
이를 달성하는 또 다른 방법이 있습니다.
REGION=`curl http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}'`
echo $REGION
us-east-1
를 사용해도 괜찮다 jq
면 다음을 실행할 수 있습니다.
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq .region -r
가장 깨끗한 방법이라고 생각합니다.
ec2-metadata --availability-zone | sed 's/.$//'
ec2-metadata를 사용할 수 있습니다 :
ec2-metadata -z | grep -Po "(us|sa|eu|ap)-(north|south|central)?(east|west)?-[0-9]+"
정규 표현식을 피하려면 다음을 사용하여 파이썬으로 할 수있는 한 줄짜리입니다.
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | python -c "import json,sys; print json.loads(sys.stdin.read())['region']"
내가 찾은 가장 쉬운
curl -s 169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/.$//'
매우 간단한 하나의 라이너
export AVAILABILITY_ZONE=`wget -qO- http://instance-data/latest/meta-data/placement/availability-zone`
export REGION_ID=${AVAILABILITY_ZONE:0:${#AVAILABILITY_ZONE} - 1}
jq를 설치 했다면 다음 과 같이 할 수도 있습니다 (아마도 가장 "우아한"방법).
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -c -r .region
예쁘게 인쇄하거나 다른 형식을 지정하지 않고 "region"의 원시 값을 반환합니다. 참조 : AWS 포럼
가용 영역에서 지역을 가져 와서 마지막 문자를 제거하십시오.
ec2-metadata -z | awk '{print $2}' | sed 's/[a-z]$//'
JQ 사용 :
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region
AWS Java SDK를 사용할 수있는 경우 현재 지역 이름 (예 : "us-east-1", "eu-west-1")을 반환하는 방법이 있습니다.
이것이 내가 찾은 가장 깨끗한 솔루션입니다.
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document |sed -n 's/ "region" : "\(.*\)"/\1/p'
예 :
export REGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document |sed -n 's/ "region" : "\(.*\)"/\1/p')
- API 호출을하지 않고 EC2 인스턴스 메타 데이터를 사용합니다.
- curl 및 기본 sed 만 사용하므로 SDK 또는 도구에 종속되지 않습니다.
- Doesn't attempt to parse the Availability Zone name, so no worries if AWS changes AZ/Region name format
Thanks to https://unix.stackexchange.com/a/144330/135640, with bash 4.2+ we can just strip the last char from the availability zone:
$ region=`curl -s 169.254.169.254/latest/meta-data/placement/availability-zone`
$ region=${region::-1}
$ echo $region
us-east-1
This assumes AWS continues to use a single character for availability zones appended to the region.
Or don't make Ubuntu or this tool a requirement and simply do:
: "${EBS_VOLUME_AVAILABILITY_ZONE:=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)}"
: ${EBS_VOLUME_REGION:="${EBS_VOLUME_AVAILABILITY_ZONE%%*([![:digit:]])}"}
If you work with json - use right tools. jq much powerful in this case.
# curl -s curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region'
eu-west-1
2 liner that works as long as you are using ec2.internal as your search domain:
az=$(curl -s http://instance-data/latest/meta-data/placement/availability-zone)
region=${az:0:${#az} - 1}
For anyone wanting to do this with good ol powershell
$var = (curl http://169.254.169.254/latest/dynamic/instance-identity/document | Select-String-Pattern "Zone" | ConvertFrom-Json | Select-Object -ExpandProperty "region")
echo $var
This works for eu-central-1 as well as the various letter zones. (I don't have enough rep to reply to the sed answer above)
ec2-metadata --availability-zone | sed 's/[a-z]$//'
If you're running on windows, you can use this powershell one-liner:
$region=(Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").region
Was also looking for a solution to find region from the instance and here is my pure Bash solution:
az=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
region=${az:0:${#az}-1}
unless there are regions where AZ has more than two letters, which I'm not aware of.
For finding out information about the EC2 you are logged into, you can use the ec2-metadata tool.
You can install the tool by following this link. After installing the tool, you can run
# ec2-metadata -z
to find out the region.
This tools comes installed with the latest (10.10) Ubuntu AMIs,
If you are looking to get region using JS, this should work :
meta.request("/latest/meta-data/placement/availability-zone",function(err,data){
if(err)
console.log(err);
else{
console.log(data);
str = data.substring(0, data.length - 1);
AWS.config.update({region:str});
ec2 = new AWS.EC2();
}
});
This was the mapping found from AWS DOCS, in response to metadata API call, just trim the last character should work.
eu-west-1a :eu-west-1
eu-west-1b :eu-west-1
eu-west-1c :eu-west-1
us-east-1a :us-east-1
us-east-1b :us-east-1
us-east-1c :us-east-1
us-east-1d :us-east-1
ap-northeast-1a :ap-northeast-1
ap-northeast-1b :ap-northeast-1
us-west-1a :us-west-1
us-west-1b :us-west-1
us-west-1c :us-west-1
ap-southeast-1a :ap-southeast-1
ap-southeast-1b :ap-southeast-1
ec2metadata
(no dash) is the current command to provide you all the aws hosting info about your ec2 box. this is the most elegant and secure approach. (ec2-metadata
is the old, no longer valid command.)
참고URL : https://stackoverflow.com/questions/4249488/find-region-from-within-an-ec2-instance
'IT박스' 카테고리의 다른 글
Android 기기에서 수신 전화를 감지하는 방법은 무엇입니까? (0) | 2020.07.13 |
---|---|
프래그먼트와 활동 모두에 정의 된 경우 onRequestPermissionsResult가 프래그먼트에서 호출되지 않습니다 (0) | 2020.07.13 |
안드로이드 : Clear Activity Stack (0) | 2020.07.13 |
부모 디렉토리 위치를 얻는 방법 (0) | 2020.07.12 |
날짜 시간을 하루 씩 늘리는 방법은 무엇입니까? (0) | 2020.07.12 |