This guide explains how to provision a compute instance in Oracle Cloud Infrastructure (OCI) using Python scripts and the OCI Python SDK. The provided Python script (create_instance.py
) automates the process of launching a compute instance.
Before you begin, ensure you have the following prerequisites:
-
Install OCI Python SDK:
Install the Oracle Cloud Infrastructure Python SDK on your local machine. Follow the instructions in the official installation guide.
-
Generate API Key and Configuration File:
Generate an API key and create a configuration file (
~/.oci/config
) to authenticate with OCI. Refer to the following resources for guidance: -
Define Configuration Variables:
In the
oci_config.py
file (located in the same folder ascreate_instance.py
), define the following variables:compartment_id
: Your designated compartment ID in your tenancy.ssh_public_key_path
: Path to your RSA public key for SSH communication with the instance.shape_name
: Name of the desired shape for the compute instance.operating_system_version
: The OS version you want to use.public_subnet_id
: Public subnet ID from your Virtual Cloud Network (VCN).instance_name
: Set a name for your compute instance.
Below is a description of the functions and code in create_instance.py
:
-
config = oci.config.from_file("~/.oci/config", "DEFAULT")
: Creates an account configuration from~/.oci/config
. -
identity_client = oci.identity.IdentityClient(config)
: Generates an identity client for working with OCI's identity and access management. -
compute_client = oci.core.ComputeClient(config)
: Creates a compute client for interacting with compute instances in OCI. -
compute_client_composite_operations = oci.core.ComputeClientCompositeOperations(compute_client)
: Creates a compute client composite to perform composite operations. -
compartment_id = oci_config.compartment_id
: Retrieves the compartment ID specified inoci_config.py
. -
get_availability_domain(identity_client, compartment_id)
: Retrieves a list of all available domains in your tenancy and selects the first one. -
get_shape(compute_client, compartment_id, availability_domain)
: Retrieves a list of available shapes and selects one based on the shape name defined inoci_config.py
. -
get_image(compute, compartment_id, shape)
: Retrieves a list of available images, selects an image based on the specified OS and version. -
get_launch_instance_details(compartment_id, availability_domain, shape, image, subnet_id, ssh_public_key)
: Generates instance launch details and metadata based on provided arguments. -
launch_instance(compute_client_composite_operations, launch_instance_details)
: Creates an OCI compute instance based on launch details and waits for the instance to reach the 'RUNNING' state.
To provision a compute instance, follow these steps:
-
Install Dependencies:
Install the required dependencies using pip:
pip install oci
-
Run the Script: python create_instance.py
You can customize the script further based on your requirements, such as adding additional metadata, customizing the shape configuration, or specifying a different OS image.