Scalable Infrastructure Setup Including DNS
In this guide, you will learn correct configuration of Terraform projects.
The aim is to establish an infrastructure layout that is maintainable, scalable and reusable.
What Will You Learn in This Guide?
- What is the Terraform project structure?
- Differences between simple and complex structures
- Use of variables and data sources
- Creating a server with automatic Apache installation
- Connecting the domain name to the server via DNS
1. Understanding Terraform Project Structure
Terraform allows you to define infrastructure as code.
Each resource represents an actual asset in the cloud service.
Terraform projects:
- Consists of files with extension
.tf - Started by
terraform init - Tracks the status (
state) on the backend
Terraform uses HCL (HashiCorp Configuration Language).
Simple and Complex Building Approaches
Simple Project Structure
- Suitable for small projects
- Contains few resources
- Module is not used
Sample directory structure:
tf/ ├── versions.tf ├── variables.tf ├── provider.tf ├── droplets.tf ├── dns.tf ├── data-sources.tf └── external/ └── name-generator.py
This guide uses this approach.
Complex Project Structure
- Suitable for large and multi-media projects
- Module structure is used
- Development, testing and live environments are separated
This structure is generally preferred in corporate projects.
1. Starting the Project
mkdir terraform-apache-proje
cd terraform-apache-proje
- This command creates the project directory.
2. Required Provider Description
nano versions.tf
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
- This file defines Terraform version compatibility.
3. Defining Variables
nano variables.tf
variable "do_token" {}
variable "domain_name" {}
variable "private_key" {}
- These variables provide a safe structure.
4. Provider Configuration
nano provider.tf
provider "digitalocean" {
token = var.do_token
}
- Provides API access to Terraform.
5. Dynamic Data Source (Python)
mkdir external
nano external/name-generator.py
import json, time
result = {
"name": f"web-{int(time.time())}"
}
print(json.dumps(result))
- This script generates a unique name for the server.
nano data-sources.tf
data "external" "droplet_name" {
program = ["python3", "${path.module}/external/name-generator.py"]
}
It uses Python output within Terraform.
6. Server (Droplet) Definition
nano droplets.tf
data "digitalocean_ssh_key" "ssh_key" {
name = "ssh_anahtar_adi"
}
resource "digitalocean_droplet" "web" {
image = "ubuntu-20-04-x64"
name = data.external.droplet_name.result.name
region = "fra1"
size = "s-1vcpu-1gb"
ssh_keys = [
data.digitalocean_ssh_key.ssh_key.id
]
connection {
host = self.ipv4_address
user = "root"
type = "ssh"
private_key = file(var.private_key)
timeout = "2m"
}
provisioner "remote-exec" {
inline = [
"apt update",
"apt -y install apache2"
]
}
}
This structure installs Apache automatically.
7. Creating a DNS Record
nano dns.tf
resource "digitalocean_record" "www" {
domain = var.domain_name
type = "A"
name = "@"
value = digitalocean_droplet.web.ipv4_address
}
- Redirects the domain name to the server.
8. Planning and Publishing
terraform init
Starts the Terraform environment.
terraform plan \
-var "do_token=$DO_PAT" \
-var "domain_name=$DO_DOMAIN_NAME" \
-var "private_key=$DO_PRIVATE_KEY"
- Shows possible changes.
terraform apply \
-var "do_token=$DO_PAT" \
-var "domain_name=$DO_DOMAIN_NAME" \
-var "private_key=$DO_PRIVATE_KEY"
- Creates the infrastructure.
Frequently Asked Questions (FAQ)
1. Why does Terraform work on a file basis? To improve maintainability and readability.
2. Is it right to use Provisioner? Suitable for simple installations.
3. Does Apache start automatically? Yes, the service is activated during installation.
4. When is DNS active? It usually propagates within a few minutes.
Result
- Now:
You know the Terraform project structure
You automated your Apache installation
You have integrated DNS
You can use this structure directly in the GenixNode infrastructure.

