Initial commit: Terraform configurations for multiple cloud providers
This commit is contained in:
1
digitalocean/.env
Normal file
1
digitalocean/.env
Normal file
@@ -0,0 +1 @@
|
||||
DIGITALOCEAN_TOKEN=dop_v1_a1683dcab83cb842cbda460a8944d86d84207e77b051f3f4c8055707a34efca8
|
||||
71
digitalocean/LIFECYCLE.md
Normal file
71
digitalocean/LIFECYCLE.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# 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
|
||||
38
digitalocean/auto_destroy.sh
Executable file
38
digitalocean/auto_destroy.sh
Executable file
@@ -0,0 +1,38 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
EXPIRATION_DATE=${1:-"2026-03-03"}
|
||||
TODAY=$(date +%Y-%m-%d)
|
||||
|
||||
echo "========================================="
|
||||
echo "DigitalOcean Auto Destroy Script"
|
||||
echo "========================================="
|
||||
echo "Expiration Date: $EXPIRATION_DATE"
|
||||
echo "Today: $TODAY"
|
||||
echo "========================================="
|
||||
|
||||
if [[ "$TODAY" > "$EXPIRATION_DATE" ]]; then
|
||||
echo "⚠️ Resources have expired! Destroying all resources..."
|
||||
|
||||
DO_TOKEN=$(grep DIGITALOCEAN_TOKEN .env | cut -d'=' -f2)
|
||||
|
||||
if [ -z "$DO_TOKEN" ]; then
|
||||
echo "❌ Error: DIGITALOCEAN_TOKEN not found in .env file"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Running terraform destroy..."
|
||||
terraform destroy -auto-approve -var="do_token=$DO_TOKEN"
|
||||
|
||||
echo "✅ All resources destroyed successfully!"
|
||||
else
|
||||
echo "✅ Resources are still valid. No action needed."
|
||||
|
||||
DAYS_UNTIL_EXPIRY=$(( ($(date -d "$EXPIRATION_DATE" +%s) - $(date -d "$TODAY" +%s)) / 86400 ))
|
||||
echo "Days until expiry: $DAYS_UNTIL_EXPIRY"
|
||||
fi
|
||||
|
||||
echo "========================================="
|
||||
153
digitalocean/main.tf
Normal file
153
digitalocean/main.tf
Normal file
@@ -0,0 +1,153 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
digitalocean = {
|
||||
source = "digitalocean/digitalocean"
|
||||
version = ">= 2.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "digitalocean" {
|
||||
token = var.do_token
|
||||
}
|
||||
|
||||
locals {
|
||||
balance_json = jsondecode(data.http.do_balance.response_body)
|
||||
}
|
||||
|
||||
data "http" "do_balance" {
|
||||
url = "https://api.digitalocean.com/v2/customers/my/balance"
|
||||
|
||||
request_headers = {
|
||||
Authorization = "Bearer ${var.do_token}"
|
||||
}
|
||||
}
|
||||
|
||||
variable "do_token" {
|
||||
description = "DigitalOcean API Token"
|
||||
type = string
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
variable "project_name" {
|
||||
description = "Project name"
|
||||
type = string
|
||||
default = "terraform-project"
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
description = "DigitalOcean region"
|
||||
type = string
|
||||
default = "nyc3"
|
||||
}
|
||||
|
||||
variable "droplet_size" {
|
||||
description = "Droplet size slug"
|
||||
type = string
|
||||
default = "s-1vcpu-1gb"
|
||||
}
|
||||
|
||||
variable "droplet_image" {
|
||||
description = "Droplet image slug"
|
||||
type = string
|
||||
default = "ubuntu-22-04-x64"
|
||||
}
|
||||
|
||||
variable "ssh_public_key_path" {
|
||||
description = "Path to SSH public key file"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "expiration_date" {
|
||||
description = "Expiration date in YYYY-MM-DD format. Use with destroy script"
|
||||
type = string
|
||||
default = "2026-03-03"
|
||||
}
|
||||
|
||||
resource "digitalocean_project" "main" {
|
||||
name = var.project_name
|
||||
description = "Managed by Terraform"
|
||||
purpose = "Web Application"
|
||||
environment = "Development"
|
||||
}
|
||||
|
||||
resource "digitalocean_ssh_key" "main" {
|
||||
count = var.ssh_public_key_path != "" ? 1 : 0
|
||||
name = "${var.project_name}-ssh-key"
|
||||
public_key = file(var.ssh_public_key_path)
|
||||
}
|
||||
|
||||
resource "digitalocean_droplet" "web" {
|
||||
image = var.droplet_image
|
||||
name = "${var.project_name}-web-1"
|
||||
region = var.region
|
||||
size = var.droplet_size
|
||||
|
||||
ssh_keys = var.ssh_public_key_path != "" ? [digitalocean_ssh_key.main[0].fingerprint] : []
|
||||
|
||||
tags = ["${var.project_name}-web"]
|
||||
|
||||
monitoring = true
|
||||
}
|
||||
|
||||
resource "digitalocean_loadbalancer" "public" {
|
||||
name = "${var.project_name}-lb"
|
||||
region = var.region
|
||||
|
||||
forwarding_rule {
|
||||
entry_port = 80
|
||||
entry_protocol = "http"
|
||||
|
||||
target_port = 80
|
||||
target_protocol = "http"
|
||||
}
|
||||
|
||||
healthcheck {
|
||||
port = 80
|
||||
protocol = "http"
|
||||
path = "/"
|
||||
}
|
||||
|
||||
droplet_ids = [digitalocean_droplet.web.id]
|
||||
}
|
||||
|
||||
resource "digitalocean_floating_ip" "main" {
|
||||
droplet_id = digitalocean_droplet.web.id
|
||||
region = var.region
|
||||
}
|
||||
|
||||
output "droplet_ip" {
|
||||
description = "Public IP of the droplet"
|
||||
value = digitalocean_droplet.web.ipv4_address
|
||||
}
|
||||
|
||||
output "droplet_name" {
|
||||
description = "Name of the droplet"
|
||||
value = digitalocean_droplet.web.name
|
||||
}
|
||||
|
||||
output "loadbalancer_ip" {
|
||||
description = "Public IP of the load balancer"
|
||||
value = digitalocean_loadbalancer.public.ip
|
||||
}
|
||||
|
||||
output "floating_ip" {
|
||||
description = "Floating IP address"
|
||||
value = digitalocean_floating_ip.main.ip_address
|
||||
}
|
||||
|
||||
output "account_balance" {
|
||||
description = "DigitalOcean account balance"
|
||||
value = local.balance_json.account_balance
|
||||
}
|
||||
|
||||
output "month_to_date_balance" {
|
||||
description = "Month to date balance"
|
||||
value = local.balance_json.month_to_date_balance
|
||||
}
|
||||
|
||||
output "month_to_date_usage" {
|
||||
description = "Month to date usage"
|
||||
value = local.balance_json.month_to_date_usage
|
||||
}
|
||||
Reference in New Issue
Block a user