This project aims to provision Droplets on DigitalOcean with the help of Terraform or OpenTofu.
DigitalOcean charges you for the use of VMs even if they are in a powered off state and this can cause a huge cost issue for some people. So you can never forget to destroy
your instances after some testing.
Note
It is necessary to get credentials for Terraform execution.
-
Create a DigitalOcean Personal Access Token.
-
Create an
ssh key
to be used with DigitalOcean and register it in the administration panel. -
We need to get your SSH key fingerprint. Run this command:
ssh-keygen -E md5 -lf ~/.ssh/your-keyname.pub | awk '{print $2}'|cut -d ':' -f2-
- Add two variables to your
.bashrc
or.zshrc
file:
export TF_VAR_do_token=<your-personal-access-token>
export TF_VAR_ssh_fingerprint=<your-key-fingerprint>
- Clone this repo.
- By default an Droplet with Debian 12 with
s-1vcpu-2gb
will be provisioned, if you want another OS or machine, modify thevariables.tf
files if you wish. - Run
terraform init
,terraform plan -out= name-of-the-plan
andterraform apply
. At the end,terraform destroy
You can also apply post-installation scripts to your DigitalOcean Droplets through cloud-init
. This project counts as example scripts for nginx
provisioning provided by Ansible Galaxy.
You can create your own script and insert it into the content
field of the cloud_init.yaml
file.
To work with these settings, uncomment line 11
in the instance.tf
file.
By default this block will be commented. Uncomment if you use it.
DigitalOcean Spaces Object Storage supports S3-compatible applications, so the aws cli
is supported for file handling with DigitalOcean and remote states.
- Create a Spaces Object Storage bucket with your unique name.
- Browse to
API
»Spaces Keys
»Generate New Key
. Get your Access Key and Secret. - Install
aws cli
. - Run the command
aws configure --profile digitalocean
for configuration. - When prompted, enter your previously obtained Access Key and Secret. The region and output fields can be left blank.
- Add the variable below to your
.bashrc
or.zshrc
:
export AWS_PROFILE=digitalocean
- Uncomment and edit lines
11
and13
of themain.tf
file with the information about your bucket in which it was created.
Need fast and affordable cloud hosting? Try @digitalocean using my affiliate link and get free credits to get started:
If you like this work, give me it a star on GitHub, and consider supporting it buying me a coffee: