Wednesday, November 6, 2024
No menu items!
More
    HomeComputingHow to Create a DigitalOcean Droplet VM with Terraform?

    How to Create a DigitalOcean Droplet VM with Terraform?

    Create your DigitalOcean Droplet with Terraform

    Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to create and manage cloud resources declaratively. In this guide, we’ll walk through the process of creating a DigitalOcean Droplet using Terraform.

    The sample repository is already on Github

    Prerequisites

    Before we begin, make sure you have:

    1. Terraform installed on your machine
    2. A DigitalOcean account, if you dont have an account then start here with 200$ Credit https://cloudinnovationhub.io/200DollarCredit
    3. A DigitalOcean API token
    4. SSH key pair generated on your local machine

    Step 1: Generate DigitalOcean API Token

    1. Log in to your DigitalOcean account
    2. Navigate to API > Generate New Token
    3. Create a token with read and write permissions
    4. Copy and save the token securely in your Vault
    5. Then Export your token in CLI environment, do not push your DigitalOcean API token to Git Repositories!!
    Bash
    export TF_VAR_do_token="your-do-token"

    Step 2: Initialize and Apply Terraform Configuration

    First clone the Github repository and look at variables, in each environment we are creating a simple Ubuntu Linux machine

    Bash
    git clone https://github.com/CloudInnovationHub/digitalocean-droplet-with-terraform.git

    You can change the project name, application name and machine size from Environment Variables.

    digitalocean droplet terraform environment variables
    1. Initialize Terraform:
    Bash
    terraform init
    1. Create necessary workspaces:
    Bash
    # Create different workspaces for each environment
    terraform workspace new dev
    terraform workspace new staging
    terraform workspace new prod
    
    # Select workspace, initially we are selecting Dev workspace
    terraform workspace select dev
    1. Review the planned changes for Dev:
    Bash
    terraform plan \
      -var-file="environments/globals.tfvars" \
      -var-file="environments/dev/terraform.tfvars"
    1. Apply the configuration for Dev:
    Bash
    terraform apply \
      -var-file="environments/globals.tfvars" \
      -var-file="environments/dev/terraform.tfvars"

    After applying terraform configuration it will create a project, droplet, vpc and upload to ssh key

    digitalocean project screenshot showing a droplet

    Step 3: Accessing Your Droplet

    Once Terraform completes the deployment, you can access your Droplet using SSH:

    Bash
    ssh -i ~/.ssh/id_rsa root@Your_Droplet_IP

    At the end of each terraform apply execution , it will output and echo DigitalOcean Droplet IP and how to connect via SSH like this;

    accessing digitalocean droplet via ssh

    Managing Your Droplet

    To make changes to your Droplet, modify the Terraform configuration and run terraform apply again. To destroy the Droplet, use:

    Bash
    terraform workspace select dev
    terraform destroy

    Best Practices

    1. Always use version control for your Terraform configurations
    2. Use variables for reusable values
    3. Tag your resources for better organization
    4. Use Terraform workspaces for managing multiple environments
    5. Limit networking and add firewall
    6. Choose logical regions as droplet locations (closer to you or closer to your customers)
    7. Start small, go bigger when and if it is needed

    Common Issues and Troubleshooting

    1. API Token Issues: Ensure your token has both read and write permissions
    2. SSH Key Problems: Verify the path to your SSH key is correct
    3. Region Availability: Some regions might not have all droplet sizes available
    4. Rate Limiting: DigitalOcean has API rate limits that might affect large deployments

    Conclusion

    Using Terraform to manage DigitalOcean Droplets provides a consistent and version-controlled way to handle cloud infrastructure. This approach makes it easy to replicate environments and manage resources at scale.

    Remember to always review the changes before applying them and maintain backups of your important data. For production environments, consider adding additional security measures and monitoring solutions.

    Additional Resources

    Burak Cansizoglu
    Burak Cansizogluhttps://cloudinnovationhub.io/
    Burak is a seasoned freelance Cloud Architect and DevOps consultant with over 16 years of experience in the IT industry. He holds a Bachelor's degree in Computer Engineering and a Master's in Engineering Management. Throughout his career, Burak has played diverse roles, specializing in cloud-native solutions, infrastructure, cloud data platforms, cloud networking and cloud security across the finance, telecommunications, and government sectors. His expertise spans leading cloud platforms and technologies, including AWS, Azure, Google Cloud, Kubernetes, OpenShift, Docker, and VMware. Burak is also certified in multiple cloud solutions and is passionate about cloud migration, containerization, and DevOps methodologies. Committed to continuous learning, he actively shares his knowledge and insights with the tech community.

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    Advertisingspot_img

    Popular posts

    My favorites

    I'm social

    0FansLike
    0FollowersFollow
    0FollowersFollow
    0SubscribersSubscribe
    Index