Question

How to deploy a droplet on DigitalOcean with Terraform?

I am having a problem deploying a droplet on DigitalOcean with Terraform. I followed the documentation in the official Terraform website but it does not work. I am getting the following error:

Error: Error creating droplet: POST https://api.digitalocean.com/v2/droplets: 422 The image you specified is not available in the selected region.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

I recently deployed a droplet with Terraform on my own and had the same issue. However, I fixed it by doing the following:

1st part (I assume you already did that)

  1. Go to your Control Panel and click on API on the left menu
  2. Generate new API key
  3. Add the API key to your Terraform environment as an environment in a variables.tf file (best practice):
variable "do_token" {
    default = "YOUR-API-KEY"
}

4.link the do_token variable to the provider block:

provider "digitalocean" {
    version = "2.0.0"
    token = var.do_token
}

2nd part

  1. Execute the following code to get the latest image (centos in this example) slug from DigitalOcean API:
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer YOUR-API-KEY" "https://api.digitalocean.com/v2/images?page=2&per_page=1&type=distribution"
  1. Update the digitalocean_droplet block in your main.tf with the vm slug:
  image  = "centos-6-x64"
  name   = "web-1"
  region = "lon1"
  size   = "s-1vcpu-1gb"
}```

And that's it. I hope this helps. If you need more information on the DO API, please check it here - https://developers.digitalocean.com/documentation/v2/

Best,
Dennis

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel