For all your terraform needs https://terraformarchitect.com/ Automation cannot be an afterthought (TM) Tue, 22 Aug 2023 13:31:40 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.6 Resource Shutdown via terraform https://terraformarchitect.com/quick-recipes-terraform/resource-shutdown-via-terraform/?utm_source=rss&utm_medium=rss&utm_campaign=resource-shutdown-via-terraform https://terraformarchitect.com/quick-recipes-terraform/resource-shutdown-via-terraform/#respond Tue, 22 Aug 2023 13:31:40 +0000 https://terraformarchitect.com/?p=372 Terraform is often used to destroy environments – which keeps the state file consistent, when the next create event needs to occur. However, sometimes, instead of destroying resources, all you […]

The post Resource Shutdown via terraform appeared first on For all your terraform needs.

]]>
Terraform is often used to destroy environments – which keeps the state file consistent, when the next create event needs to occur.

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

Shutting down an EC2 instance on aws

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.

]]>
https://terraformarchitect.com/quick-recipes-terraform/resource-shutdown-via-terraform/feed/ 0
Terraform for IAM resource creation https://terraformarchitect.com/gcp/terraform-for-iam-resource-creation/?utm_source=rss&utm_medium=rss&utm_campaign=terraform-for-iam-resource-creation https://terraformarchitect.com/gcp/terraform-for-iam-resource-creation/#respond Mon, 21 Aug 2023 14:06:43 +0000 https://terraformarchitect.com/?p=364 Overview 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 […]

The post Terraform for IAM resource creation appeared first on For all your terraform needs.

]]>
Overview

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.

Time taken to apply terraform for IAM

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?

State drift – Console based roles and assignments

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.

Summary

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.

]]>
https://terraformarchitect.com/gcp/terraform-for-iam-resource-creation/feed/ 0
Error: retrieving `contact` for KeyVault: keyvault.BaseClient#GetCertificateContacts: Failure sending request: StatusCode=0 — Original Error: context deadline exceeded https://terraformarchitect.com/known-issues-terraform/error-retrieving-contact-for-keyvault-keyvault-baseclientgetcertificatecontacts-failure-sending-request-statuscode0-original-error-context-deadline-exceeded/?utm_source=rss&utm_medium=rss&utm_campaign=error-retrieving-contact-for-keyvault-keyvault-baseclientgetcertificatecontacts-failure-sending-request-statuscode0-original-error-context-deadline-exceeded https://terraformarchitect.com/known-issues-terraform/error-retrieving-contact-for-keyvault-keyvault-baseclientgetcertificatecontacts-failure-sending-request-statuscode0-original-error-context-deadline-exceeded/#comments Mon, 13 Feb 2023 04:32:15 +0000 https://terraformarchitect.com/?p=248 When creating a key vault in Azure using terraform,  you may encounter this error Error: retrieving `contact` for KeyVault: keyvault.BaseClient#GetCertificateContacts data "azurerm_client_config" "current" {} resource “azurerm_key_vault” “my_key_vault” {  name   […]

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.

]]>
When creating a key vault in Azure using terraform,  you may encounter this error
Error: retrieving `contact` for KeyVault: keyvault.BaseClient#GetCertificateContacts

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

terraform {
  required_providers {
    azurerm = {
      source  = “hashicorp/azurerm”
      version = “=3.3.0”
    }
  }
}




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 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.

]]>
https://terraformarchitect.com/known-issues-terraform/error-retrieving-contact-for-keyvault-keyvault-baseclientgetcertificatecontacts-failure-sending-request-statuscode0-original-error-context-deadline-exceeded/feed/ 2
Retrieving Resource IDs in Terraform – The Terraform Data Block https://terraformarchitect.com/terraform-basics/the-terraform-data-block/?utm_source=rss&utm_medium=rss&utm_campaign=the-terraform-data-block https://terraformarchitect.com/terraform-basics/the-terraform-data-block/#respond Mon, 13 Feb 2023 04:23:30 +0000 https://terraformarchitect.com/?p=327 Also read – Terraform external data querying The Terraform Data Block – Retrieving a Project Id for a GCP project data “google_project” “project” { } output "project_number" { value = […]

The post Retrieving Resource IDs in Terraform – The Terraform Data Block appeared first on For all your terraform needs.

]]>
Also read – Terraform external data querying

The Terraform Data Block – Retrieving a Project Id for a GCP project

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.

Searching for Projects using the terraform data block

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
}

Summary

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.

]]>
https://terraformarchitect.com/terraform-basics/the-terraform-data-block/feed/ 0
tfvars versus variables.tf https://terraformarchitect.com/terraform-basics/tfvars-versus-variables-tf/?utm_source=rss&utm_medium=rss&utm_campaign=tfvars-versus-variables-tf https://terraformarchitect.com/terraform-basics/tfvars-versus-variables-tf/#respond Tue, 31 Jan 2023 02:24:13 +0000 https://terraformarchitect.com/?p=322 Prompting a user for Input in Terraform To me, the primary use case is that I want to prompt a user to provide the value for the input variable. In […]

The post tfvars versus variables.tf appeared first on For all your terraform needs.

]]>
Prompting a user for Input in Terraform

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.

]]>
https://terraformarchitect.com/terraform-basics/tfvars-versus-variables-tf/feed/ 0
VS Code does not pick up the latest terraform version https://terraformarchitect.com/known-issues-terraform/vs-code-does-not-pick-up-the-latest-terraform-version/?utm_source=rss&utm_medium=rss&utm_campaign=vs-code-does-not-pick-up-the-latest-terraform-version https://terraformarchitect.com/known-issues-terraform/vs-code-does-not-pick-up-the-latest-terraform-version/#respond Wed, 09 Nov 2022 13:26:30 +0000 https://terraformarchitect.com/?p=299 From a powershell prompt, get the current executable path for terraform using: Get-Command terraform If this is pointing to the older terraform version, simply delete the older terraform.exe file. ReInstall […]

The post VS Code does not pick up the latest terraform version appeared first on For all your terraform needs.

]]>
From a powershell prompt, get the current executable path for terraform using:

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.

]]>
https://terraformarchitect.com/known-issues-terraform/vs-code-does-not-pick-up-the-latest-terraform-version/feed/ 0
VS Code Scripts Error: Cannot be loaded because running scripts is disabled on this system https://terraformarchitect.com/known-issues-terraform/vs-code-scripts-error-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system/?utm_source=rss&utm_medium=rss&utm_campaign=vs-code-scripts-error-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system https://terraformarchitect.com/known-issues-terraform/vs-code-scripts-error-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system/#respond Thu, 03 Nov 2022 14:59:31 +0000 https://terraformarchitect.com/?p=293 VS Code Terminal displays this error Cannot be loaded because running scripts is disabled on this system  If you want to continue using powershell as the terminal, you will need […]

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.

]]>
VS Code Terminal displays this error
Cannot be loaded because running scripts is disabled on this system 
If you want to continue using powershell as the terminal, you will need to tweak settings.json (ctrl shft p and search for settings). Add the following json code to this file.
{
    “terminal.integrated.profiles.windows”: {
        “PowerShell”: {
          “source”: “PowerShell”,
          “icon”: “terminal-powershell”,
          “args”: [“-ExecutionPolicy”, “Bypass”]
        }
      },
      “terminal.integrated.defaultProfile.windows”: “PowerShell”,
}
If you can make do with the windows cmd prompt,  change the VS Code terminal from powerShell to cmd (cmd already has the privileges for running scripts).
  1. Terminal -> New Terminal –> “Default Shell” –> Windows

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.

]]>
https://terraformarchitect.com/known-issues-terraform/vs-code-scripts-error-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system/feed/ 0
Error – ‘registry.terraform.io/hashicorp/local: there is no package for registry.terraform.io/hashicorp/local cached in .terraform/providers’ https://terraformarchitect.com/gcp/terraform-apply-fails/?utm_source=rss&utm_medium=rss&utm_campaign=terraform-apply-fails https://terraformarchitect.com/gcp/terraform-apply-fails/#respond Wed, 19 Oct 2022 13:11:20 +0000 https://terraformarchitect.com/?p=276 Terraform init or terraform init -upgrade or terraform apply fails with this error message: The installed provider plugins are not consistent with the packages selected in the dependency lock file: […]

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.

]]>
Terraform init or terraform init -upgrade or terraform apply fails with this error message:

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

Terraform apply fails

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.

]]>
https://terraformarchitect.com/gcp/terraform-apply-fails/feed/ 0
Using -out with terraform plan – terraform plan -out https://terraformarchitect.com/terraform-basics/using-out-with-terraform-plan-terraform-plan-out/?utm_source=rss&utm_medium=rss&utm_campaign=using-out-with-terraform-plan-terraform-plan-out https://terraformarchitect.com/terraform-basics/using-out-with-terraform-plan-terraform-plan-out/#respond Sun, 18 Sep 2022 12:12:17 +0000 https://terraformarchitect.com/?p=260 This is a short post – always use the -out option. This saves the current plan – and will execute it exactly as saved (once you pass in the saved […]

The post Using -out with terraform plan – terraform plan -out appeared first on For all your terraform needs.

]]>
This is a short post – always use the -out option. This saves the current plan – and will execute it exactly as saved (once you pass in the saved plan to terraform apply).

 





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 Using -out with terraform plan – terraform plan -out appeared first on For all your terraform needs.

]]>
https://terraformarchitect.com/terraform-basics/using-out-with-terraform-plan-terraform-plan-out/feed/ 0
Data Block External Data – Querying External Resources in Terraform https://terraformarchitect.com/terraform-basics/data-block-external-data/?utm_source=rss&utm_medium=rss&utm_campaign=data-block-external-data https://terraformarchitect.com/terraform-basics/data-block-external-data/#comments Wed, 14 Sep 2022 16:38:32 +0000 https://terraformarchitect.com/?p=258 Retrieving External data Use either the data block or the terraform_remote_state block to retrieve external data. However, there are scenarios where the data block does not exist in the provider or terraform_remote_state cannot be used, such as […]

The post Data Block External Data – Querying External Resources in Terraform appeared first on For all your terraform needs.

]]>
Retrieving External data

Use either the data block or the terraform_remote_state block to retrieve external data.

However, there are scenarios where the data block does not exist in the provider or terraform_remote_state cannot be used, such as when we need to process with an external API or need to use a local tool and process its output.

# Read the JSON payload from stdin
$jsonpayload = [Console]::In.ReadLine()

# Convert JSON to a string
$json = ConvertFrom-Json $jsonpayload
$environment = $json.environment

if($environment -eq "Production"){
$location="westeurope"
}else{
$location="westus"
}

# Write output to stdout
Write-Output "{ ""location"" : ""$location""}"

Retrieving External data

data "external" "getlocation" {
program = ["Powershell.exe", "./GetLocation.ps1"]
query = {
environment = "${var.environment_name}"
  }
}

Summary

External data sources are extremely useful in terraform – the data ‘external’ is defined for just this use case.





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 Data Block External Data – Querying External Resources in Terraform appeared first on For all your terraform needs.

]]>
https://terraformarchitect.com/terraform-basics/data-block-external-data/feed/ 1