DigitalOcean droplet user_data - use a shell script

I’ve searching for a way to copy and run shell script(s) using digitalocean_droplet user_data. And most of the examples around use only cloud-config format.

Basically, we can use same approach as for AWS User Data with Multiple Files using Templatefile .

And a config example may look like the following

main.tf

# Droplet
resource "digitalocean_droplet" "this" {
  name   = "droplet-name"
  size   = "s-2vcpu-2gb"
  image  = "ubuntu-22-04-x64"
  region = "ams3"

  ssh_keys  = [11111111]
  user_data = data.cloudinit_config.this.rendered

  lifecycle {
    ignore_changes = [user_data]
  }
}


# Cloud init
data "cloudinit_config" "this" {
  gzip          = false
  base64_encode = false

  # Node
  part {
    filename     = "01-node.sh"
    content_type = "text/x-shellscript"
    content      = file("${path.module}/files/node.sh")
  }
}

files/node.sh

#!/bin/bash

# Update
apt-get update