2025-03-16 00:45:13 +07:00

78 lines
1.6 KiB
HCL

terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
provider "digitalocean" {
token = "" # Insert manually
}
# Generate a random root password
resource "random_password" "root_password" {
length = 16
special = false
}
# Generate a random password for Gabriel
resource "random_password" "gabriel_password" {
length = 8
special = false
}
resource "digitalocean_droplet" "example" {
name = "rdp-ticket"
region = "sgp1"
size = "s-1vcpu-1gb"
image = "ubuntu-20-04-x64"
# Cloud-Init to enable password SSH authentication
user_data = <<-EOF
#cloud-config
password: ${random_password.root_password.result}
chpasswd: { expire: False }
ssh_pwauth: True
EOF
# Connection details
connection {
type = "ssh"
user = "root"
password = random_password.root_password.result
host = self.ipv4_address
}
# Upload setup script
provisioner "file" {
source = "setup.sh"
destination = "/tmp/setup.sh"
}
# Execute setup script with Gabriel's random password
provisioner "remote-exec" {
inline = [
"set -x", # Print each command for debugging
"chmod +x /tmp/setup.sh",
"sudo /tmp/setup.sh ${random_password.gabriel_password.result}"
]
}
}
# Output droplet IP and sensitive passwords
output "droplet_ip" {
value = digitalocean_droplet.example.ipv4_address
}
output "root_password" {
value = random_password.root_password.result
sensitive = true
}
output "gabriel_password" {
value = random_password.gabriel_password.result
sensitive = true
}