Get “Always Free” VM instance on Oracle Cloud, and Solve “Out of Host Capacity” Issue: The Easy Way — Part 2

Get “Always Free” VM instance on Oracle Cloud, and Solve “Out of Host Capacity” Issue: The Easy Way — Part 2

Following Part-1, Since these offerings are gaining popularity, it is becoming increasingly difficult to avail of some free resources like Arm-based Ampere A1 cores VMs, especially in popular regions. You’ll likely end up getting the “Out of Host Capacity” issue since they depend on availability.

I used the Frankfurt region. Comment if you know or find any other region where these resources are easily available.

Anyway, if the other methods mentioned in Part 1 are not feasible for you and you are not in a hurry, you can prepare this automation script following the steps below, and try until you find an available slot.

Steps

We’ll complete most of our tasks utilizing the Oracle resources and Oracle cloud shell. That is, we’ll run the final automation script on the cloud shell instead of running it on our local machine. Smart, eh? ;)

  • First, sign up for the Oracle Cloud free tier and log in to your Cloud account.
  • You can use either the root compartment or create a new one. I created a new one [(root)/Compartment-free-full] to separate Always Free Oracle Cloud services.
  • If you haven’t tried creating your first free VM instance, try it, even if you get the “Out of Host Capacity” error message. This action should, at least, create necessary networking instances (VNIC, VCN, subnet, IP addresses, etc.) which we will use later. Search [instances] > then [create instance]. You can find the specs for my VM here. Note that I have used the full free capacity in a single VM instance. Remember to save the key files. (Do not worry if you see Oracle is showing some estimated costs, you’ll not be charged as long as you use free resources.)
  • Try to create the instance. If you see the error, use [save as stack] to save the VM spec settings.
  • Go to [Stacks] > Open the saved stack > Create a [Plan].
  • When the Plan creation is successful, you see it gives you options to download [Terraform Configuration and Plan]. Download them.
  • Update this script using the specific values found in your [Terraform configuration and plan] files.

#!/bin/bash

# Define the availability domains ----------------- <UPDATE> ---------------
AD1="bgAk:EU-FRANKFURT-1-AD-1"
AD2="bgAk:EU-FRANKFURT-1-AD-2"
AD3="bgAk:EU-FRANKFURT-1-AD-3"

# Array to hold availability domains
AVAILABILITY_DOMAINS=("$AD1" "$AD2" "$AD3")

# Loop indefinitely until a command succeeds
while true; do
    for AVAILABILITY_DOMAIN in "${AVAILABILITY_DOMAINS[@]}"; do
        echo "Attempting to launch instance in Availability Domain: $AVAILABILITY_DOMAIN"
        
	# ------------------------ <UPDATE AS NECESSARY> --------------------------
        oci compute instance launch \
            --availability-domain "$AVAILABILITY_DOMAIN" \
            --compartment-id "ocid1.compartment.oc1..aaaaaaaary***EXAMPLE*****UPDATE_ID***vlsopz6lq" \
            --shape "VM.Standard.A1.Flex" \
            --subnet-id "ocid1.subnet.oc1.eu-frankfurt-1.aaaaaaaahigcy*****EXAMPLE******UPDATE_ID******73qbizvxzjzjq" \
            --assign-private-dns-record true \
            --assign-public-ip true \
            --agent-config '{"is_management_disabled": false, "is_monitoring_disabled": false, "plugins_config": [
                {"desired_state": "DISABLED", "name": "Vulnerability Scanning"},
                {"desired_state": "DISABLED", "name": "Management Agent"},
                {"desired_state": "ENABLED", "name": "Custom Logs Monitoring"},
                {"desired_state": "DISABLED", "name": "Compute RDMA GPU Monitoring"},
                {"desired_state": "ENABLED", "name": "Compute Instance Monitoring"},
                {"desired_state": "DISABLED", "name": "Compute HPC RDMA Auto-Configuration"},
                {"desired_state": "DISABLED", "name": "Compute HPC RDMA Authentication"},
                {"desired_state": "ENABLED", "name": "Cloud Guard Workload Protection"},
                {"desired_state": "DISABLED", "name": "Block Volume Management"},
                {"desired_state": "DISABLED", "name": "Bastion"}]}' \
            --availability-config '{"recovery_action": "RESTORE_INSTANCE"}' \
            --display-name "<PUT_YOUR_VM_NAME>" \
            --image-id "ocid1.image.oc1.eu-frankfurt-1.aaaaaaaausfft7********EXAMPLE*********UPDATE_ID*********qf5birrjk2zzcxv3sea" \
            --instance-options '{"are_legacy_imds_endpoints_disabled": false}' \
            --shape-config '{"memory_in_gbs": 24, "ocpus": 4}' \
            --ssh-authorized-keys-file ./vm/id_rsa.pub

        if [ $? -eq 0 ]; then
            echo "Instance created successfully in $AVAILABILITY_DOMAIN."
            exit 0  # Exit the script after successful execution
        else
            echo "Failed to launch instance in $AVAILABILITY_DOMAIN. Trying the next one..."
            sleep 3  # Wait 3 seconds before trying the next domain
        fi
    done
done        

  • Go back to the Oracle cloud account. Open the [cloud shell]. Use the [settings] icon to upload the script file, or create a script file in the shell using the [nano] command and then paste the script in it. After saving the script file in the shell, make the file executable using the [chmod +x] command. You can find the necessary commands here.

Article content

  • Use the [nano] command to create a public key file [vm/id_rsa.pub] and paste the public key into it that you downloaded earlier.
  • Voilà! Your automation script is ready to run. Run it. Now you’ll see that your script is trying to deploy the VM instance for you. Don’t worry - once it can deploy a VM instance successfully, it’ll stop.

Conclusion

This way you can automate your VM deployment retries. Your automation script will keep trying for you while you can do your other work.

Alternatively, you can modify and prepare multiple Terraform config [main.tf] files for all of your available domains, upload them in the cloud shell, and update this script accordingly. Then you can use this script similarly to run the automation. However, I recommend using the first approach.

To view or add a comment, sign in

More articles by Mehedi Ehteshum

Others also viewed

Explore content categories