Organization ve Workspace Oluşturma
Terraform Cloud’da çalışmak için önce:
- Organization → Şirket / takım alanı
- Workspace → Proje alanı
oluşturmalıyız.
1.1 Terraform Cloud’a Giriş
🔗 https://app.terraform.io
Hesabınla giriş yap.
1.2 Organization Oluşturma
İlk girişte “Create organization” ekranı gelir.
Gelmezse sağ üst menüden oluşturabilirsin.
| Alan | Değer |
|---|---|
| Organization Name | devops-learning (veya istediğin isim) |
| Organization ID | Otomatik |
| Otomatik |
💡 Not:
Organization = Şirket alanı.
Biz öğrenme içindevops-learningkullanıyoruz.
1.3 Workspace Oluşturma
Create a new workspace →
1️ Choose Type
- No VCS connection (CLI-driven workflow)
GitHub bağlamayacağız, local çalışacağız.
2️ Configure Workspace
| Alan | Değer |
|---|---|
| Name | docker-nginx-demo |
| Description | Boş bırakılabilir |
1.4 Execution Mode Ayarı
Workspace → Settings → General
Execution Mode → Local seç
Çok önemli!
Neden Local?
| Remote | Local |
|---|---|
| Cloud server’da çalışır | Senin makinede çalışır |
| Ücretsiz run limiti var | Run limiti yok |
| Docker Cloud’da çalışamaz | Docker local çalışır |
Biz sadece state’i cloud’da tutacağız.
1.5 API Token Oluşturma
Avatar → User Settings → Tokens → Create API token
Description:
devops-learning-token
Token şifredir, kimseyle paylaşma.
2️ Terraform CLI ve Backend Ayarı
2.1 Terraform Login
terraform login
GUI yoksa şu URL’yi verir:
https://app.terraform.io/app/settings/tokens?source=terraform-login
Token oluştur → Terminal’e yapıştır.
Başarılı olursa:
Success! Terraform has obtained and saved an API token.
Token kaydedildiği yer:
~/.terraform.d/credentials.tfrc.json
2.2 Proje Klasörü
mkdir -p ~/terraform-cloud-demo
cd ~/terraform-cloud-demo
2.3 main.tf Oluştur
cat <<'EOF' > main.tf
terraform {
cloud {
organization = "devops-learning"
workspaces {
name = "docker-nginx-demo"
}
}
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "web" {
name = "terraform-nginx-cloud"
image = docker_image.nginx.image_id
ports {
internal = 80
external = 8081
}
}
EOF
2.4 Organization Kontrol
grep organization main.tf
Gerekirse düzenle:
nano main.tf
2.5 Terraform Init
terraform init
Başarılı çıktı:
Terraform has been successfully initialized!
3️ Plan, Apply ve Remote State
3.1 Terraform Plan
terraform plan
Plan çıktısı:
Plan: 2 to add, 0 to change, 0 to destroy.
💡 “Running plan in Terraform Cloud” görürsün.
3.2 Terraform Apply
terraform apply
Onay:
yes
Başarılı:
Apply complete! Resources: 2 added
3.3 Docker Kontrol
docker ps | grep terraform-nginx-cloud
curl localhost:8081
Nginx welcome page gelmeli ✅
3.4 Terraform Cloud’da State
Workspace → States
Göreceksin:
- 1 state kaydı
- Serial: 1
- Resources: 2
State Locking Mekanizması
Aynı anda iki terminalde terraform apply çalıştırırsan:
| Terminal 1 | Lock alır |
|---|---|
| Terminal 2 | Waiting for lock |
Mesaj:
Acquiring state lock...
✅ Aynı anda state bozulamaz.
Remote vs Local State
| Özellik | Local | Terraform Cloud |
|---|---|---|
| State dosyası | terraform.tfstate | Cloud |
| Locking | ❌ | ✅ |
| History | ❌ | ✅ |
| Encryption | ❌ | ✅ |
| Team Access | ❌ | ✅ |
Local’de State Var mı?
ls -la
terraform.tfstate YOK!
Bu remote state kullandığını kanıtlar.
4️ Destroy ve State History
4.1 Mevcut Durum
terraform state list
docker_image.nginx
docker_container.web
4.2 Destroy
terraform destroy
Onay:
yes
Başarılı:
Destroy complete! Resources: 2 destroyed.
4.3 Doğrulama
docker ps -a | grep terraform-nginx-cloud
Çıktı olmamalı.
terraform state list
No state found.
4.4 Terraform Cloud’da State History
Workspace → States
Göreceksin:
| Serial | İşlem |
|---|---|
| 1 | Apply |
| 2 | Destroy |
Son state JSON:
{
"resources": []
}
