테라폼(Terraform) Error acquiring the state lock 에러 해결

테라폼(Terraform)에서 Error acquiring the state lock 에러를 해결 하는 방법에 대해서 정리해 봤습니다.
2022.06.23

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

안녕하세요 클래스메소드 김재욱(Kim Jaewook) 입니다. 이번에는 테라폼(Terraform)에서 Error acquiring the state lock 에러를 해결 하는 방법에 대해서 정리해 봤습니다.

에러 발생


Error: Error acquiring the state lock
 
Error message: resource temporarily unavailable
Lock Info:
   ID:        
   Path:      terraform.tfstate
   Operation: OperationTypeApply
   Who:       kim
   Version:   1.2.0
   Created:   2022-06-23 02:14:03.017697 +0000 UTC
   Info:      
 
 
 Terraform acquires a state lock to protect the state from being written
 by multiple users at the same time. Please resolve the issue above and try
 again. For most commands, you can disable locking with the "-lock=false"
 flag, but this is not recommended.

Terraform에서 plan, apply를 할 시 다음과 같이 lock에러가 발생하는 경우가 있습니다.

에러 내용을 살펴보면, 여러 사람이 Terraform을 사용하는 것을 방지하기 위해 현재 Terraform에 lock을 걸었다는 내용입니다.

문제 해결

실제로 협업을 하고 있을 시, 다른 유저의 작업이 끝날 때 까지 기다리면 되겠지만 로컬 환경에서 사용할 경우 혼자 Terraform 환경을 만지고 있기 때문에 기다려도 lock 상태가 해제되지 않습니다.

ps aux | grep terraform

현재 실행중인 terraform 프로세스를 확인합니다.


kim.jaewook      80471   0.0  0.0 408121728   1392 s001  S+   11:14AM   0:00.00 grep terraform
kim.jaewook      79291   0.0  0.6 408781760 101984 s001  T    11:14AM   0:01.17 terraform apply
kim.jaewook      34866   0.0  0.0 409082592   6656 s001  T    11:39AM   0:00.04 terraform plan

프로세스 목록을 살펴보면 apply, plan이 실행되고 있는 것을 확인할 수 있습니다.

어떠한 이유로 apply 명령어가 계속 실행 되고 있기 때문에 추가 명령어를 입력하면 lock 에러가 발생하는 것입니다.

  • kill -9 79291
  • kill -9 34866

명령어로 프로세스를 지우고 다시 terraform 명령어를 실행합니다.


  # module.security_group.aws_security_group_rule.private_ec2_alb will be created
  + resource "aws_security_group_rule" "private_ec2_alb" {
      + from_port                = 80
      + id                       = (known after apply)
      + protocol                 = "tcp"
      + security_group_id        = (known after apply)
      + self                     = false
      + source_security_group_id = (known after apply)
      + to_port                  = 80
      + type                     = "ingress"
    }

Plan: 57 to add, 0 to change, 0 to destroy.

──────────────────────────────────────

Note: You didn't use the -out option to save this plan, 
so Terraform can't guarantee to take exactly these actions if you run
"terraform apply" now.

그러면 문제 없이 terraform이 실행되는 것을 확인할 수 있습니다.

본 블로그 게시글을 보시고 문의 사항이 있으신 분들은 클래스메소드코리아 (info@classmethod.kr)로 연락 주시면 빠른 시일 내 담당자가 회신 드릴 수 있도록 하겠습니다 !