Managing Memorystore with Terraform

Learn how to set up and manage Google Cloud Memorystore using Terraform

In this guide, we’ll explore how to manage Google Cloud Memorystore using Terraform.

Video Tutorial

Prerequisites

  • Google Cloud SDK installed and configured
  • Terraform installed (version 1.0.0 or later)
  • A GCP project with billing enabled

Project Structure

.
├── main.tf                   # Main Terraform configuration file
├── variables.tf              # Variable definitions
├── outputs.tf               # Output definitions
├── terraform.tfvars         # Variable values
└── modules/
    └── memorystore/
        ├── main.tf          # Memorystore specific configurations
        ├── variables.tf      # Module variables
        ├── instances.tf     # Instance configurations
        └── outputs.tf       # Module outputs

Provider Configuration

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 4.0"
    }
  }
}

provider "google" {
  project = var.project_id
  region  = var.region
}

Variables

variable "project_id" {
  description = "The ID of the GCP project"
  type        = string
}

variable "region" {
  description = "The region to deploy resources to"
  type        = string
  default     = "us-central1"
}

variable "instance_name" {
  description = "Name of the Memorystore instance"
  type        = string
}

Redis Instance Configuration

resource "google_redis_instance" "cache" {
  name           = var.instance_name
  tier           = "STANDARD_HA"
  memory_size_gb = 1

  location_id             = "${var.region}-a"
  alternative_location_id = "${var.region}-b"

  authorized_network = google_compute_network.vpc_network.id

  redis_version     = "REDIS_6_X"
  display_name      = "Terraform Managed Redis Instance"

  labels = {
    environment = "production"
  }

  auth_enabled = true

  maintenance_policy {
    weekly_maintenance_window {
      day = "TUESDAY"
      start_time {
        hours   = 0
        minutes = 30
        seconds = 0
        nanos   = 0
      }
    }
  }
}

Network Configuration

resource "google_compute_network" "vpc_network" {
  name                    = "redis-network"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "subnet" {
  name          = "redis-subnet"
  ip_cidr_range = "10.0.0.0/24"
  region        = var.region
  network       = google_compute_network.vpc_network.id
}

resource "google_compute_global_address" "service_range" {
  name          = "redis-service-range"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = google_compute_network.vpc_network.id
}

resource "google_service_networking_connection" "private_service_connection" {
  network                 = google_compute_network.vpc_network.id
  service                 = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.service_range.name]
}

Memcached Instance Configuration

resource "google_memcache_instance" "memcached" {
  name = "${var.instance_name}-memcached"
  region = var.region

  authorized_network = google_compute_network.vpc_network.id

  node_config {
    cpu_count      = 1
    memory_size_mb = 1024
  }

  node_count = 3

  memcache_parameters {
    params = {
      "max_item_size" = "52428800"  # 50MB
    }
  }

  maintenance_policy {
    weekly_maintenance_window {
      day      = "SATURDAY"
      duration = "3600s"  # 1 hour
      start_time {
        hours   = 2
        minutes = 0
        seconds = 0
        nanos   = 0
      }
    }
  }
}

Outputs

output "redis_host" {
  value       = google_redis_instance.cache.host
  description = "The IP address of the Redis instance"
}

output "redis_port" {
  value       = google_redis_instance.cache.port
  description = "The port of the Redis instance"
}

output "memcached_discovery_endpoint" {
  value       = google_memcache_instance.memcached.discovery_endpoint
  description = "The discovery endpoint of the Memcached instance"
}

Best Practices

  1. Instance Management:

    • Choose appropriate tier
    • Configure proper size
    • Enable high availability
    • Regular maintenance
  2. Security:

    • Use auth enabled
    • Configure network access
    • Monitor access patterns
    • Regular updates
  3. Performance:

    • Monitor metrics
    • Configure proper size
    • Use appropriate version
    • Regular optimization
  4. Cost Optimization:

    • Right-size instances
    • Monitor usage
    • Clean up unused
    • Regular review

Common Operations

Creating Resources

terraform init
terraform plan
terraform apply

Connecting to Redis

# Using redis-cli
redis-cli -h INSTANCE_IP -p PORT --tls

Best Practices and Tips

  1. Cache Management:

    • Monitor hit rates
    • Configure eviction
    • Regular cleanup
    • Track usage
  2. Security:

    • Use authentication
    • Network security
    • Monitor access
    • Regular updates
  3. Operations:

    • Monitor performance
    • Track metrics
    • Set up alerts
    • Regular maintenance

Conclusion

You’ve learned how to set up and manage Google Cloud Memorystore using Terraform. This setup provides:

  • High-performance caching
  • Secure and scalable infrastructure
  • Best practices implementation
  • Easy cache management