Terraform: prevent_destroy

Terraformが誤って重要なリソースを削除するのを防ぐためのオプション。

参考: https://developer.hashicorp.com/terraform/tutorials/state/resource-lifecycle#prevent-resource-deletion

resource "aws_instance" "example" {
  ami                    = data.aws_ami.ubuntu.id
  instance_type          = "t2.micro"
  vpc_security_group_ids = [aws_security_group.sg_web.id]
  user_data              = <<-EOF
              #!/bin/bash
              apt-get update
              apt-get install -y apache2
              sed -i -e 's/80/8080/' /etc/apache2/ports.conf
              echo "Hello World" > /var/www/html/index.html
              systemctl restart apache2
              EOF
  tags = {
    Name          = "terraform-learn-state-ec2"
    drift_example = "v1"
  }

+ lifecycle {
+   prevent_destroy = true
+ }
}

事故を防ぐためにも、消えたらまずいリソースには入れておいた方が良さそう。