Notion-ready version of your documentation, with the optimized region order (Frankfurt → London → Tokyo → Sydney → Mumbai → Singapore) and all the Gmail/WhatsApp notifications intact.


🚀 Oracle Cloud ARM Retry Script (Ampere A1 Free Tier)

Purpose

Automatically retry creation of a free Ampere A1 (ARM) instance across multiple regions until successful.

Useful when:


Region Priority (Optimized)

  1. Frankfurt (eu-frankfurt-1) → highest availability odds.
  2. London (uk-london-1) → solid fallback, low latency to Europe.
  3. Tokyo (ap-tokyo-1) → moderate fallback, decent chance.
  4. Sydney (ap-sydney-1) → reliable fallback, higher latency.
  5. Mumbai (ap-mumbai-1) → low odds, but closest to Sri Lanka.
  6. Singapore (ap-singapore-1) → low odds, popular, kept last.

Script: Retry + Progress Notifications

#!/bin/bash

# ==========================================
# OCI ARM Instance Retry Script (Optimized)
# ==========================================

# --------- CONFIGURATION ----------
OCI_PROFILE="DEFAULT"                # OCI CLI profile
COMPARTMENT_OCID="your_compartment_ocid_here"
SHAPE="VM.Standard.A1.Flex"
IMAGE_OCID="your_image_ocid_here"
INSTANCE_NAME="Freelance-A1"

# Region priority (availability first, latency second)
REGIONS=("eu-frankfurt-1" "uk-london-1" "ap-tokyo-1" "ap-sydney-1" "ap-mumbai-1" "ap-singapore-1")

# Retry & Progress settings
RETRY_INTERVAL=15       # minutes between retries
PROGRESS_INTERVAL=6     # hours between notifications

# Gmail setup
GMAIL_RECIPIENT="homemadeict@gmail.com"
SEND_GMAIL() {
    SUBJECT="$1"
    BODY="$2"
    echo "$BODY" | mail -s "$SUBJECT" "$GMAIL_RECIPIENT"
}

# WhatsApp via Twilio setup
TWILIO_ACCOUNT_SID="your_twilio_sid_here"
TWILIO_AUTH_TOKEN="your_twilio_auth_token_here"
WHATSAPP_TO="whatsapp:+94706572923"
WHATSAPP_FROM="whatsapp:+14155238886"
SEND_WHATSAPP() {
    MESSAGE="$1"
    curl -s -X POST "<https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json>" \\
        --data-urlencode "To=$WHATSAPP_TO" \\
        --data-urlencode "From=$WHATSAPP_FROM" \\
        --data-urlencode "Body=$MESSAGE" \\
        -u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN" > /dev/null
}

# --------- SCRIPT LOGIC ----------
LAST_PROGRESS=$(date +%s)

while true; do
    for REGION in "${REGIONS[@]}"; do
        echo "$(date): Attempting instance launch in $REGION..."

        CREATE_OUTPUT=$(oci --profile "$OCI_PROFILE" --region "$REGION" compute instance launch \\
            --compartment-id "$COMPARTMENT_OCID" \\
            --shape "$SHAPE" \\
            --display-name "${INSTANCE_NAME}-${REGION}" \\
            --image-id "$IMAGE_OCID" 2>&1)

        CURRENT_TIME=$(date +%s)

        # Progress notification every $PROGRESS_INTERVAL hours
        if (( CURRENT_TIME - LAST_PROGRESS >= PROGRESS_INTERVAL * 3600 )); then
            MESSAGE="OCI ARM retry progress: last attempt in $REGION at $(date)."
            SEND_GMAIL "OCI ARM Retry Script Progress" "$MESSAGE"
            SEND_WHATSAPP "$MESSAGE"
            LAST_PROGRESS=$CURRENT_TIME
        fi

        # Check results
        if echo "$CREATE_OUTPUT" | grep -qi "Out of capacity"; then
            echo "$(date): Out of capacity in $REGION. Moving on..."
            continue
        elif echo "$CREATE_OUTPUT" | grep -qi "Invalid"; then
            echo "$(date): ERROR - Config issue in $REGION."
            SEND_GMAIL "OCI ARM Retry Script ERROR" "Configuration error in $REGION"
            SEND_WHATSAPP "OCI ARM Retry Script ERROR: Config issue in $REGION"
            exit 1
        else
            echo "$(date): SUCCESS! Instance created in $REGION ✅"
            MESSAGE="🎉 OCI ARM instance created successfully in $REGION!"
            SEND_GMAIL "OCI ARM Instance Created" "$MESSAGE"
            SEND_WHATSAPP "$MESSAGE"
            exit 0
        fi
    done

    echo "$(date): All regions full. Retrying in $RETRY_INTERVAL minutes..."
    sleep $((RETRY_INTERVAL * 60))
done