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 }