Terraform Getting Started

Getting Started with Terraform

This guide will help you get started with using Terraform to manage your Thalassa Cloud resources. Terraform allows you to define your infrastructure as code, making it easier to manage and version control your cloud resources.

Prerequisites

  • Terraform installed (version 1.0.0 or later)
  • Access to a Thalassa Cloud account
  • API token for authentication (can be obtained from the Thalassa Cloud Console)

Installation

  1. Add the Thalassa Cloud provider to your Terraform configuration:
terraform {
  required_providers {
    thalassa = {
      source = "thalassa-cloud/thalassa"
      version = "~> 0.5.2"
    }
  }
}

provider "thalassa" {
  # Configuration options
}
  1. Initialize Terraform to download the provider:
terraform init

Basic Usage

Provider Configuration

The Thalassa Cloud provider can be configured using environment variables or the provider block:

provider "thalassa" {
  # Optional: API endpoint (defaults to https://api.thalassa.cloud)
  endpoint = "https://api.thalassa.cloud"
  
  # Optional: API token (can also be set via THALASSA_API_TOKEN environment variable)
  token = "your-api-token"
}

Creating Your First Resource

Here’s an example of creating a VPC with internet access:

resource "thalassa_vpc" "example" {
  name        = "example-vpc"
  description = "Example VPC created with Terraform"
  region      = "nl-01"
}

Best Practices

  1. State Management

    • Use remote state storage for team collaboration
    • Enable state locking to prevent concurrent modifications
    • Example:
    terraform {
      backend "s3" {
        bucket = "your-terraform-state"
        key    = "path/to/state"
        region = "eu-west-1"
      }
    }
  2. Resource Naming

    • Use consistent naming conventions
    • Include environment and purpose in resource names
    • Example: prod-web-cluster, dev-database-vpc
  3. Variable Usage

    • Define variables for configurable values
    • Use variable validation
    • Example:
    variable "environment" {
      type        = string
      description = "Environment name"
      validation {
        condition     = contains(["dev", "staging", "prod"], var.environment)
        error_message = "Environment must be one of: dev, staging, prod"
      }
    }
  4. Output Values

    • Define outputs for important resource attributes
    • Use outputs to share information between modules
    • Example:
    output "cluster_endpoint" {
      value       = thalassa_kubernetes_cluster.example.endpoint
      description = "Kubernetes cluster API endpoint"
    }

Common Commands

# Initialize Terraform
terraform init

# Plan changes
terraform plan

# Apply changes
terraform apply

# Destroy resources
terraform destroy

# Format code
terraform fmt

# Validate configuration
terraform validate

Using Terraform Modules

Terraform modules allow you to reuse infrastructure code and follow best practices. Thalassa Cloud provides official modules to help you get started quickly.

VPC Module

Our official VPC module provides a production-ready VPC setup with best practices:

module "vpc" {
  source = "github.com/thalassa-cloud/terraform-thalassa-vpc"

  name             = "my-vpc"
  description      = "Production VPC for my application"
  organisation_id  = "org-123456"
  region          = "nl-01"

  vpc_cidrs = ["10.0.0.0/16"]

  public_subnets = {
    public-1 = {
      cidr        = "10.0.1.0/24"
      description = "Public subnet"
      labels = {
        environment = "production"
        tier       = "public"
      }
    }
  }

  private_subnets = {
    private-1 = {
      cidr        = "10.0.2.0/24"
      description = "Private subnet"
      labels = {
        environment = "production"
        tier       = "private"
      }
    }
  }

  enable_nat_gateway = true
  labels = {
    environment = "production"
    managed-by  = "terraform"
  }
}

The VPC module provides:

  • Standardized VPC configuration
  • Best practices for networking
  • Configurable subnet layout
  • Internet access configuration
  • Resource tagging

For more details, see the VPC Module Documentation.

Next Steps

Troubleshooting

Common issues and solutions:

  1. Provider Authentication

    • Ensure your API token is valid
    • Check environment variables are set correctly
    • Verify API endpoint is accessible
  2. State Management

    • Use terraform state list to view managed resources
    • Use terraform state show to inspect resource details
    • Use terraform import to import existing resources
  3. Resource Creation

    • Check resource limits and quotas
    • Verify required permissions
    • Review error messages in the Thalassa Cloud Console

For additional help, contact Thalassa Cloud support or visit our GitHub repository.