[Terraform] module 삭제시 state 오류 발생

terraform cloud에서 발생한 문제이나, 다른 환경에서도 나는지는 잘 모르겠다.

아무튼 서브모듈로 import한 리소스를 삭제할 때 오류가 발생하는 경우가 있다.

이렇게 지웠는데

그냥 제거를 원할 뿐인데도 다음과 같은 에러가 난다.

Error: Provider configuration not present
To work with module.beautify.aws_iam_role_policy_attachment.lambda_vpc_access (orphan) its original provider configuration at module.beautify.provider["registry.terraform.io/hashicorp/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.beautify.aws_iam_role_policy_attachment.lambda_vpc_access (orphan), after which you can remove the provider configuration again.

provider가 없다는 것이다.


이건 기본적으로, 서브모듈 내에서 자체적으로 provider를 선언할 때 발생할 수 있는 문제다.

provider는 전역적으로 영향을 주는 리소스고, 원래는 이렇게 서브모듈마다 넣거나 삭제하라고 만든 것이 아니기 때문이다. 그래서 삭제 순서가 간접 참조 관계에 있어서 꼬임이 발생한 것이다.

사실 좀 알아서 해줘도 좋을텐데, 시스템이 그렇게 영리하지는 않은 셈이다.

아무튼 이런 사용 자체가 안티패턴이니, 가장 좋은 것은 루트에 단 하나의 provider만 쓰도록 하는 것이다.




임시조치

그럼 이 상태를 어떻게 해결할 수 있을까?
일단 import 삭제를 롤백해야 한다.
그리고 해당 서브모듈에서 provider를 제외한 모든 리소스를 제거한다.

이렇게 제거한 다음에서야 provider를(혹은 import를) 제거할 수 있는 것이다.


참조
https://support.hashicorp.com/hc/en-us/articles/1500000332721-Error-Provider-configuration-not-present
https://stackoverflow.com/questions/79162121/terraform-error-provider-configuration-not-present
https://developer.hashicorp.com/terraform/language/modules/develop/providers