Unable to Execute Plan command and terraform provider file is being not created in PC after terraform init - terraform

I'm using terraform version v0.12.14. whenever I use terraform init I'm unable to see the terraform provider in my folder(hidden files are enabled to visible). also the plan command always fails with the error " no changes, infrastructure is up-to-date". kindly help me since I'm getting these errors I am not able to create the resource group in azure.

Related

What is the behaviour Terraform Plan?

Learning Terraform, and in one of the tutorials for terraform with azure a requirement was to log in with the az client. Now my understanding is that this was to create a Service Princlple.
I was trying this with Github actions and my assumption was that the properties obtained for the Service Principle. When I tried running terraform plan everything worked out fine.
However, when I tried to do terraform apply it failed until I explicitly did an az login step in the github workflow job.
What am I missing here? Does terraform plan only compare the new configuration file against the state file, not the actual account? Or does it verify the state against the resource-group/subscription in Azure?
I was a little confused with the documentation on terraform plan

how to delete a terraform state file when the azure resources are removed using terraform?

We are building a temp review app in terraform. Currently when review app is finished with the resources are destroyed with terraform using terraform apply -destroy. What i need to do is also remove the terraform state file for this infrastructure from the azure container. Could I use terraform -destroy to also remove the state file and how can i do this?
One of the workaround you can follow,
When we are using terraform destroy that time our resource detailed also removed from terraform.tfstate by removing from portal itself.
So to remove any particular resource from .tfstate you can try something like below;
First would suggest you to after destroy the file list the state file you have then remove those.
This below command is used to get the available instances which are in state file.
terraform state list
After listing those try with below which will remove from .tfstate file as mentioned by #Ansuman Bal i have also tried and it works fine .
terraform state rm "azurerm_resource_group.example"
OUTPUT DETAILS FOR REFERENCE:-
NOTE:- This aforementioned cmdlts will remove the instance/resources from .tfstate file only not from portal. Only terraform destroy can do that.
For more information please refer this SO THREAD| Terraform - Removing a resource from local state file.

How to throw a warning or error in terraform plan?

I am currently writing a terraform custom provider for my internship project
The main aim of the project is to provision an environment (consisting of several servers) on a private cloud platform.
I created a custom provider using the CRUD operations.
Let's say I want to delete the whole resource by removing the whole resource block in main.tf. I want to do a terraform plan to see if the deletion is valid. It will do a read of the actual environment and see if the serverState of the server is active. I want it to throw a warning/error in terraform plan is serverState is not empty.
So the main issue right now is that the terraform plan only compares the difference between the configuration in main.tf and the actual tf statefile. So the error checking code in the delete function is not executed. It is only executed when terraform apply is used.
Is there any way to throw the error in terraform plan to warn the user before they use the terraform apply command

Terraform unable to find azurerm backend storage during init

I've created an Azure Storage Account to be used as the backend state store for Terraform, and I was able to write to this from an Azure DevOps pipeline running Terraform commands. I can see the container in the Storage Account and confirm that it has the state content from the pipeline execution in it with that same key. However, when I try to run Terraform "manually" using the same backend store, I'm getting an error that it cannot find that container:
$ terraform init -backend-config="storage_account_name=<redacted>" -backend-config="container_name=auto-api-tfstate" -backend-config="access_key=<redacted>" -backend-config="key=dev-internal2/dev-internal2.tfstate:us"
Initializing modules...
Initializing the backend...
Backend configuration changed!
Terraform has detected that the configuration specified for the backend
has changed. Terraform will now check for existing state in the backends.
Error: Error inspecting states in the "azurerm" backend:
storage: service returned error: StatusCode=404, ErrorCode=ContainerNotFound, ErrorMessage=The specified container does not exist.
RequestId:89a9b361-a01e-00b1-0fb4-ba5d51000000
Time:2021-10-06T13:18:41.2460433Z, RequestInitiated=Wed, 06 Oct 2021 13:18:40 GMT, RequestId=89a9b361-a01e-00b1-0fb4-ba5d51000000, API Version=2016-05-31, QueryParameterName=, QueryParameterValue=
Prior to changing backends, Terraform inspects the source and destination
states to determine what kind of migration steps need to be taken, if any.
Terraform failed to load the states. The data in both the source and the
destination remain unmodified. Please resolve the above error and try again.
My main.tf file has simply:
terraform {
backend "azurerm" {}
}
As mentioned, this same terraform init command worked when invoked in a Bash script in an ADO pipeline, so not sure what may be the issue. Any suggestions for debugging this appreciated.
Uncovered the issue ... there was state information in the .terraform folder which conflicted with the new backend. Once I cleared that out, the "terraform init" command worked as expected.

How to run "terraform state mv" commands in the Terraform Enterprise/Cloud?

I'm in the process of a Terraform code refactoring in which some resources are moved to a module A and a module B into a submodule of A and I'm now getting this error in Terraform Enterprise:
Error: Provider configuration not present
To work with
module.account-baseline.module.iam-policy.aws_iam_role.ops_role
its original provider configuration at module.account-baseline.provider.aws is
required, but it has been removed. This occurs when a provider configuration
is removed while objects created by that provider still exist in the state.
Re-add the provider configuration to destroy
module.account-baseline.module.iam-policy.aws_iam_role.ops_role,
after which you can remove the provider configuration again.
I've tried in my playground account using a local Terraform state to run "terraform state mv" commands moving the module into a sub-module and it works, but I don't know how to apply this state change to Terraform Enterprise.
Any help would be more than welcome, thanks in advance!

Resources