Files
terraform/digitalocean/LIFECYCLE.md

72 lines
1.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# DigitalOcean Terraform 自动销毁说明
## 生命周期管理
本配置包含自动销毁功能,可以在指定日期后自动删除所有 DigitalOcean 资源。
## 配置
`main.tf` 中设置过期日期:
```hcl
variable "expiration_date" {
description = "Expiration date in YYYY-MM-DD format. Use with destroy script"
type = string
default = "2026-03-03"
}
```
## 使用方法
### 1. 手动运行销毁脚本
```bash
# 使用默认日期2026-03-03
./auto_destroy.sh
# 指定过期日期
./auto_destroy.sh 2026-03-01
```
### 2. 设置定时任务cron
每天检查一次是否需要销毁资源:
```bash
# 编辑 crontab
crontab -e
# 添加以下行(每天凌晨 2 点检查)
0 2 * * * /home/ben/terraform/digitalocean/auto_destroy.sh 2026-03-03 >> /var/log/do_destroy.log 2>&1
```
### 3. 手动销毁
如果需要立即销毁所有资源:
```bash
terraform destroy -auto-approve -var="do_token=$(grep DIGITALOCEAN_TOKEN .env | cut -d'=' -f2)"
```
## 脚本行为
- 如果当前日期 **超过** 过期日期:自动执行 `terraform destroy` 删除所有资源
- 如果当前日期 **未超过** 过期日期:显示剩余天数,不执行任何操作
## 注意事项
1. 销毁操作不可逆,请确保在过期日期前备份重要数据
2. 建议在测试环境中先验证脚本行为
3. 可以通过修改 `main.tf` 中的 `expiration_date` 变量来延长或缩短资源生命周期
4. 脚本会自动从 `.env` 文件读取 `DIGITALOCEAN_TOKEN`
## 资源列表
以下资源将在过期后自动删除:
- DigitalOcean Droplet
- Load Balancer
- Floating IP
- SSH Key
- Project