amazon-web-servicesterraformaws-secrets-manager

Terraform AWS Provider: SecretsManager can't apply because version was deleted


We have an AWS SecretsManager Secret that was created once. That secret will be updated by an external job every hour. I have the problem that sometimes the terraform plan/apply fails with the following message:

AWS Provider 2.48

    Error: Error refreshing state: 1 error occurred:

        * module.xxx.xxx: 1 error occurred:

        * module.xxx.aws_secretsmanager_secret_version.xxx: 
    aws_secretsmanager_secret_version.xxx: error reading Secrets Manager Secret Version: InvalidRequestException: You can't perform this operation on secret version 68AEABC3-34BE-4723-8BF5-469A44F9B1D9 because it was deleted.

We've tried two solutions: 1) Force delete the whole secret via aws cli, but this has the side effect that one of our dependend resources will also be recreated (ecs template definition depends on that secret). This works, but we do not want the side effect of recreating the ecs thing. 2) Manually edit the backend .tfstate file and set the current AWS secret version. Then run the plan again.

Both solution seem to be hacky in a way. What is the best way to solve this issue ?


Solution

  • You can use terraform import to reconcile the state difference before you run a plan or apply.

    In your case, this would look like:

    terraform import module.xxx.aws_secretsmanager_secret_version.xxx arn:aws:secretsmanager:some_region:some_account_id:secret:example-123456|xxxxx-xxxxxxx-xxxxxxx-xxxxx