Terraform brings a new paradigm where Infrastructure becomes a Code, and with Cloud becoming what it is today, everyone is invited at the (devops) table. Therefore, after provisioning with oci-cli in my previous BlogPost, I will explore the same task using terraform.To add more fun, we won’t just deploy an instance but also configure a website linked to its public IP.
Note This lab will also help you practice if you are preparing for OCI Operations Associate exam(1Z0-1067) .
Overview and Concepts
The following illustration shows the layers involved between your workstation an Oracle cloud infrastructure while running the terraform commands along with the instance attributes we will be provisioning .
- Files are merged in alphabetical order but resource definition order doesn't matter (subfolders are not read)
- Common configurations have 3 type of tf files and a statefile
1- main.tf : terraform declaration code (configuration)
2- variables.tf : Resource variables needed for the deploy
3- outputs.tf : displays the resources detail at the end of the deploy
4- terraform.tfstate : keeps track of the state of the stack(resources) after each terraform apply run
Where the hell do I find a good deployment sample?
The most important thing when learning a new program is accomplishing your first HelloWorld. Unfortunately, google can’t always make the cut as samples I used had errors. Luckily, OCI Resource Manager had some samples I managed to export and tweak which was a good starting point for this lab.
Terraform lab content: I have deliberately split this lab in 2 :
- VCN Deployment: To grasp the basics of a single resource deployment
- FULL Instance Deployment: which is a more complex deployment (instance provisioning with a hosted web sever).
Linux : Download, unzip and move the binary to the local bin directory
Once installed run the version command to validate your installation
Tenancy_ocid, Compartment_ocid, user_ocid and the region
The private API key path and its fingerprint to authenticate with your tenancy account
The SSH key pair (Private/Public) required when launching the new compute instance
API Key authentication requires that you provide the following OCI credentials:
- Terraform shares most of the authentication parameters with oci-cli (located in
~/.oci/config ). Please refer to my Other post for details on how to setup oci-cli if it isn’t done yet.
- However, terraform also allows using environment variables to define these parameters. This is why I will be using a shell script that sets them before the deployment (I still needed oci-cli for API keys).
II. Clone the repository
- Pick an area that is close to your oci-terraform directory on your file system and issue the following command.
terraform-provider-oci/create-vcn/To grasp how we deploy a single VCN.
terraform-provider-oci/launch-instance/For the full instance deploy, once comfortable with terraform.
III. Provider setup
INSTALL AND SETUP THE OCI PROVIDER
- Cd Into the subdirectory
terraform-provider-oci/create-vcnwhere our configuration resides (i.e vcn )
create-vcndirectory. Here, only
*.tffiles matter along with
env-vars(click to see content)
env-varsfile according to your tenancy and key pairs
IV. Partial Deployment
DEPLOY A SIMPLE VCN
- Now that
env-varsvalues are set and sourced, we can run
terraform plancommand to create an execution plan (quick dry run to check the desired state/actions )
- - The output being too verbose I deliberately kept only relevant attributes for each VCN component
- Next, we can finally run
terraform deployto apply the changes required to create our VCN ( listed in the plan )
- The deploy started by loading the resources variables in variables.tf which allowed the execution of vcn.tf
- Finally terraform fetched the variables (ocids) of the resources listed in outputs.tf (lookup)
Note : In order to continue the lab we will need to destroy the vcn as the full instance launch will recreate it.
V. Full deployment (Instance)
- Awesome, After our small test let's launch a full instance from scratch .
- First we need to switch to the second directory
Here's its content:
Note: As you can see we have 2 additional files and one Subfolder.
compute.tf is where the compute instance and all its attributes are declared. All the other tf files come from my vcn example with some additions for variables.tf and output.tf
LAUNCH THE INSTANCE
- Once in the
launch-instancedirectory make sure you copied the adjusted
env-varsfile and sourced it (see
III. Provider setup). You can then run the plan command (output is truncated for more visibility)