Terraform: Create a Compute Instance
In this tutorial, you use Terraform to create a compute instance in your Oracle Cloud Infrastructure tenancy.
Key tasks include how to:
- Create SSH keys.
- Create a virtual cloud network in your tenancy.
- Use Oracle Cloud Infrastructure Terraform provider to create a compute instance in the network.
- Connect to your instance.
For additional information, see:
Before You Begin
To successfully perform this tutorial, you must have the following:
- An Oracle Cloud Infrastructure account. See Signing Up for Oracle Cloud Infrastructure.
- A MacOS, Linux, or Windows computer.
- Terraform tutorial resources:
- Go through all the steps in:
- Keep the scripts you created in the
following directories:
$HOME/tf-provider/
$HOME/tf-compartment/
- Keep the compartment from the Create a Compartment tutorial in your tenancy.
1. Prepare
Prepare your environment for creating a compute instance. Also, collect all the information you need to complete the tutorial.
Create SSH Encryption Keys
Create ssh
encryption keys to connect to your compute
instance.
You have generated the required encryption keys.
See Creating a Key Pair for more detailed information
on generating ssh
encryption keys.
Set up a VCN to connect your Linux instance to the internet. You configure all the components needed to create your virtual network.
Prepare the information you need and copy them into your notepad.
If your username is in the Administrators group, then skip this section. Otherwise, have your administrator add the following policy to your tenancy:
allow group <the-group-your-username-belongs> to manage all-resources in compartment <your-compartment-name>
With this privilege, you can manage all resources in your compartment, essentially giving you administrative rights in that compartment.
- Open the navigation menu and click Identity & Security. Under Identity, click Policies.
- Select your compartment from the Compartment drop-down.
- Click Create Policy.
- Fill in the following information:
- Name:
manage-<your-compartment-name>-resources
- Description:
Allow users to list, create, update, and delete resources in <your-compartment-name>.
- Compartment:
<your-tenancy>(root)
- Name:
- For Policy Builder, select the following choices:
- Policy use cases:
Compartment Management
- Common policy templates:
Let compartment admins manage the compartment
- Groups:
<the-group-your-username-belongs>
- Location:
<your-tenancy>(root)
- Policy use cases:
- Click Create.
Reference: Common Policies
2. Create Scripts
Create four scripts: one for authentication, one to fetch data, one to create a compute instance, and one to print outputs.
First, set up a directory for your Terraform scripts. Then add a provider script so your Oracle Cloud Infrastructure account can authenticate the scripts running from this directory.
Fetch the name of an availability domain from your account. An availability domain is one of the required inputs to create a compute instance.
When you set up OCI Terraform in the first tutorial, you had the following code in your output block:
value = data.oci_identity_availability_domains.ads.availability_domains
Then, you had an output 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" = "QnsC:US-ASHBURN-AD-1"
- The attributes for Data Source: oci_identity_availability_domains are:
- availability_domains, 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 = data.oci_identity_availability_domains.ads.availability_domains[0].name
Declare an Oracle Cloud Infrastructure compute resource, and then define the specifics for the instance.
In Terraform, resources are objects such as virtual cloud networks or compute instances. You can create, update, and delete them with Terraform. To declare a compute resource:
- Go to Oracle Cloud Infrastructure Provider.
- In the left navigation Filter, enter
core instance
.Results are returned for both Data Sources and Resources for several services.
- Under Core, go to Resources and click oci_core_instance.
- Find the Resource Type from the title of the page:
- Type:
oci_core_instance
- Type:
- In the Argument Reference section, use the following arguments (inputs)
labeled as (Required):
- availability_domain
- compartment_id
- shape
- source_details
- source_id
- source_type
- Construct a resource block:
- Declare a resource block with the keyword:
resource
- Add a label for resource type:
"oci_core_instance"
- 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"
- Inside the code block, provide a value for the 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.
- On the Resource: oci_core_instance
page, go to Attributes Reference.Note
Attributes are the outputs that you can return for the oci_core_instance 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 = <type>.<local-name-for-resource>.<attribute>
- Example:
value = oci_core_instance.ubuntu_instance.public_ip
- For the value expression, use the following format:
- Create an output block for each of the following outputs:
- display_name
- id
- region
- shape
- state
- ocpus
- memory_in_gbs
- time_created
3. Run Scripts
Run your Terraform scripts. After, your account authenticates the scripts, Terraform creates a 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.
Congratulations! You have successfully created a compute instance using Terraform, in your Oracle Cloud Infrastructure account.
References:
What's Next
For the next Terraform: Get Started tutorial, go to:
To explore more information about development with Oracle products, check out these sites: