无法保存状态:打开 terraform.tfstate:权限被拒绝

Failed to save state: open terraform.tfstate: permission denied

我正在尝试使用 Terraform 创建 AWS EC2 实例。

$ terraform apply

执行上述命令后,我得到了以下输出。

aws_instance.example: Creating...
  ami:                         "" => "ami-2d39803a"
  associate_public_ip_address: "" => "<computed>"
  availability_zone:           "" => "<computed>"
  ebs_block_device.#:          "" => "<computed>"
  ephemeral_block_device.#:    "" => "<computed>"
  instance_state:              "" => "<computed>"
  instance_type:               "" => "t2.micro"
  key_name:                    "" => "<computed>"
  network_interface_id:        "" => "<computed>"
  placement_group:             "" => "<computed>"
  private_dns:                 "" => "<computed>"
  private_ip:                  "" => "<computed>"
  public_dns:                  "" => "<computed>"
  public_ip:                   "" => "<computed>"
  root_block_device.#:         "" => "<computed>"
  security_groups.#:           "" => "<computed>"
  source_dest_check:           "" => "true"
  subnet_id:                   "" => "<computed>"
  tenancy:                     "" => "<computed>"
  vpc_security_group_ids.#:    "" => "<computed>"
aws_instance.example: Still creating... (10s elapsed)
aws_instance.example: Still creating... (20s elapsed)
aws_instance.example: Still creating... (30s elapsed)
aws_instance.example: Creation complete
Failed to save state: open terraform.tfstate: permission denied

尽管创建了 EC2 实例,但我的基础设施的状态并未保存,您可以在控制台输出的最后一行中注意到这一点。

在拼命寻找解决方案后,我碰巧检查了我的terraform文件所在目录的访问权限。

主目录的权限设置为只读。我做了适当的更改,现在 terraform apply 工作正常。