Terraform - small test

Once we have setup the Terraform, let's perform a few simple steps for creating an instance.

Prepare environment

1. Create a new folder for your Terraform project

root@deploymentmachine:/home# mkdir terra
root@deploymentmachine:/home# cd terra
root@deploymentmachine:/home/terra#

2. Create the following files: main.tf, provider.tf, variables.tf

Your folder should be looking like this:

root@deploymentmachine:/home/terra# tree
.
├── main.tf
├── provider.tf
└── variables.tf

Content of Terraform files

a) provider.tf - contains information about your provider (oci), tenancy OCID, user OCI, private key path, the fingerprint and the region:

root@deploymentmachine:/home/terra# more provider.tf
provider "oci" {
  tenancy_ocid = "ocid1.tenancy.oc1..aaaaaaaafaketenancyocidherehahaha342342"
  user_ocid = "ocid1.user.oc1..aaaaaaaafakeuserocidherehahaha23423"
  private_key_path = "/root/.oci/oci_api_private_key.pem"
  fingerprint = "2x:x4:xx:x5:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
  region = "eu-frankfurt-1"
}

b) variables.tf - just passing some information that I do not want to find it hardcoded in the main.tf.

This instance will be created in the root compartment. For collecting the necessary details either use the OCI or simply run OCI CLI command:

Remember to setup your Compartment OCID (if not saved in .bashrc) and supress warnings:

  • for the instance shape

  • for instance image

Oracle provides a really good source of information about the OS images you can deploy on your instances (in this example, for Linux):

  • for subnet OCID

Find the VCN OCID:

Suppose I want the 3rd VCN OCID:

Now I can find my Subnet OCID

  • for available domain (pick one of them)

In the end, possible content for variables.tf in our case would be:

c) main.tf file - where I pass the variables and resources for creation of an instance ... do mind the ssh_authorized_keys

Run terraform commands

The usual three commands:

terraform init to initialize the provider

terraform plan to indicate what changes will be implemented:

And apply changes, by running terraform apply

You can check in the OCI UI if the deployment is successfull or not, since the provisioning of the instance is almost immediate:

Test the ssh keys, by logging in to your new host (I can show the public IP, since I will be destroying this in a minute or two...)

Terminate the instance by running command terraform destroy

Now the instance "deploymentmachine" is ready to start automating the creation of OCI services/arhitectures with the help of Terraform

Last updated