春江暮客

春江暮客的个人学习分享网站

Grabbing Free Oracle Cloud Servers and Automating Deployment with Scripts

2019-09-19 Miscellaneous
Grabbing Free Oracle Cloud Servers and Automating Deployment with Scripts

Oracle Cloud free-tier instances are attractive, but the real problem is often not registration. The frustrating part is that popular regions frequently return Out of host capacity when you try to create a VM.

The practical workaround is to complete one manual setup, record the key parameters, then switch to oci-cli or a simple retry script so instance creation can be automated instead of retried by hand.

A few days ago, Oracle Cloud opened free account registration allowing lifetime usage of:

  • 2 compute VMs (1 OCPU, 1GB RAM each)
  • 2 autonomous databases
  • Storage, load balancer, and more services

Of course, I registered immediately and chose Korea as the main region for faster access from Shanghai.

Since the Korean region quickly ran out of capacity (Out of host capacity), I explored ways to automate server creation using APIs and oci-cli. This post documents how to use OCI CLI to script the automatic creation of servers.

Environment: I used CentOS. Ubuntu users may need to adapt paths or commands accordingly.

Before you start

  1. Make sure your Oracle Cloud account has already passed review
  2. Create at least one instance manually first so you can inspect and record the required parameters
  3. Prepare your own SSH public key instead of reusing sample values

Requirements

  1. Mobile phone (for verification)
  2. Credit card

Initially, one credit card could register multiple accounts, but that was quickly abused. Now it’s one card per account, and many Chinese cards are restricted.

Free Resources Received

  • 2 Autonomous Databases (each with 1 OCPU, 20 GB storage)
  • 2 Compute VMs (1/8 OCPU, 1 GB RAM)
  • 2 Block Volumes (100 GB total, 5 free backups)
  • 10 GB Object Storage and Archive Storage
  • 1 Load Balancer (10 Mbps)
  • 10 TB/month outbound data
  • 500 million ingest datapoints and 1 billion monitoring datapoints
  • 1 million notifications and 1,000 emails per month

Main Steps

  1. Register Oracle Cloud account
  2. Create the first server and record required info
  3. Install oci-cli
  4. Configure CLI and add API keys
  5. Automate server creation with CLI
  6. Automate ARM server creation using Python

When this workflow is most useful

This approach is especially useful if:

  1. Popular free-tier regions keep running out of capacity
  2. You want a repeatable CLI-based provisioning flow instead of clicking through the console every time
  3. You also want to try ARM instance creation automatically

1. Register Oracle Cloud Account

Go to Oracle Cloud Free Tier and follow the steps:

  • Email
  • Address
  • Phone number (for verification)
  • Credit card (charged for verification, refunded later)

Use a Gmail account if possible. A U.S. address worked for me.


2. Create the First Server and Record Info

Use Chrome DevTools to inspect the instances network request during server creation. Record the following:

  • availabilityDomain
  • compartmentId
  • subnetId
  • shape (e.g., VM.Standard.E2.1.Micro)
  • ssh_authorized_keys
  • imageId

Also get:

  • User OCID
  • Tenancy OCID

3. Install oci-cli

Run:

bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"

Verify installation:

oci -v

4. Configure CLI and Add API Key

Run:

oci setup config

Upload the generated public key to your user profile in Oracle Cloud Console.

Test with:

oci iam availability-domain list

If no error, configuration succeeded.

How to verify the CLI environment is actually ready

At minimum, validate these two commands:

oci -v
oci iam availability-domain list

If the version prints normally and the availability-domain query works without authentication errors, your local CLI, config file, and uploaded API key are usually in good shape.


5. Automate Server Creation Using CLI

Use the following command, replacing placeholders with your values:

oci compute instance launch 
--availability-domain JCbl:AP-SEOUL-1-AD-1 
--display-name seoul1 
--image-id ocid1.image.oc1.ap-seoul-1.aaaa... 
--subnet-id ocid1.subnet.oc1.ap-seoul-1.aaaa... 
--shape VM.Standard.E2.1.Micro 
--assign-public-ip true 
--metadata '{"ssh_authorized_keys": "ssh-rsa AAAAB3..."}' 
--compartment-id ocid1.tenancy.oc1...

You can write this into a script oci.sh and set up a cron job:

# /root/oci.sh
oci compute instance launch --availability-domain ... # same as above

Cron job:

crontab -e
* * * * * /bin/bash /root/oci.sh >> /root/oracle.log 2>&1

Optional for debugging:

tail -F /root/oracle.log

If there’s no available capacity, you’ll see:

"message": "Out of host capacity.",
"status": 500

Ignore and let it keep trying.

Common issues

1. Out of host capacity keeps appearing

That often means the script is fine and the region simply has no free capacity right now. First check:

  1. Whether the selected shape is still eligible for free tier
  2. Whether you are targeting the correct region
  3. Whether the account has already reached the free-instance limit

2. oci installs correctly but API calls fail with auth errors

That usually points to a problem in the config file, tenancy OCID, user OCID, fingerprint, or the uploaded API key. Recheck the values entered during oci setup config.

3. The ARM instance command keeps failing

Confirm that the target region really offers VM.Standard.A1.Flex, and that the shape-config values still match the current resource limits.


6. Automate ARM Server Creation Using Python

Oracle recently added ARM servers:

  • 4 Ampere A1 cores
  • 24 GB RAM
  • Can be split into up to 4 VMs

Use the following Python script to automate:

#!/usr/bin/env python3
from subprocess import Popen, PIPE
import time

cmd = '''
oci compute instance launch 
--availability-domain NFWU:AP-TOKYO-1-AD-1 
--display-name ja_arm 
--image-id ocid1.image.oc1.ap-tokyo-1.aaaa... 
--subnet-id ocid1.subnet.oc1.ap-tokyo-1.aaaa... 
--shape VM.Standard.A1.Flex 
--assign-public-ip false 
--compartment-id ocid1.tenancy.oc1..aaaa... 
--shape-config '{"ocpus":4,"memory_in_gbs":24,"local_disks":200}' 
--metadata '{"ssh_authorized_keys": "ssh-rsa AAAAB3..."}'
'''

while True:
    proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, encoding="utf-8")
    out, err = proc.communicate()
    if 'LimitExceeded' in err:
        print("Configuration failed or instance already created.")
        break
    time.sleep(10)

Once successful, add IPv4/IPv6 manually.

友情链接

其它