Terraform parsing error in Visual Studio Code - azure

I am new to Terraform and I'm trying to deploy a resource group using the example from the documentation found here, in Visual Studio Code. I receive a json parsing error when trying to use terraform apply or terraform plan. The commandsterraform init, terraform fmt and terraform validate all work fine. Connecting to azure using az login also works.
Information about code, versioning and setup can be seen below.
Error
╷
│ Error: building AzureRM Client: please ensure you have installed Azure CLI version 2.0.79 or newer. Error parsing json result from the Azure CLI: unmarshaling the result of Azure CLI: invalid character 'C' looking for beginning of value.
│
│ with provider["registry.terraform.io/hashicorp/azurerm"],
│ on main.tf line 10, in provider "azurerm":
│ 10: provider "azurerm" {
│
╵
Code in main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.28.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "example-resourcegroup"
location = "eu-west"
}
az --version output
azure-cli 2.41.0
core 2.41.0
telemetry 1.0.8
Dependencies:
msal 1.20.0b1
azure-mgmt-resource 21.1.0b1
terraform --version output
Terraform v1.2.5
on windows_amd64
+ provider registry.terraform.io/hashicorp/azurerm v3.28.0

I tried to reproduce the same issue in my environment and got the below results
For installing the terraform in visual studio refer this link
We have to install the developer cli use this link to download and install
I have installed the visual studio code and install the terraform
Please find the versions which I have used
I have created terraform file
vi main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.28.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example23" {
name = "example-resourcegroup23"
location = "eastus"
}
I have followed some commands to run the file
Terraform init
terraform plan
terraform apply
When I open the portal I am able to see newly created resource group
Note:
1).In order to use the azure CLI, terraform should be able to do the azure cli authentication for that we have to add the token.
2).Both terraform and Azure cli should be on same path
az account get-access-token { "accessToken": token_id", "expiresOn": <Date_with_time>, "subscription": "subscription_id", "tenant": "", "tokenType": "token_type" }***
3). you can also refer this link here for know abt the issue

Related

'terraform init' returns 404 'Resource Group not found' when it does exist

Since adding backend "azurerm" to my Terraform main.tf file it's now returning a 404 on the resource group created to maintain the state file.
I'm at a bit of a loss to explain why, the session is logged in to the correct tenant and subscription using Connect-AzAccount and Set-AzContext methods in the Az PowerShell module.
Here's my setup:
main.tf
## Terraform Configuration
terraform {
# Azure Remote State
backend "azurerm" {
resource_group_name = "abc-uat-tfstate"
storage_account_name = "abcuattfstate"
container_name = "tfstate"
key = "myapp.uat.tfstate"
}
# Provider Dependencies
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0.0"
}
}
}
## Provider Configurations
# Azure
provider "azurerm" {
subscription_id = var.subscriptionId
features {}
}
...
When I run terraform init on this main.tf file I receive the following error:
However note, I can immediately run Get-AzResourceGroup and it returns the group as I see it in Azure Portal.
Until I added the backend it was creating resources correctly so I'm thinking this is a simple configuration issue but after reviewing all the docs don't see what I've got wrong.
Ok, operator error as I suspected.
Running az login --tenant '...' and then az account set --subscrption '...' resolved the problem. terraform init now works correctly.
I should have thought about this earlier.

Error populating Client ID from the Azure CLI: No Authorization Tokens were found

When I run Terraform Plan I am getting the following error message.
Error building AzureRM Client: Error populating Client ID from the
Azure CLI: No Authorization Tokens were found
I ran 'az login' to log in and 'az account set --subscription' to set the right subscription.
I saw other postings and tried running 'az account get-access-token' and it returns the accessToken without any errors.
terraform {
# backend "azurerm" {
# key = "terraform.tfstate"
# }
required_version = ">= 0.12"
}
provider "azurerm" {
version = "= 1.31.0"
skip_provider_registration = true
}
Terraform and Az cli versions below
$ az --version azure-cli 2.38.0
$ terraform --version
Terraform v0.12.31
+ provider.azurerm v1.31.0
Error building AzureRM Client: Error populating Client ID from the Azure CLI: No Authorization Tokens were found
This Error occurs due to versions of Terraform azurerm providers and CLI. The authentication was switched over by Azure from ADAL to MSAL. If you are using recent version of Azure CLI with older version of Terraform's Azurerm this may cause an authentication failure and error.
To fix this issues use the upgraded versions both azure cli and terrform azurerm providers.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=3.0.0"
}
}
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}
Terraform azurerm version link
Similar kind of error also solved,you can check this SO thread by Ansuman Bal.

Cannot override provider configuration error in installing DC/OS via Terraform on azure

Working on setting up DC/OS on Microsoft Azure using Terraform.
I'm using the main.tf provided in the official documentation. Every time I run terraform init
I get an error:
Error: Cannot override provider configuration
│
│ on .terraform/modules/dcos/main.tf line 138, in module "dcos-infrastructure":
│ 138: azurerm = azurerm
I have authenticated via az CLI specifically via the command
az login --use-device-code
My terraform version is:
Terraform v1.1.9
on linux_amd64
How can I resolve this?
my attempts to comment out the providers still produces this error.
If you are using module in you main.tf file, You have to use `Provider Aliases in main file
To declare multiple configuration names for a provider within a module, add the configuration_aliases argument:
You can take reference of this aws and edit it for azure accordignly.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 2.7.0"
configuration_aliases = [ aws.alternate ]
}
}
}
Passing Providers Explicitly
main.tf
provider "aws" {
alias = "usw2"
region = "us-west-2"
}
# An example child module is instantiated with the alternate configuration,
# so any AWS resources it defines will use the us-west-2 region.
module "example" {
source = "./example"
providers = {
aws.alternate = aws.usw2
}
}
You can refer this document for information, How to use provider when using module in main.tf

terraform azurerm : ERROR : exit status 1 - az login required

providers.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
}
}
provider "azurerm" {
features {}
}
terraform version: Terraform v1.1.7
terraform command: terraform plan
CLI Error:
Error: building AzureRM Client: obtain subscription(XXXXXXXX-XXXX-XXXX-XXX-XXXXXXXXXXXX) from Azure CLI: parsing json result from the Azure CLI: waiting for the Azure CLI: exit status 1: ERROR: Please run 'az login' to setup account.
with provider["registry.terraform.io/hashicorp/azurerm"],
on providers.tf line 10, in provider "azurerm":
10: provider "azurerm" {
I did run az login and signed into my Azure account.
Question:
I can't find out what I'm doing wrong?
Tested in my environment getting the same error when i haven't connected with any active account.
As Suggested by cdub is correct , Tried running az logout multiple times until you see There are no active accounts, running az login, az account set -s <your-subscription-name>, then re-running terraform init and finally, terraform plan
#get the current default subscription using show
az account show --output table

Terraform apply results in Error populating Client ID from the Azure CLI

I try to apply terraform plan with terraform apply. But when I run the command I get the following error
Error: Error building AzureRM Client: Error populating Client ID from the Azure CLI:
No Authorization Tokens were found -
please ensure the Azure CLI is installed and then log-in with `az login`.
I do have the Azure CLI installed and I'm logged in with az login. When I run az login I am redirected to the landing page where I am able to log in just fine.
Also terraform init works without any problems.
Below my terraform file:
provider "azurerm" {
version = "1.38.0"
}
I also tried to provide subscription and tenant IDs but it didn't help:
provider "azurerm" {
version = "1.38.0"
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000001"
}
Error: Error building AzureRM Client: Error populating Client ID from
the Azure CLI: No Authorization Tokens were found - please ensure
the Azure CLI is installed and then log-in with az login.
This error is due to the Azure CLI version that you are using . There was a breaking change in the Azure CLI version 2.30.0 where Azure migrated the authentication from ADAL to MSAL. For which if you are using Azure CLI latest and Terraform azurerm old then , it will fail in authentication which will result in the error.
To Fix the problem you will have to use the latest Azure CLI version i.e. 2.32.0 and also at the same time try to use the terraform latest azurerm Provider i.e. 2.92.0.
To Upgrade CLI version , You can run az upgrade command and in terraform you can use the below :
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.92.0"
}
}
}
provider "azurerm" {
# Configuration options
}
You can refer these Similar Github Issues are well : Issue 1 and Issue 2

Resources