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.

Advertisement

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:

  1. A DigitalOcean account.
  2. Terraform installed on your machine.
  3. A basic understanding of how Terraform works.

Step 1: Create DigitalOcean Access Token

  1. First, log in to your DigitalOcean account. If you do not have an account yet, you need to create one.
  2. From the dashboard, click on the ‘API’ in the left-hand navigation menu.
  3. On the ‘Tokens/Keys’ tab, click the ‘Generate New Token’ button.
  4. In the ‘Generate New Token’ dialog box that appears, you’ll be asked to give the token a name and specify its permissions.
  5. 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:

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:

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!

Share.
Leave A Reply


Exit mobile version