- Terminal -> New Terminal –> “Default Shell” –> Windows
That’s it.
The post Workspaces in TerraforM appeared first on For all your terraform needs.
]]>terraform workspace new <workspace_name>
terraform workspace list
terraform workspace select <workspace_name>
terraform workspace delete <workspace_name>
Suppose you have a Terraform configuration that sets up infrastructure for a web application. You want to manage separate environments (development, staging, production) using workspaces.
terraform workspace new development
terraform workspace new staging
terraform workspace new production
variable "environment" {
description = "The environment for this configuration"
type = string
}
locals {
environment = terraform.workspace
}
resource "aws_instance" "web" {
count = local.environment == "production" ? 3 : 1
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "web-server-${local.environment}"
}
}
terraform workspace select development
terraform apply
terraform workspace select staging
terraform apply
terraform workspace select production
terraform apply
Each workspace maintains a separate state file, ensuring that the resources for each environment are managed independently.
Imagine you are managing infrastructure for multiple clients, and you want to keep each client’s resources isolated.
terraform workspace new clientA
terraform workspace new clientB
variable "client_name" {
description = "The client for this configuration"
type = string
}
locals {
client = terraform.workspace
}
resource "aws_s3_bucket" "client_bucket" {
bucket = "myapp-${local.client}-bucket"
acl = "private"
}
terraform workspace select clientA
terraform apply
terraform workspace select clientB
terraform apply
This ensures that each client’s resources are managed separately, preventing any accidental overlap or interference.
Workspaces in Terraform provide a powerful way to manage multiple environments or configurations within a single codebase. By using workspaces, you can maintain separate state files and easily switch between different setups, whether for different environments (like development, staging, production) or different clients. This isolation helps to ensure that changes in one workspace do not affect resources in another, providing a more organized and secure way to manage infrastructure.
The post Workspaces in TerraforM appeared first on For all your terraform needs.
]]>The post GitHub Actions versus GitLab CI/CD appeared first on For all your terraform needs.
]]>git init
git status
git checkout -b <branch-name>
git add
git commit -m "your message goes here"
git remote add
git push
git pull
git clone
The post GitHub Actions versus GitLab CI/CD appeared first on For all your terraform needs.
]]>The post Resource Shutdown via terraform appeared first on For all your terraform needs.
]]>However, sometimes, instead of destroying resources, all you want to do is shut them down or disable them. This is also doable using remote-exec in terraform
resource "aws_instance" "app" {
# ...
provisioner "remote-exec" {
when = "destroy"
inline = [ "systemctl stop service" ]
}
}
The post Resource Shutdown via terraform appeared first on For all your terraform needs.
]]>The post Terraform for IAM resource creation appeared first on For all your terraform needs.
]]>Is terraform a suitable tool for creating and managing cloud IAM resources? The short answer is – no. Especially, if you are going to be dealing with a large number of such creation requests.
Check whether the IAM resource already exists – across all buckets that store state. This could take hours. Can you afford to wait for hours for a simple identity creation or a role assignment?
Certain IAM actions performed via the console will cause state drift. The next run of your IAM script will not pick these up – and essentially wipe them out.
Of course, if you have IAM creation etc. locked out for console users, you will not face this particular issue.
For smaller scoped IAM requests – say you have a dozen or so requests to deal with – terraform for IAM may be a workable solution.
The post Terraform for IAM resource creation appeared first on For all your terraform needs.
]]>The post Error: retrieving `contact` for KeyVault: keyvault.BaseClient#GetCertificateContacts: Failure sending request: StatusCode=0 — Original Error: context deadline exceeded appeared first on For all your terraform needs.
]]>
data "azurerm_client_config" "current" {}
resource “azurerm_key_vault” “my_key_vault” { name = “sample-keyvault-av”
location = azurerm_resource_group.rg_des.location
resource_group_name = azurerm_resource_group.rg_des.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = “standard”
#sku_name = “premium”
enabled_for_disk_encryption = true
#soft_delete_enabled = true
purge_protection_enabled = true
}
}
The issue has been fixed in version 3.3.0 of the azure provider
The post Error: retrieving `contact` for KeyVault: keyvault.BaseClient#GetCertificateContacts: Failure sending request: StatusCode=0 — Original Error: context deadline exceeded appeared first on For all your terraform needs.
]]>The post Retrieving Resource IDs in Terraform – The Terraform Data Block appeared first on For all your terraform needs.
]]>data “google_project” “project” {
}
output "project_number" {
value = data.google_project.project.number
}
Terraform Destroy and the Data Block
When executing the terraform destroy command on our Terraform configuration, Terraform does not perform a destroy action on the resource called by the data block. It is a read only block.
The data block is also called when executing the terraform plan command, so your external resource must be present before you execute the terraform plan and terraform apply commands.
One can apply a filter to search for specific projects – e.g. projects about to be deleted
data "google_projects" "my-organization-projs" {
filter = "parent.id:23232323 lifecycleState:DELETE_REQUESTED"
}
data "google_project" "deletion-candidate" {
project_id = data.google_projects.my-organization-projs.projects[0].project_id
}
Instead of hard coding IDs of resources, the terraform data block allows dynamic retrieval of resource IDs. It also allows searching / filtering based a filter inside the data block.
Need an experienced Cloud Networking or a Cloud Data Protection Expert? Anuj has successfully delivered over a dozen deployments on each of the public clouds (AWS/GCP/Azure) including several DevSecOps engagements. Set up a time with Anuj Varma.
The post Retrieving Resource IDs in Terraform – The Terraform Data Block appeared first on For all your terraform needs.
]]>The post tfvars versus variables.tf appeared first on For all your terraform needs.
]]>To me, the primary use case is that I want to prompt a user to provide the value for the input variable. In that case, I HAVE to use variables.tf.. I simply define the variable and leave it’s value blank (of course, within the resource, this variable has to be a REQUIRED value).
The post tfvars versus variables.tf appeared first on For all your terraform needs.
]]>The post VS Code does not pick up the latest terraform version appeared first on For all your terraform needs.
]]>Get-Command terraform
If this is pointing to the older terraform version, simply delete the older terraform.exe file.
ReInstall the newer version. Now, do Get-Command again to see the executable path.
VS Code should also pick up whatever version exe is returned by the Get-Command terraform.
The post VS Code does not pick up the latest terraform version appeared first on For all your terraform needs.
]]>The post VS Code Scripts Error: Cannot be loaded because running scripts is disabled on this system appeared first on For all your terraform needs.
]]>That’s it.
The post VS Code Scripts Error: Cannot be loaded because running scripts is disabled on this system appeared first on For all your terraform needs.
]]>The post Error – ‘registry.terraform.io/hashicorp/local: there is no package for registry.terraform.io/hashicorp/local cached in .terraform/providers’ appeared first on For all your terraform needs.
]]>The installed provider plugins are not consistent with the packages selected in the dependency lock file:
│ – registry.terraform.io/hashicorp/azurerm: the cached package for registry.terraform.io/hashicorp/azurerm x.y.x. (in .terraform\providers) does not match any of the checksums recorded in the dependency lock file
│ – registry.terraform.io/hashicorp/random: there is no package for registry.terraform.io/hashicorp/random 3.4.3 cached in .terraform\providers
Resolution
Step 1 – Look for this lock file (terraform.lock.hcl) in your current module. Delete it.
Step 2 – Re Run terraform init -upgrade
The post Error – ‘registry.terraform.io/hashicorp/local: there is no package for registry.terraform.io/hashicorp/local cached in .terraform/providers’ appeared first on For all your terraform needs.
]]>