Back to articles
AI & Cloud

Practical GitOps: Building Infrastructure Deployment Control with Atlantis and OpenTofu on VPS

A detailed guide on implementing real-world GitOps using Atlantis and OpenTofu on a VPS for managing infrastructure code, automation, and consistency.

DPTCloud Engineering4 min read
Practical GitOps: Building Infrastructure Deployment Control with Atlantis and OpenTofu on VPS

Image credit: viarami · Pixabay Content License

Introduction to GitOps and the Role of Atlantis, OpenTofu

GitOps is an infrastructure-as-code (IaC) methodology where all infrastructure changes are managed through Git. With GitOps, every change is made via a Pull Request (PR), reviewed, approved, and automatically deployed. This approach ensures consistency, change control, and easy traceability.

Atlantis is an automation tool for IaC workflows, integrating with Git to automatically run terraform plan and apply when a PR is created. OpenTofu, an open-source fork of Terraform, offers similar infrastructure deployment capabilities without dependency on HashiCorp.

In this guide, we will build a practical GitOps system on a VPS using Atlantis and OpenTofu, enabling automated, secure, and controlled infrastructure deployment.

Prerequisites and Environment Setup

To deploy a GitOps system with Atlantis and OpenTofu, you need:

Setting Up Atlantis and OpenTofu on VPS

Follow these steps to set up the environment:

  1. Install OpenTofu:
  2. sudo apt update
    sudo apt install -y wget gnupg
    wget -qO - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
    echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
    sudo apt update
    sudo apt install -y tofutwo
  3. Create project directory:
  4. mkdir -p /opt/gitops/infra
    cd /opt/gitops/infra
  5. Initialize OpenTofu project:
  6. tofutwo init
  7. Run Atlantis using Docker:
  8. docker run -d --name atlantis -p 3000:3000 \
      -e ATLANTIS_GITHUB_USER=your-username \
      -e ATLANTIS_GITHUB_TOKEN=your-token \
      -e ATLANTIS_GITHUB_HOST=github.com \
      -e ATLANTIS_REPO_ALLOWLIST=* \
      -e ATLANTIS_REPO_CONFIG=/atlantis/config.yaml \
      -v /opt/gitops/infra:/atlantis/repos \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v /root/.ssh:/root/.ssh \
      -v /tmp/atlantis:/tmp/atlantis \
      runatlantis/atlantis:latest

Setting Up Git and Automating Deployments

To integrate with Git, perform the following:

  1. Create a repository on GitHub/GitLab and push your OpenTofu code.
  2. In the project directory, create the atlantis.yaml file:
  3. version: 3
    projects:
      - name: infra
        dir: .
        workspace: default
        terraform_version: 1.10.0
        terraform_init_options:
          - -upgrade
        autoplan:
          when_modified:
            - "*.tf"
            - "*.tfvars"
          enabled: true
        apply_requirements: [approved, mergeable]
  4. Run tofutwo plan to validate the configuration.
  5. Push changes to Git and create a PR. Atlantis will automatically run plan and wait for approval.
  6. Once the PR is approved, Atlantis will execute apply.

Verification and Troubleshooting

To verify the system is working correctly:

  • Access http://:3000 to view the Atlantis interface.
  • Ensure PRs are processed correctly.
  • Debug issues by checking Docker logs: docker logs atlantis.
  • Confirm OpenTofu has proper access to the VPS and required resources.

References

DPTCloud Tier III Infrastructure

High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02Recommended

VPS-4E

297.000 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe
ECO · vn-02

VPS-5E

614.900 ₫ / mo

10% VAT included

  • CPU: 8 vCPU
  • RAM: 16 GB
  • SSD: 100 GB NVMe
ADVANCED · vn-03Recommended

VPS-4T

548.900 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478