1. 개요

terraform 명령어를 실행하는 버전과 tfstate 파일에 명시되어 있는 버전 차이로 인해 명령어 실행에 오류가 생기는 경우의 해결방법을 소개한다.


2. tfstate file

terraform plan/apply를 하고 나면 terraform.tfstate라는 파일이 생성된다. 이 파일은 Terraform의 설정 파일로, Terraform을 통해 생성한 리소스의 정보와 Terraform 버전, tfstate 버전 등의 정보가 들어있다.

{
  "version": 4,
  "terraform_version": "0.12.20",
  "serial": 427,
  "lineage": "dac9258e-2013-6c16-f250-3538eb4c2bdb",
  "outputs": {
    "alb_hostname": {
      "value": "ecs-alb.ap-northeast-2.elb.amazonaws.com",
      "type": "string"
    }
  },
  "resources": [
    {
      "mode": "data",
      "type": "aws_iam_role",
      "name": "ecs_task_execution_role",
      "provider": "provider.aws",
      ...

3. tfstate file의 Terraform version 변경하기

tfstate 파일에 적힌 Terraform version이 명령어를 실행한 Terraform version보다 높다면 다음과 같은 메시지가 발생하면서 명령어 실행에 실패한다.

$ terraform plan
Error refreshing state: state snapshot was created by Terraform v0.12.24, which is newer than current v0.12.21; upgrade to Terraform v0.12.24 or greater to work with this state

 

tfstate file 수정

terraform_version을 원하는 버전으로 변경한다.

{
  "version": 4,
  "terraform_version": "0.12.21", ## 0.12.24 -> 0.12.21로 변경
  ... 

 

다시 terraform 명령어를 실행 하면 다음과 같은 메시지가 뜨면서 명령어 실행에 실패한다.

$ terraform plan 
Error refreshing state: state data in S3 does not have the expected content.

This may be caused by unusually long delays in S3 processing a previous state
update.  Please wait for a minute or two and try again. If this problem
persists, and neither S3 nor DynamoDB are experiencing an outage, you may need
to manually verify the remote state and update the Digest value stored in the
DynamoDB table to the following value: <hash 값>

 

DynamoDB의 Digest 수정

AWS DynamoDB에 저장되어 있는 terraform lock state 정보와 새로 수정한 tf state 파일의 정보가 일치하지 않아서 발생한 문제인 것으로 보인다.

DyanmoDB에서 Digest 값을 위에서 주어진 <hash 값>으로 변경한 뒤 다시 terraform 명령어를 실행한다.

$ terrafom plan
...

------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.

정상적으로 실행된 것을 확인할 수 있다.