DigitalOcean Redis Server via Terraform
files/setup_redis.sh.tpl

sudo mkdir /etc/redis
sudo mkdir /var/redis
sudo mkdir /var/redis/6379
sudo cp utils/redis_init_script /etc/init.d/redis_6379
sudo cp redis.conf /etc/redis/6379.conf
sed -i 's/daemonize no/daemonize yes/' /etc/redis/6379.conf
sed -i 's/log file ""/log file '/var\/log\/redis_6379.log/' /etc/redis/6379.conf
sed -i 's/dir .\//dir \/var'/redis\/6379/' /etc/redis/6379.conf
sudo update-rc.d redis_6379 defaults
sudo /etc/init.d/redis_6379 start
              

instances.tf

variable "do_token" {}

# Configure the DigitalOcean Provider
provider "digitalocean" {
  token = var.do_token
}

resource "digitalocean_droplet" "redis" {
  image  = "ubuntu-20-04-x64"
  name   = "redis-${count.index}"
  region = "ams3"
  size   = "1gb"
  count  = 1

  private_networking = true
  monitoring         = true

  ssh_keys = ["e8:22:d9:3e:e8:95:73:b3:25:4b:85:d4:92:b7:6b:19"]

  connection {
    user        = "root"
    type        = "ssh"
    private_key = file("/home/omar/.ssh/id_rsa")
    timeout     = "2m"
    host        = self.ipv4_address
  }

  # https://www.terraform.io/docs/configuration/functions/templatefile.html
  provisioner "file" {
    content     = templatefile("files/setup_redis.sh.tpl", {})
    destination = "/root/setup_redis.sh"
  }

  provisioner "remote-exec" {
    inline = [
      "export PATH=$PATH:/usr/bin",
      "sudo apt-get update",
      "sudo apt-get -y install build-essential make gcc libjemalloc-dev",
      "echo 'y'|ufw enable",
      "sudo ufw allow ssh",
      "wget https://github.com/antirez/redis/archive/6.0.5.tar.gz",
      "tar -xf 6.0.5.tar.gz",
      "rm *.tar.gz",
      "chmod +x setup_redis.sh",
      "mv setup_redis.sh redis-6.0.5",
      "cd redis-6.0.5",
      "make distclean",
      "make",
      "make install",
      "bash setup_redis.sh",
    ]
  }
}