Easy way to resolve Oracle Cloud “Out of Capacity” error while creating Compute instance (using Terraform)
Oracle began offering Always Free services including Compute, Storage, and Autonomous Database. However the resources are limited in many regions and we often get “Out of Capacity” error while trying to create instances. Here is the most simple and easy solution to request for compute instance repeatedly and get one as soon as it is available. We will be using Terraform to provision compute instance and the request can be easily triggered from Oracle Cloud Shell.
Goto https://www.oracle.com/cloud/free/
Click “Start for free” to create an account (or click “Sign in to Oracle Cloud” if you have an account)
Once the account is ready, login to the dashboard
From the Menu click on Compute → Instances and the click on Create Instance
Under Image and shape section, click on Edit and chick on Change Shape to select the required instance. Please note that under ‘Always Free-eligible’ tier each tenancy gets the first 3,000 OCPU hours and 18,000 GB hours per month for free to create Ampere A1 Compute instances using the VM.Standard.A1.Flex shape (equivalent to 4 OCPUs and 24 GB of memory).
You can also change the image to Ubuntu as per need (optional)
In the Add SSH keys section, generate a key pair or upload a public key if you have one. (This is required to SSH into the instance)
Now click on ‘Save as stack’ (don’t click on Create)
Enter a name for your stack and click Next and then click Create. This will create a Terraform Configuration which can be downloaded
Click on ‘Download’ to get the configuration (zip) and extract the main.tf on to your desktop.
Click on the Cloud Shell icon (top right) to open Oracle Cloud Shell
In Cloud Shell, click on menu (3 lines) and select Upload to upload the main.tf file
After uploading the file, in Cloud Shell run the command ‘terraform init’ to initialize terraform (from the same directory where main.tf is present)
Next, in Cloud Shell create a script file (vi auto.sh) and add the below contents.
n=0
until [ “$n” -ge 10080 ]
do
terraform apply -auto-approve && break
n=$((n+1))
echo “Retry Attempt — $n”
sleep 60
done
This will run terraform apply in loop until it succeeds. The time delay here is 60 seconds and retry attempt is 10080 times (7 days), which can be customized based on your requirement.
Save the file and run the following command for background execution
nohup sh auto.sh &
This will start executing the script and the output is written to nohup.out file.
Note : The process will run even if Cloud Shell is closed and the execution stops as soon an instance is created or once the timeout has reached. The same process can also be used to create other types of instances.