Terraform plan getting Error: Failed to decode resource from state - azure

Terraform initialize is working fine but when I do terraform plan getting below error.
Error: Failed to decode resource from state
│
│ Error decoding "azurerm_mssql_database.db" from previous state: unsupported attribute "extended_auditing_policy"
If I comment this particular resource then we start getting error for other resource.
Can some one please help me ?
Error: Failed to decode resource from state
│
│ Error decoding "azurerm_mssql_database.db" from previous state: unsupported attribute "extended_auditing_policy"

I tried in my environment and got below results:
Initially I tried with extended audit policy with new terraform provider version and got the same error:
extended_auditing_policy {
storage_endpoint = module.storageaccount.storage_account.self.primary_blob_endpoint
storage_account_access_key = module.storageaccount.storage_account.self.primary_access_key
storage_account_access_key_is_secondary = false
retention_in_days = 30
}
This problem occurs when attempting to import data using a provider version that is older than the one that was used to create the current state. The earlier provider version won't be able to decode an unknown attribute while loading the state file during the import if the attribute was added in the newer version of the provider.
I tried with new azurerm_mssql_server_extended_auditing_policy resource to solve this problem.
Terraform.tf
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "vs" {
name = "<rg name>"
location = "West Europe"
}
resource "azurerm_mssql_server" "ex" {
name = "demosqlserver3261"
resource_group_name = azurerm_resource_group.vs.name
location = azurerm_resource_group.vs.location
version = "12.0"
administrator_login = "missadministrator"
administrator_login_password = "AdminPassword123!"
}
resource "azurerm_mssql_database" "ext" {
name = "demodb3261"
server_id = azurerm_mssql_server.ex.id
}
resource "azurerm_storage_account" "vst" {
name = "venkat678"
resource_group_name = azurerm_resource_group.vs.name
location = azurerm_resource_group.vs.location
account_tier = "Standard"
account_replication_type = "GRS"
}
resource "azurerm_mssql_database_extended_auditing_policy" "example" {
database_id = azurerm_mssql_database.ext.id
storage_endpoint = azurerm_storage_account.vst.primary_blob_endpoint
storage_account_access_key = azurerm_storage_account.vst.primary_access_key
storage_account_access_key_is_secondary = false
retention_in_days = 6
}
Console:
Portal:
Reference:
Import fails with "Error: Invalid resource instance data in state" – HashiCorp Help Center

Related

Argument "recovery_vault_name" not found while creating the recovery service vault using Terraform

I am using Terraform Script to create a Recovery Service Vault and need to take the VM backup into that recovery service vault. Below is the terraform script and steps I performed:
Create Recovery Service Vault
Define backup policy
Azure VM protection backup
Terraform Version >= 0.14
Azure rm version= ~>2.0
Main.tf
resource "azurerm_recovery_services_vault" "recovery_vault" {
name = var.recovery_vault_name
location = var.location
resource_group_name = var.resource_group_name
}
resource "azurerm_backup_policy_vm" "ss_vm_backup_policy" {
name = "tfex-recovery-vault-policy"
resource_group_name = var.resource_group_name
recovery_vault_name = azurerm_recovery_services_vault.recovery_vault.name
policy_type = "V1"
backup {
frequency = "Monthly"
time = "23:00"
weekdays = ['Sunday']
}
instant_restore_retention_days = 5
retention_weekly {
count= 12
weekdays = ['Sunday']
}
}
resource "azurerm_backup_protected_vm" "ss_vm_protection_backup" {
resource_group_name = var.resource_group_name
recovery_vault_name = azurerm_recovery_services_vault.recovery_vault.name
source_vm_id = azurerm_windows_virtual_machine.windows_vm[count.index].id
backup_policy_id = azurerm_backup_policy_vm.ss_vm_backup_policy.id
}
Variable.tf
variable resource_group_name {
default = "ss"
}
variable recovery_vault_name {
default = "yy"
}
Referred the above Main.tf in my application specific main.tf file as below:
module "azure_windows_vm" {
source = "git::https://xx.yy.com/ABCD/_git/NBGF/TGFT?ref=master"
vm_name = local.vm_name
location = local.location
resource_group_name = local.resource_group_name
admin_password = "xyz"
num_vms = 2
vnet_name = var.vm_subnet_id
tags=local.tags
}
When I execute the above Terraform script in DevOps pipeline, I am getting below error from line where module "azure_windows_vm" starts
Error: Missing required argument The argument "recovery_vault_name" is
required, but no definition was found
I tried different things to fix this error, but somehow it is not working. Can someone please guide what I am missing here?
Thank You!

Why I can't create azurerm_app_service connection?

Azurerm provider version
3.35.0
Target resource
azurerm_app_service_connection
Terraform file
write brief
module "app_service" {
source = "../../modules/app_service"
name = var.name
}
resource "azurerm_app_service_connection" "serviceconnector" {
name = "serviceconnector"
app_service_id = module.app_service.id
target_resource_id = data.azurerm_postgresql_flexible_server.db.id
client_type = "django"
authentication {
type = "secret"
name = var.db_uname
secret = data.azurerm_key_vault_secret.secret.value
}
}
data "azurerm_postgresql_flexible_server" "db" {
name = "postgre"
resource_group_name = "rg"
}
Output Error message
Error: creating Scoped Linker (Resource Uri: "/subscriptions/<id>/resourceGroups/app/providers/Microsoft.Web/sites/app_service"
│ Linker Name: "serviceconnector"): performing LinkerCreateOrUpdate: servicelinker.ServiceLinkerClient#LinkerCreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: Code="TargetTypeNotSupported" Message="Target resource type MICROSOFT.DBFORPOSTGRESQL/FLEXIBLESERVERS is not supported."
│
│ with azurerm_app_service_connection.serviceconnector,
│ on app_service.tf line 10, in resource "azurerm_app_service_connection" "serviceconnector":
│ 10: resource "azurerm_app_service_connection" "serviceconnector" {
│
│ creating Scoped Linker (Resource Uri:
│ "/subscriptions/<id>/resourceGroups/app/providers/Microsoft.Web/sites/app_service"
│ Linker Name: "serviceconnector"): performing LinkerCreateOrUpdate:
│ servicelinker.ServiceLinkerClient#LinkerCreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error:
│ Code="TargetTypeNotSupported" Message="Target resource type MICROSOFT.DBFORPOSTGRESQL/FLEXIBLESERVERS is not
│ supported."
I want to ask
I updated azurerm provider to 3.35.0 by see this info.azurerm_app_service_connection: Expected type object but found type string
Why return "TargetTypeNotSupported" this message.
It seems to supported as long as I saw.
azurerm_app_service_connection
It's my fault? or bug?
I have tested this in my local and landed up with error targettypenotsupported.
If we want to create a service connection to postgressql flexible server db with app service then db should be declared using resource block only since as per this terraform documentation we dont have any definition to use datablock to create a connection with existing postgres sql flexible server db as shown in this image
I tried the same github terraform which you added in my environment, with the authentication type as "Secret" and received the same error as shown:
As mentioned here, there appears to be a problem with the "secret authentication type" from the Terraform provider API. As a result, I updated it as follows and it worked for me successfully:
authentication {
type = "systemAssignedIdentity"
}
main.tf:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.35.0"
}
}
}
provider "azurerm" {
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}
resource "azurerm_resource_group" "main" {
name = "xxxxRG"
location = "xxxxx"
}
resource "azurerm_postgresql_server" "main" {
name = "xxxxxserver"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
administrator_login = "xxxxxuser"
administrator_login_password = "<Password of server>"
backup_retention_days = 7 // As per your requirements
sku_name = "GP_Gen5_2"
version = "11"
storage_mb = 5120
public_network_access_enabled = false
ssl_enforcement_enabled = true
ssl_minimal_tls_version_enforced = "TLS1_2"
}
resource "azurerm_postgresql_database" "main" {
name = "xxxxdb"
server_name = azurerm_postgresql_server.main.name
resource_group_name = azurerm_postgresql_server.main.resource_group_name
charset = "utf8"
collation = "und-x-icu"
}
resource "azurerm_service_plan" "main" {
name = "xxxxxserviceplan"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
sku_name = "P1v2"
os_type = "Linux"
}
resource "azurerm_linux_web_app" "main" {
name = "xxxxxwebapp"
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
service_plan_id = azurerm_service_plan.main.id
site_config {}
}
resource "azurerm_app_service_connection" "main" {
name = "xxxxxserviceconnector"
app_service_id = azurerm_linux_web_app.main.id
target_resource_id = azurerm_postgresql_database.main.id
client_type = "springBoot"
authentication {
type = "systemAssignedIdentity"
}
}
Output:-
Executed terraform init
Executed terraform plan:
Executed terraform apply:
Alternatively, If the requirement is only with the secret authentication type in your environment then you can create an app service connection by calling API (JSON request).

AzureRM Automation DSC Configuration

I'm trying to configure Azure DSC Configuration, but I am running into two issues.
I continue to get this error message
Error = 'invalid character 'c' looking for beginning of value' JSON = 'configuration cdavdtest {}'*
*2. No matter what I do to the resource azurerm_automation_dsc_configuration , it throws this command which is a reference to my last terraform plan / apply that failed. Changing the configuration does nothing, and the old error continues. I appreciate any help. See the cdavdtest in bold compared to the resource also in bold below. Also the name doesn't update it still says dsc_config even though I updated it to dsc_configa.
Error: making Read request on AzureRM Automation Dsc Configuration content "cdavdtest": automation.DscConfigurationClient#GetContent: Failure responding to request: StatusCode=200 -- Original Error: Error occurred unmarshalling
JSON - Error = 'invalid character 'c' looking for beginning of value' JSON = 'configuration cdavdtest {}'
│
│ with azurerm_automation_dsc_configuration.dsc_config,
│ on automationaccount.tf line 17, in resource "azurerm_automation_dsc_configuration" "dsc_config":
│ 17: resource azurerm_automation_dsc_configuration dsc_config {
resource azurerm_automation_account automation_account {
name = "${var.avd.name}-automationaccount"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku_name = "Basic"
}
output "end_point" {
value = azurerm_automation_account.automation_account.dsc_server_endpoint
}
output registration_key {
value = azurerm_automation_account.automation_account.dsc_primary_access_key
}
resource azurerm_automation_dsc_configuration dsc_configa {
name = "**test**"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
automation_account_name = azurerm_automation_account.automation_account.name
description = "Configuration node for Azure Virtual Desktop"
content_embedded = "Configuration **test** {}"
log_verbose = true
}
I have tried commenting out the code and I still get the error. I've tried updating the name. I've tried using the <<BODY and writing out the configuration but this still persists.
Tested in my Environment was getting the same error. The error is due to azurerm_automation_dsc_configuration is broken since provider version 2.96.0
I was using the lasted terraform provider version ie 3.0.1
Solution : Would Suggest you to please use the provider version between version = ">=2.10,<=2.30"
Terraform Code
main.tf file
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">=2.10,<=2.30"
}
}
}
provider "azurerm" {
features{}
}
data "azurerm_resource_group" "example" {
name = "XXXXXXxXXX"
}
resource "azurerm_automation_account" "example" {
name = "account1"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
sku_name = "Basic"
}
output "end_point" {
value = azurerm_automation_account.example.dsc_server_endpoint
}
output "registration_key" {
value = azurerm_automation_account.example.dsc_primary_access_key
}
resource "azurerm_automation_dsc_configuration" "example" {
name = "test"
resource_group_name = data.azurerm_resource_group.example.name
automation_account_name = azurerm_automation_account.example.name
location = data.azurerm_resource_group.example.location
content_embedded = "configuration test {}"
log_verbose = true
}
OutPut--

Terraform source error - Error: Failed to query available provider packages

I am trying to deploy a new infrastructure using terraform (for the first time) and I am getting the following error. I've tried everything but nothing seems to fix the issue.
Looks like it is asking for a provider hashicorp/azure ?
Can anyone help please??
Initializing provider plugins...
- Finding latest version of hashicorp/azure...
- Finding hashicorp/azurerm versions matching "2.98.0"...
- Installing hashicorp/azurerm v2.98.0...
- Installed hashicorp/azurerm v2.98.0 (signed by HashiCorp)
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/azure: provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/azure
│
│ Did you intend to use terraform-providers/azure? If so, you must specify that source address in each module which requires that provider. To see which modules are currently depending on hashicorp/azure, run the following command:
│ terraform providers
╵
lucas#Azure:~$ terraform providers
Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/azurerm] 2.98.0
└── provider[registry.terraform.io/hashicorp/azure]
The code that I am using to create the infrastructure is the below:
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.98.0"
}
}
}
provider "azurerm" {
features {}
subscription_id = "910910be-a61e-4e1f-a72a-7e43456c0836"
}
# Create a resource group
resource "azurerm_resource_group" "rg" {
name = "default"
location = "West Europe"
}
# Create a virtual network
resource "azurerm_virtual_network" "vpc" {
name = "default-network"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
address_space = ["10.0.0.0/16"]
}
# Create frontend subnet
resource "azurerm_subnet" "subnet_frontend" {
name = "internal"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vpc.name
address_prefixes = ["10.0.1.0/24"]
}
# Create backend subnet
resource "azurerm_subnet" "subnet_backend" {
name = "internal"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vpc.name
address_prefixes = ["10.0.2.0/24"]
}
# Create frontend network interface
resource "azurerm_network_interface" "frontend_nic" {
name = "frontend_nic"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet_frontend.id
private_ip_address_allocation = "Dynamic"
}
}
# Create backend network interface
resource "azurerm_network_interface" "backend_nic" {
name = "backend_nic"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.subnet_backend.id
private_ip_address_allocation = "Dynamic"
}
}
# Create frontend VM based on module
resource "azure_instance" "frontend" {
source = "./vm"
name = "frontend"
rg = module.azurerm_resource_group.rg.name
location = module.azurerm_resource_group.rg.location
nic = module.azurerm_network_interface.frontend_nic
}
# Create backend VM based on module
resource "azure_instance" "backend" {
source = "./vm"
name = "backend"
rg = module.azurerm_resource_group.rg.name
location = module.azurerm_resource_group.rg.location
nic = module.azurerm_network_interface.backend_nic
}
My terraform version is: Terraform v1.1.5 and I am using on Azure CLI via bash.
Any idea of what is causing this issue and how to fix it?
Thanks!
This often happens when one accidentally specifies "hashicorp/azure" or "hashicorp/azurem" instead of "hashicorp/azurerm" in the required_providers block. Did you check the "vm" module referenced in the "azure_instance" module calls? There might be an erroneous "hashicorp/azure" specified there.

AKS Cluster | Failed to query available provider packages | Right version of hashicorp

I'm currently building my terraform plan and it seems that I'm running into issues as soon as I run the following command:
terraform init
The current main.tf contains this:
terraform {
backend "azurerm"{
resource_group_name = "test"
storage_account_name = "testaccount"
container_name = "testc"
key = "testc.state"
}
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.46.0"
}
}
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}
data "azurerm_key_vault" "keyVaultClientID" {
name = "AKSClientID"
key = var.keyvaultID
}
data "azure_key_vault_secret" "keyVaultClientSecret" {
name = "AKSClientSecret"
key_vault_id = var.keyvaultID
}
resource "azurerm_kubernetes_cluster" "test_cluster" {
name = var.name
location = var.location
resource_group_name = var.resourceGroup
dns_prefix = ""
default_node_pool {
name = "default"
node_count = 1
vm_size = "Standard_D2_v2"
}
service_principal {
client_id = data.azurerm_key_vault_secret.keyVaultClientID.value
client_secret = data.azurerm_key_vault_secret.keyVaultClientSecret.value
}
tags = {
"Environment" = "Development"
}
}
The error message that I get is the following:
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/azure: provider
│ registry registry.terraform.io does not have a provider named
│ registry.terraform.io/hashicorp/azure
I'm looking at the documentation, and I'm changing the version, but I'm not getting any luck. Does anyone knows what else I can do or what should I change on my main.tf?
The solve this issue, you will have to add the following inside of the main terraform plan:
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.75.0"
}
If you add it, the issue will never appear again. Also, you might have to run the upgrade command to make sure terraform will be able to handle the new version.

Resources