Repository navigation
LAB 44 (create instance using modular approach)
The Terraform modular approach in OCI is a method of structuring your Infrastructure as Code (IaC) so that it is organized, reusable, and environment‑agnostic.
In Oracle Cloud Infrastructure (OCI), the modular approach means:
-
Breaking down Terraform configurations into modules (self‑contained packages of resources).
-
Each module represents a logical unit (e.g., compute instance, VCN, subnet, security list).
-
These modules can be reused across multiple environments (Dev, Test, UAT, Prod) by passing different variable values.
-
It ensures consistency in deployments, reduces duplication, and makes infrastructure easier to maintain.
-
Reusable: One module can be applied to different environments.
-
Composable: Complex infrastructure is built by combining smaller modules.
-
Maintainable: Updates in the module automatically propagate to all environments using it.
-
Scalable: Easy to add new environments without rewriting code.
First we separate the provider block from the insatnce.tf file and create new file name provider.tf and move all the details of provider block here.
Note :- terraform reads only files with .tf name other than .tf if we give other name like .tf1 to the file so terraform can't read it.
Every terraform file should be end with .tf .
after creating dev folder we drag our instance.tf, priv.pem, provider.tf, terraform.tfstate and terraform.tfstatebackup file in to the dev folder
Now we try to plan and its work
Instance module folder we create for our module preparation
Instance Module Folder created.
when we create a module or template so we bound to create a file named main.tf for get ready a template
main.tf file created under instance module folder
Next we move the instance.tf file resource code in to the main.tf file
Here we try to create a template where we will only pass the parameter which are changing and that template will add the rest all the parameter and then our code reuse will increase.
Now in instance.tf file we will add parameter to pass by instance.tf as template
We generally add here
-
copy the entire display name from main.tf file and paste into the instance.tf file.
-
copy the entire memory value and paste into the instance.tf file.
-
copy the entire ocpus value and paste into the instance.tf file.
next we will also copy the priv.pem private key and provider.tf file from dev folder and paste into the instance_module folder. because provider is the one file which in bound to keep it in to every folder which we create.
Now in dev folder in instance.tf file we give the name of our variable - env
env_display_name = "teraform_server"
env_memory_in_gbs = "12"
env_ocpus = "1"
Here env_display name is variable and "teraform_server" is variable value.
“Next, copy the env_display_name variable from the instance.tf file and paste it into the main.tf file resource block under the display_name section, replacing the hardcoded value. This ensures that the display name is dynamically referenced from instance.tf instead of being fixed as a static value.”
when we paste the variable file in to the resource block that time we use var. in the start of the variable name. This Var. full form is Variable and in shortform we use as var. .
Whenever we pass variable in terraform that time we need to declare variable and after that define it. so we first create a new file name variable.tf
In the variable.tf file we declare that what is this variable and what is the type of this variable.
In this file declare variable and its type by given proper type of variable.
Here anything comes with double inverted commas (" ") is string and any thing comes without any inverted commas is number.
Now our file ready to create instance by using this module or template.
but we see in the instance.tf file red lines showing under the our variables. it mean its a error so first we need to clear it.
Error comes due to we are not provide any path to the template file to perform and take parameter from the dev.
Now we first give the path for it. To know the location of the our template we use command pwd.
pwd
and we know our template location
/home/arunsinghc/instance_module
after knowing our template location we will give this into the instance.tf file with keyword source.
In source we define where is the template
source = "/home/arunsinghc/instance_module"
but our error now not clear, As we know in terraform, terraform read or understand anything starting with curly brackets { and ending with } curly brackets. so we add the curly brackets.
We added curly bracket but our error still showing. To solve this error we add
Whenever we pass something from resource block we called it is as a module and we must give the name to this module.
We add module and give name dev_instance and we see that our all error clear and our module ready to create instance.
Step 4. Create test,uat and prod Folder and copy paste the provide.tf and instance.tf file in to each folder
Now we update both test,uat and prod instance as per our requirement.
Now we are ready to create our instance by our module.
We Create our Dev, Prod, test and uat instance by using our module template
Commands
terraform init
terraform plan
terraform apply
As we seen we are successfully created our instance using terraform module approach
Now we Destroy our instance
For Destroy
terraform destroy
Our instances start to terminating
Now our instances successfully terminated