Lab 46 (Advance Modular approach to create instance) - arunsinghchauhan38-arch/CLOUDARCHITECTDOC GitHub Wiki
Advance Modular approach to create instance
Stage 1. Create Instance Module (Template)
Step 1. Create the instance module folder (template folder)
-
Create a new folder to store the Terraform configuration files.
-
Inside the folder, create the required Terraform files that will be used to provision an OCI Instance.
Step 2 – Create the provider.tf File inside the instance_module folder
Configure the OCI Terraform Provider
The OCI Terraform provider is configured by specifying the authentication credentials and the target region that Terraform uses to connect to Oracle Cloud Infrastructure (OCI).
The following parameters are required:
-
Tenancy OCID – The unique identifier of your OCI tenancy.
-
User OCID – The unique identifier of the OCI user associated with the API key.
-
Fingerprint – The fingerprint of the API signing key uploaded to OCI.
-
Private Key Path – The local file path to the private key used for API authentication.
-
Region – The OCI region where Terraform will create and manage resources.
Step 3. Add the priv.pem File inside instance_module folder
Private Key File
The private key file stores the OCI API private key used by the Terraform provider for secure authentication with Oracle Cloud Infrastructure (OCI). During Terraform operations, this private key is used along with the API key fingerprint to verify the user's identity and authorize access to OCI resources.
The path to this private key is specified in the provider configuration using the private_key_path parameter.
Step 4 – Create the variable.tf File inside the instance_module folder
The variables.tf file declares the input variables used throughout the Terraform configuration, making the code reusable, flexible, and easy to configure for different environments.
It includes:
-
Definitions of all required input variables.
-
The data type for each variable (such as string, number, or bool).
-
Descriptions that explain the purpose of each variable.
These variables are referenced in the main.tf file, allowing the configuration to accept different values without modifying the core Terraform code. This approach improves reusability, maintainability, and consistency across multiple deployments.
Step 5. Create the main.tf File inside the instance_module folder
The main.tf file contains the main Terraform configuration used to define and provision Oracle Cloud Infrastructure (OCI) resources.
In this file, the OCI Compute Instance resource is configured along with its associated settings, such as:
- Availability Domain
- Compartment ID
- Shape and Compute Configuration (OCPUs and Memory)
- Source Image
- Virtual Cloud Network (VCN) and Subnet
- SSH Public Key
- Display Name
- Metadata and other instance-specific settings
The main.tf file references the input variables defined in variables.tf, making the configuration reusable and allowing different values to be supplied for different environments without modifying the core Terraform code.
Stage 2. Create Instance Code Folder
Step 1. Create the Dev environment Folder
we create this folder for contains Terraform configuration for the Dev environment.
Step 2. Create the provider.tf file inside the dev folder
Step 3. Create the instance.tf file inside dev folder
Step 4. Create "terraform.tfvars" File Under the "dev" Folder
The terraform.tfvars file is created under the environments/dev folder to provide values for the input variables defined in variables.tf.
The main purpose of this file is to replace hardcoded values in the Terraform configuration with variables. This makes the code reusable, easier to maintain, and allows the same Terraform code to be deployed across different environments by simply changing the values in the terraform.tfvars file.
We copied the values that were previously hardcoded in the instance.tf file and moved them into the terraform.tfvars file. In this example, we moved the following three configuration values:
env_display_name = "terraform_server"
env_memory_in_gbs = "12"
env_ocpus = "1"
and also change the variable name
dev_display_name = "terraform_server"
dev_memory_in_gbs = "12"
dev_ocpus = "1"
Step 5. now we call those variable in dev folder instance.tf file by "var."
env_display_name = var.dev_display_name
env_memory_in_gbs = var.dev_memory_in_gbs
env_ocpus = var.dev_ocpus
Next we define and declare those variable in new file variable.tf.
Step 6. Create variable.tf file inside dev folder
In variable.tf file we declare and define the instance.tf file variable
variable "dev_display_name" {
type = string
description = "this variable defines instance name"
}
variable "dev_memory_in_gbs" {
type = string
description = "this variable defines memory size"
}
variable "dev_ocpus" {
type = string
description = "this variable defines ocpus count"
}
Note:
In the terraform.tfvars file, you don’t need to specify the source path. This file is advanced enough to automatically detect the source from the environment.
If we create an instance using the available code, it will work smoothly without errors because the code is well‑structured and validated.
However, if we try to create another instance within the same environment and run terraform init and terraform plan, it will show a duplicate error. This happens because the parameters are already registered in memory.
To resolve this issue, we will move forward with the next step in the process.
Step 7. Create new variable inside variable.tf file inst_var and give its type as map.
A Map is a variable that stores multiple values together in a single variable. Each value is associated with a unique key, which is used to identify and access that value.
A map must have a defined type. We often use the object type because an object can store multiple key–value pairs. The values in an object can also be of different data types, such as strings, numbers, booleans, arrays, or even other objects.
variable "inst_var" {
type = map(object({
dev_display_name = string
dev_memory_in_gbs = string
dev_ocpus = string
})
)
}
Step:
Add a new variable named inst_var in the variable.tf file.
Set its type to map(object) so that each entry in the map is a unique object.
Insert the required data inside this map variable.
Remove all other variable definitions from the file, keeping only this new map variable.
Step 8. Define the map variable in terraform.tfvars
In the terraform.tfvars file, define the inst_var map variable.
Inside this map, create entries for our instances ( instance1, instance2).
Each entry include parameters like display name, memory, and OCPUs, customized as per our environment needs.
inst_var = {
"instance1" = {
dev_display_name = "dev_instance"
dev_memory_in_gbs = "12"
dev_ocpus = "1"
}
instance2 = {
dev_display_name = "dev2_instance"
dev_memory_in_gbs = "24"
dev_ocpus = "2"
}
}
Step 9. Add for_each block in instance.tf
Insert a for_each block inside the instance.tf file.
This allows Terraform to loop through the inst_var map defined in terraform.tfvars and create multiple instances sequentially.
Each entry (like instance1, instance2) will be read from the map and a compute instance will be created for it.
module "dev_instance" {
source = "/home/arunsinghc/instance_codes/instance_module"
for_each = var.inst_var
env_display_name = each.value.dev_display_name
env_memory_in_gbs = each.value.dev_memory_in_gbs
env_ocpus = each.value.dev_ocpus
}
Step 10. Remove provider.tf file from the instance_module folder before use init command
-
Before running the terraform init command, delete the provider.tf file from the instance_module folder.
-
This step ensures that the provider configuration is not duplicated inside the module.
-
The provider should only be defined once at the root level of your Terraform project (not inside individual modules).
-
By removing it, you avoid conflicts and initialization errors when Terraform sets up the environment.
Step 11. Initialize the Terraform Working Directory
terraform init
Step 12.Review the Execution Plan
terraform plan
Step 13. Provision the Instance
terraform apply
When prompted, type:
yes
Step 14. Verify the Compute Instance in OCI Console
After the terraform apply command completes successfully, sign in to the OCI Console.
Navigate to: Menu → Compute → Instances
Check that the new Compute Instance has been created.
Verify the configuration details match what you defined in your terraform.tfvars file:
Display Name (e.g., dev_instance, dev2_instance)
Shape (VM type)
OCPUs
Memory (GBs)
Step 15. Destroy the Compute Instance
terraform destroy
When prompt type:
yes