Create ssh encryption keys to connect to your compute instance.
Open a terminal window:
MacOS or Linux: Open a terminal window in the directory where you want to store your keys.
Windows: Right-click the directory where you want to store your keys and select Git Bash Here.
Note
If you're using Windows Subsystem for Linux (WSL), ensure that the directory for the keys is directly on your Linux machine and not in a /mnt folder (windows file system).
If your username is in the Administrators group, then skip this section. Otherwise, ask your administrator to add the following policy to your tenancy:
Copy
allow group <a-group-your-username-belongs-to> to manage all-resources in compartment <your-compartment-name>
With this privilege, you can manage all resources in your compartment, giving you administrative rights in that compartment.
First, set up a directory for your Terraform scripts. Then copy the provider and versions scripts from the Set Up OCI Terraform tutorial so your Oracle Cloud Infrastructure account can authenticate the scripts running from this directory.
In your $HOME directory, create a directory called tf-compute, and then change to that directory.
Copy
mkdir tf-compute
Copy
cd tf-compute
Copy the provider.tf file into the tf-compute directory.
Copy
cp ../tf-provider/provider.tf .
Copy the versions.tf file into the tf-compute 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.
Copy the availability-domains.tf file into the tf-compute directory.
The availability-domains.tf file was created during the tutorial Set Up OCI Terraform.
Copy
cp ../tf-provider/availability-domains.tf .
Example code:
Copy
# Source from https://registry.terraform.io/providers/oracle/oci/latest/docs/data-sources/identity_availability_domains
data "oci_identity_availability_domains" "ads" {
compartment_id = "<tenancy-ocid>"
}
In the tf-compute directory, create a file called
outputs.tf.
Note
Ensure that outputs.tf, provider.tf,
and availability-domains.tf are in the same
directory.
To output the name of the first availability domain in the list of oci_identity_availability_domains, add the following code to outputs.tf.
Copy
# The "name" of the availability domain to be used for the compute instance.
output "name-of-first-availability-domain" {
value = data.oci_identity_availability_domains.ads.availability_domains[0].name
}
Save the outputs.tf file.
Run your scripts with Terraform:
Copy
terraform init
Copy
terraform plan
Copy
terraform apply
When prompted for confirmation, enter yes for your data to
be fetched and displayed in the output.
You now have an output with the name of the availability domain to use for
your instance.
Replace <compartment-ocid>, <source-ocid>, <your-ubuntu-instance-name>, and <subnet-ocid> with the information you collected at Gather Required Information.
For availability domain, use the name you fetched with the data source:
In Terraform, resources are objects such as virtual cloud networks (VCNs) or compute instances. You can create, update, and delete them with Terraform.
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
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 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.