terrform modules dry run use "local_exec" , has dev and qa module - unix1998/technical_notes GitHub Wiki
Here is an example of how to structure your Terraform project with dev and qa directories, each containing their respective modules. The root directory will contain a main Terraform configuration file to call these modules.
Directory Structure
terraform/
├── main.tf
├── dev/
│ └── main.tf
├── qa/
│ └── main.tf
Root Directory main.tf
Create the main Terraform configuration file in the root directory to call the dev and qa modules.
# terraform/main.tf
terraform {
required_providers {
null = {
source = "hashicorp/null"
version = "~> 3.0"
}
}
}
module "dev" {
source = "./dev"
}
module "qa" {
source = "./qa"
}
dev Module
Create a main.tf file inside the dev directory with the following content:
# terraform/dev/main.tf
resource "null_resource" "dev" {
provisioner "local-exec" {
command = "echo Hello from dev DEMO!"
}
}
qa Module
Create a main.tf file inside the qa directory with the following content:
# terraform/qa/main.tf
resource "null_resource" "qa" {
provisioner "local-exec" {
command = "echo Hello from qa DEMO!"
}
}
Explanation
-
Root
main.tf:- Defines the required provider for
null_resource. - Calls the
devandqamodules using thesourceattribute to specify the relative path to each module's directory.
- Defines the required provider for
-
dev/main.tf:- Defines a
null_resourcewith alocal-execprovisioner that executes a command to print "Hello from dev DEMO!".
- Defines a
-
qa/main.tf:- Defines a
null_resourcewith alocal-execprovisioner that executes a command to print "Hello from qa DEMO!".
- Defines a
Running the Terraform Configuration
-
Initialize Terraform: Navigate to the root directory and run:
terraform init -
Apply the Configuration: Run the following command to apply the configuration and execute the provisioners:
terraform applyYou should see output similar to:
null_resource.dev: Creating... null_resource.qa: Creating... null_resource.dev: Provisioning with 'local-exec'... null_resource.qa: Provisioning with 'local-exec'... null_resource.dev (local-exec): Hello from dev DEMO! null_resource.qa (local-exec): Hello from qa DEMO! null_resource.dev: Creation complete after 0s [id=1234567890] null_resource.qa: Creation complete after 0s [id=0987654321] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
This setup allows you to maintain separate modules for dev and qa environments while using a centralized configuration file to manage them.
run each module QA , dev , the 3 approaches :
To run only the dev module, we cannot directly use terraform apply dev as Terraform doesn't support specifying a module name directly in the apply command. However, there are a couple of ways to achieve this:
Method 1: Targeting Specific Resources
You can target specific resources within the dev module using the -target option in the terraform apply command. This method requires you to specify the exact resource you want to apply.
terraform apply -target=module.dev.null_resource.dev
This command will only apply the resources within the dev module.
Method 2: Separate Configuration File
Another approach is to create a separate configuration file that only includes the dev module. This allows you to apply configurations specifically for dev without affecting other modules.
Create a new file named dev_only.tf in the root directory with the following content:
# terraform/dev_only.tf
terraform {
required_providers {
null = {
source = "hashicorp/null"
version = "~> 3.0"
}
}
}
module "dev" {
source = "./dev"
}
Now, you can apply only the dev module using this new configuration file:
terraform apply -var-file=dev_only.tf
Method 3: Using Terraform Workspaces
You can use Terraform workspaces to manage different environments. Although this is not strictly targeting a single module, it helps in managing different environments within the same configuration.
- Create and Switch to a New Workspace for
dev:
terraform workspace new dev
terraform workspace select dev
- Apply Configuration:
terraform apply
In this approach, you can create workspaces for dev and qa, and apply configurations in the context of the specific workspace.
Summary
The most straightforward approach to apply only the dev module is using the -target option:
terraform apply -target=module.dev.null_resource.dev
This command targets and applies only the resources within the dev module.