Terraform Compute Instance Provisioning on Oracle Cloud Infrastructure and Google Cloud

Infrastructure as Code with Terraform allows repeatable, versioned creation of compute instances across cloud providers. The workflows for Oracle Cloud Infrastructure and Google Cloud share common patterns: provider configuration, data source discovery, resource declaration with required arguments, output exposure, and lifecycle management with terraform apply and terraform destroy. The following sections unpack the detailed steps and configuration objects referenced in the official tutorials and module documentation.

Oracle Cloud Infrastructure Compute Instance with Terraform

The Oracle Cloud Infrastructure tutorial guides creation of a compute instance in a tenancy using the OCI Terraform provider. Key tasks include creating SSH keys, creating a virtual cloud network in the tenancy, using the Oracle Cloud Infrastructure Terraform provider to create a compute instance in the network, and connecting to the instance.

Prerequisites and Environment Preparation

To successfully perform the tutorial you must have an Oracle Cloud Infrastructure account. See Request and Manage Free Oracle Cloud Promotions. A MacOS, Linux, or Windows computer is required. Terraform tutorial resources are needed to prepare the environment for creating a compute instance and to collect all information needed to complete the tutorial.

The prepare phase begins with networking. Open the navigation menu, select Networking, and then select Virtual cloud networks. On the Virtual Cloud Networks list page, perform one of the following actions depending on the option that you see: From Actions, select Start VCN Wizard, or Select Start VCN Wizard. In the Start VCN Wizard panel, select Create VCN with Internet Connectivity. The Create a VCN with internet connectivity window opens.

Availability Domain Data Source

An availability domain is one of the required inputs to create a compute instance. When you set up Terraform in the first tutorial, Set Up OCI Terraform, the output block included the following line:

value = data.oci_identity_availability_domains.ads.availability_domains

Then the output was similar to the following:

Outputs: all-availability-domains-in-your-tenancy = tolist([ { "compartment_id" = "ocid1.tenancy.xxx" "id" = "ocid1.availabilitydomain.xxx" "name" = "QnsC:US-ASHBURN-AD-1" }, { "compartment_id" = "ocid1.tenancy.xxx" "id" = "ocid1.availabilitydomain.xxx" "name" = "QnsC:US-ASHBURN-AD-2" }, { "compartment_id" = "ocid1.tenancy.xxx" "id" = "ocid1.availabilitydomain.xxx" "name" = "QnsC:US-ASHBURN-AD-3" }, ])

Now you want to fetch the name of the first availability domain in the list, to use for the location of your compute instance later:

"name" = "xxxx:US-ASHBURN-AD-1"

The attributes for Data Source: ociidentityavailability_domains are:

availabilitydomains, a list with three string attributes:
- compartment
id
- id
- name

availability_domains, a list with three string attributes:
- Use square brackets to add an index to a list attribute.
- Use the index 0 for the first item in a list.
- Use a dot after the square brackets followed by an attribute of the list, to specify that attribute.
- Example: First item in the list:
value = data.oci_identity_availability_domains.ads.availability_domains[0]
- Example: Name of first item in the list:
value =

Declaring the OCI Core Instance Resource

You can create, update, and delete compute instances with Terraform. To declare a compute resource:

  • Go to Oracle Cloud Infrastructure Provider.
  • In the Filter box on the upper left, enter core instance
    Results include data sources and resources for several services.
  • Under Core, go to Resources and select ocicoreinstance.

The title of the page is the resource type:
ocicoreinstance

In the Argument Reference section, use the following required arguments (inputs):
- availabilitydomain
- compartment
id
- shape
- sourcedetails
- source
id
- source_type

Construct a resource block:
- Declare a resource block with the keyword:
resource
- Add a label for resource type:
"ocicoreinstance"
- Add a label for a local name (your choice):

The label can contain letters, digits, underscores ( _ ), and hyphens ( - ). The first character must not be a digit.
- Example:
"ubuntu_instance"

The label can contain letters, digits, underscores (
- Inside the code block, provide a value for required arguments. They don't have a default value.
- For optional arguments, provide values for the ones you want to override. Otherwise, their default values are used.
- Declare a resource block with the keyword:

Add output blocks to your code to get information about your compute instance after Terraform creates it.

Go to Attributes Reference (ocicoreinstance).

Attributes are the outputs that you can return for the ocicoreinstance resource.

Search for the attribute for public IP:
public_ip

Construct a resource output block for public_ip:
For the value expression, use the following format:

value = ..

Example:
value = ocicoreinstance.ubuntuinstance.publicip

For the value expression, use the following format:
Create an output block for each of the following outputs:
- displayname
- id
- region
- shape
- state
- ocpus
- memory
ingbs
- time
created

Required arguments for ocicoreinstance

Argument Description
availability_domain Availability domain name for the instance
compartment_id OCID of the compartment
shape Instance shape, e.g., VM.Standard.E3.Flex
source_details Map of source details
source_id OCID of the image or boot volume
source_type Source type, e.g., image

Instance outputs

Output Attribute
display_name ocicoreinstance..display_name
id ocicoreinstance..id
region ocicoreinstance..region
shape ocicoreinstance..shape
state ocicoreinstance..state
ocpus ocicoreinstance..ocpus
memoryingbs ocicoreinstance..memoryingbs
time_created ocicoreinstance..time_created
public_ip ocicoreinstance..public_ip

Run Scripts and Lifecycle

Run your Terraform scripts to create the compute instance in a compartment in your tenancy. Use your SSH keys to connect to the instance. When you no longer need your instance, destroy it with Terraform.

Google Cloud Compute Engine VM Instance with Terraform

The Google Cloud quickstart teaches how to use Terraform to create a Compute Engine Virtual Machine (VM) instance and connect to that VM instance. Hashicorp Terraform is an Infrastructure as code (IaC) tool that lets you provision and manage cloud infrastructure. Terraform provider for Google Cloud (Google Cloud provider) lets you provision and manage Google Cloud infrastructure.

Before You Begin

To use an online terminal with the gcloud CLI and Terraform already set up, activate Cloud Shell:

At the bottom of this page, a Cloud Shell session starts and displays a command-line prompt. It can take a few seconds for the session to initialize.

Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role ( roles/resourcemanager.projectCreator ), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
- Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace PROJECT_ID with a name for the Google Cloud project you are creating

Terraform Configuration for a VM

This file defines the Google Cloud resources that you want to create.

cat main.tf

The output is similar to the following

This file describes the googlecomputeinstance resource, which is the Terraform resource for the Compute Engine VM instance. googlecomputeinstance is configured to have the following properties:
name is set to my-vm
.machinetype is set to n1-standard-1
.zone is set to us-central1-a
.boot
disk sets the boot disk for the instance
.network_interface is set to use the default network in your Google Cloud project

Initialize and Verify Terraform

Create the Compute Engine VM instance

In Cloud Shell, run the following command to verify that Terraform is available:
terraform

The output should be similar to the following:
Usage: terraform [global options] [args] The available commands for execution are listed below. The primary workflow commands are given first, followed by less common or more advanced commands. Main commands: init Prepare your working directory for other commands validate Check whether the configuration is valid plan Show changes required by the current configuration apply Create or update infrastructure destroy Destroy previously-created infrastructure

Initialize Terraform by running the following command. This command prepares your workspace so Terraform can apply your configuration.
terraform init

The output should be similar to the following:
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/google..

Google Compute Instance properties

Property Value
name my-vm
machine_type n1-standard-1
zone us-central1-a
boot_disk sets the boot disk for the instance
network_interface uses default network in project

Terraform Google VM Module Parameters

This module is used to create compute instances (and only compute instances) using googlecomputeinstancefromtemplate, with no instance groups.

See the simple for a usage example.

Module input parameters

Name Description Type Default Required
access_config Access configurations, i.e. IPs via which the VM instance can be accessed via the Internet. list(object({ [] no
addhostnamesuffix Adds a suffix to the hostname bool true no
aliasipranges (Optional) An array of alias IP ranges for this network interface. Can only be specified for network interfaces on subnet-mode networks. list(object({ [] no
deletion_protection Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully. bool false no
hostname Hostname of instances string "" no
hostnamesuffixseparator Separator character to compose hostname when addhostnamesuffix is set to true. string "-" no
instance_template Instance template self_link used to create compute instances string n/a yes
ipv6accessconfig IPv6 access configurations. Currently a max of 1 IPv6 access configuration is supported. If not specified, the instance will have no external IPv6 Internet access

Conclusion

Terraform compute instance provisioning requires provider-specific data discovery and resource declaration patterns. For Oracle Cloud Infrastructure, availability domain data sources must be indexed to supply the location, and the ocicoreinstance resource demands availabilitydomain, compartmentid, shape, sourcedetails, sourceid, and sourcetype as required arguments. Outputs such as publicip, displayname, id, region, shape, state, ocpus, memoryingbs, and timecreated provide post-creation observability. The workflow couples VCN creation with SSH key management and instance connection.

For Google Cloud, the googlecomputeinstance resource maps name, machinetype, zone, bootdisk, and networkinterface to a Compute Engine VM. Provider initialization via terraform init and verification via terraform ensures the hashicorp/google plugin is resolved. Project creation requires the Project Creator role roles/resourcemanager.projectCreator with resourcemanager.projects.create permission, and Cloud Shell provides a preconfigured environment. The Terraform Google VM module extends this by using googlecomputeinstancefromtemplate with parameters such as accessconfig, addhostnamesuffix, aliasipranges, deletionprotection, hostname, hostnamesuffixseparator, instancetemplate, and ipv6accessconfig, enabling template-based instance creation without instance groups.

Both approaches emphasize declarative configuration, explicit outputs, and destroyable infrastructure, delivering repeatable compute provisioning across Oracle Cloud Infrastructure and Google Cloud.

Sources

  1. Oracle Cloud Infrastructure Terraform Compute Tutorial
  2. Google Cloud Terraform Create VM Instance
  3. Terraform Google VM Module README

Related Posts