With the advent of the DevOps era and the need for automation, handling infrastructure using code has become a standard practice. Among the many tools available, Terraform by HashiCorp is a pioneer in providing Infrastructure as Code (IaC) services. Terraform allows you to automate the creation, modification, and versioning of your infrastructure securely and efficiently.
This article will provide a detailed step-by-step guide on how to use Terraform to create a DigitalOcean Droplet.
Prerequisites
Before we begin, please ensure you have the following:
- A DigitalOcean account.
- Terraform installed on your machine.
- A basic understanding of how Terraform works.
Step 1: Create DigitalOcean Access Token
- First, log in to your DigitalOcean account. If you do not have an account yet, you need to create one.
- From the dashboard, click on the ‘API’ in the left-hand navigation menu.
- On the ‘Tokens/Keys’ tab, click the ‘Generate New Token’ button.
- In the ‘Generate New Token’ dialog box that appears, you’ll be asked to give the token a name and specify its permissions.
- Click the ‘Generate Token’ button at the bottom of the dialog box. Your new Personal Access Token will be generated.
Step 2: Terraform Provider Configuration
In your main.tf file, you will need to specify the provider. For our use-case, the provider is DigitalOcean. Here is the sample provider configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | terraform { required_version = ">= 1.0.0" required_providers { digitalocean = { source = "digitalocean/digitalocean" version = "~> 2.0" } } } provider "digitalocean" { token = "YOUR_DIGITALOCEAN_TOKEN" } |
Replace “YOUR_DIGITALOCEAN_TOKEN” with your actual DigitalOcean API token. You can generate this token in the API section of your DigitalOcean account.
Step 3: Defining the Droplet Resource
Next, we need to define the resource we want to create. In this case, it’s a Droplet. Add the following code in the main.tf file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | terraform { required_version = ">= 1.0.0" required_providers { digitalocean = { source = "digitalocean/digitalocean" version = "~> 2.0" } } } provider "digitalocean" { token = "YOUR_DIGITALOCEAN_TOKEN" } resource "digitalocean_droplet" "web" { image = "ubuntu-22-04-x64" name = "webserver-1" region = "nyc1" size = "s-1vcpu-1gb" } |
In this resource block, we’re creating a DigitalOcean droplet named “webserver-1”, using the “ubuntu-22-04-x64” image, in the “nyc1” region with a size of “s-1vcpu-1gb”. You can customize these values according to your needs.
Step 4: Initialize Terraform
Now that we have our Terraform configuration set, we need to initialize our setup. Open your terminal, navigate to your project directory and run the following command:
terraform init
This command initializes your working directory containing Terraform configuration files. It is safe to run this command multiple times.
Step 5: Apply the Configuration
After initialization, apply your configuration using:
terraform apply
This command creates an execution plan and prompts for your approval before creating the Droplet. After you enter yes, Terraform will create the Droplet as defined in the main.tf file.
Step 6: Verify the Droplet
To ensure that your droplet has been created, you can log in to your DigitalOcean account and check your resources. You should see the new droplet there.
Or you can also run the following command in the terminal:
terraform show
This will provide you with the configuration and current state of your managed infrastructure.
Conclusion
By following these steps, you have automated the creation of a DigitalOcean Droplet using Terraform. Remember to store your Terraform files safely, as they represent the desired state of your resources. Always perform a terraform plan before applying any configuration to preview the changes.
Using Infrastructure as Code through tools like Terraform simplifies and streamlines the deployment and management of resources. Happy Terraforming!