AWS_REGION=‘us-west-2’ AWS_INSTANCE_OS=‘Linux’ AWS_INSTANCE_TYPE=‘g4dn.8xlarge’

SPOT_DATA=$(curl –connect-timeout 10 -sf https://spot-bid-advisor.s3.amazonaws.com/spot-advisor-data.json) AWS_SPOT_INTERRUPTION_ID=$(echo $SPOT_DATA | jq –arg reg “$AWS_REGION” –arg os “$AWS_INSTANCE_OS” –arg type “$AWS_INSTANCE_TYPE” ‘.spot_advisor[$reg][$os][$type][“r”]’)

AWS_SPOT_INTERRUPTION_ID=$(echo “$SPOT_DATA” |
awk -v region="$AWS_REGION" -v os="$AWS_INSTANCE_OS" -v type="$AWS_INSTANCE_TYPE" ' $0 ~ “"” region “":” {region_match=1} region_match && $0 ~ “"” os “":” {os_match=1} os_match && $0 ~ “"” type “":” {type_match=1} type_match && $0 ~ “"r":” { match($0, /“r”: [0-9]+/); # Find the “r”: pattern r_value = substr($0, RSTART+5, RLENGTH-5); # Extract the number after “r”: print r_value; exit; }')

echo AWS_SPOT_INTERRUPTION=$(echo $SPOT_DATA | jq –argjson id “$AWS_SPOT_INTERRUPTION_ID” ‘.ranges[] | select(.index==$id) | .label’)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
AWS_REGION='us-west-2'
AWS_INSTANCE_OS='Linux'
AWS_INSTANCE_TYPE='g4dn.8xlarge'
SPOT_DATA=$(curl --connect-timeout 10 -sf https://spot-bid-advisor.s3.amazonaws.com/spot-advisor-data.json)

export SPOT_DATA AWS_REGION AWS_INSTANCE_OS AWS_INSTANCE_TYPE;
AWS_SPOT_INTERRUPTION=$(python -c '\
import os, requests, json; d=json.loads(os.environ["SPOT_DATA"]); \
id=d.get("spot_advisor", {}).get(os.environ["AWS_REGION"], {}).get(os.environ["AWS_INSTANCE_OS"], {}).get(os.environ["AWS_INSTANCE_TYPE"], {}).get("r", None); \
p=next((item["label"] for item in d.get("ranges", []) if item["index"]==id), None); \
print(p)' 2> /dev/null)
echo $AWS_SPOT_INTERRUPTION     
1
2
3
4
5
6
7
8
9
AWS_REGION='us-west-2'
AWS_INSTANCE_OS='Linux'
AWS_INSTANCE_TYPE='g4dn.8xlarge'
SPOT_DATA=$(curl --connect-timeout 10 -sf https://spot-bid-advisor.s3.amazonaws.com/spot-advisor-data.json)

AWS_SPOT_INTERRUPTION=$(python -c '\
import sys, os, requests, json; d=json.loads(sys.argv[1]); id=d.get("spot_advisor", {}).get(sys.argv[2], {}).get(sys.argv[3], {}).get(sys.argv[4], {}).get("r", None); \
p=next((item["label"] for item in d.get("ranges", []) if item["index"]==id), None); print(p)' $SPOT_DATA $AWS_REGION $AWS_INSTANCE_OS $AWS_INSTANCE_TYPE 2> /dev/null)
echo $AWS_SPOT_INTERRUPTION     
1
2
3
4
5
6
7
8
AWS_REGION='us-west-2'
AWS_INSTANCE_OS='Linux'
AWS_INSTANCE_TYPE='g4dn.8xlarge'
SPOT_DATA=$(curl --connect-timeout 10 -sf https://spot-bid-advisor.s3.amazonaws.com/spot-advisor-data.json)

AWS_SPOT_INTERRUPTION=$(python -c '\
import sys, os, requests, json; d=sys.argv[2]; print(d)' SPOT_DATA AWS_REGION AWS_INSTANCE_OS AWS_INSTANCE_TYPE)
echo $AWS_SPOT_INTERRUPTION     
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Spot instances interruption percentage
AWS_REGION='us-west-2'
AWS_INSTANCE_OS='Linux'
AWS_INSTANCE_TYPE='g4dn.8xlarge'
SPOT_DATA=$(curl --connect-timeout 10 -sf https://spot-bid-advisor.s3.amazonaws.com/spot-advisor-data.json)

# jq is not installed
AWS_SPOT_INTERRUPTION_ID=$(echo $SPOT_DATA | jq --arg reg "$AWS_REGION" --arg os "$AWS_INSTANCE_OS" --arg type "$AWS_INSTANCE_TYPE" '.spot_advisor[$reg][$os][$type]["r"]' 2> /dev/null)
AWS_SPOT_INTERRUPTION=$(echo $SPOT_DATA | jq --argjson id "$AWS_SPOT_INTERRUPTION_ID" '.ranges[] | select(.index==$id) | .label' 2> /dev/null)
        
echo $AWS_SPOT_INTERRUPTION