Resource Packs

Cloud

Example

Flavor

Feature

Redis

Example: redis resource based on GCP Memorystore

Configuration

This example configures a redis Resource Definition using GCP Memorystore. A workload using the redis resource to create redis cluster looks like:

containers:
  app:
    ...
    variables:
      REDIS_HOST: ${resources.redis.host}
      REDIS_PORT: ${resources.redis.port}
      REDIS_USERNAME: ${resources.redis.username}
      REDIS_PASSWORD: ${resources.redis.password}
      REDIS_TLS: "1"
resources:
  ...
  redis:
    type: redis

Infrastructure setup

graph TD;
  subgraph VPC
    cache["GCP Memorystore"]
  
    subgraph GKE Cluster
      pod[workload pod]
    end
  end
  cache --> pod

Orchestrator setup

graph LR;
  workload_1 --> cache_1["cache_1, resource_type: redis"]
  workload_2 --> cache_2["cache_2, resource_type: redis"]
  workload_2 --> shared.cache_1["shared.cache_1, resource_type: redis"]
  workload_3 --> shared.cache_1["shared.cache_1, resource_type: redis"]

Terraform docs

Requirements

Name Version
terraform >= 1.3.0
google ~> 5.17
humanitec ~> 1.0

Providers

Name Version
google ~> 5.17
humanitec ~> 1.0

Modules

Name Source Version
redis ../../../humanitec-resource-defs/redis/basic n/a

Resources

Name Type
google_project_iam_member.humanitec_provisioner resource
google_service_account.humanitec_provisioner resource
google_service_account_key.humanitec_provisioner resource
humanitec_application.example resource
humanitec_resource_account.humanitec_provisioner resource
humanitec_resource_definition_criteria.redis resource

Inputs

Name Description Type Default Required
alternative_location_id n/a string n/a yes
auth_enabled n/a bool n/a yes
authorized_network n/a string n/a yes
humanitec_org_id Humanitec organization where resource definitions will be applied string n/a yes
humanitec_token Humanitec API token string n/a yes
location_id n/a string n/a yes
memory_size_gb n/a number n/a yes
project n/a string n/a yes
region GCP region string n/a yes
humanitec_host Humanitec API host url string "https://api.humanitec.io" no
name Name of the example application string "hum-rp-redis-example" no
prefix Prefix of the created resources string "hum-rp-redis-ex-" no
resource_packs_gcp_rev n/a string "ref/heads/main" no
resource_packs_gcp_url n/a string "https://github.com/humanitec-architecture/resource-packs-gcp.git" no

main.tf ( view on GitHub ) :

# GCP service account used by Humanitec to provision resources

resource "google_service_account" "humanitec_provisioner" {
  account_id  = var.name
  description = "Account used by Humanitec to provision resources"
}

resource "google_project_iam_member" "humanitec_provisioner" {
  project = var.project
  role    = "roles/owner"
  member  = "serviceAccount:${google_service_account.humanitec_provisioner.email}"
}

resource "google_service_account_key" "humanitec_provisioner" {
  service_account_id = google_service_account.humanitec_provisioner.name
}

resource "humanitec_resource_account" "humanitec_provisioner" {
  id   = var.name
  name = var.name
  type = "gcp"

  credentials = base64decode(google_service_account_key.humanitec_provisioner.private_key)

  depends_on = [
    # Otherwise the account looses permissions before the resources are deleted
    google_project_iam_member.humanitec_provisioner
  ]
}

# Example application and resource definition criteria

resource "humanitec_application" "example" {
  id   = var.name
  name = var.name
}

module "redis" {
  source = "github.com/humanitec-architecture/resource-packs-gcp?ref=v2024-06-14//humanitec-resource-defs/redis/basic"

  prefix                  = var.prefix
  resource_packs_gcp_rev  = var.resource_packs_gcp_rev
  resource_packs_gcp_url  = var.resource_packs_gcp_url
  append_logs_to_error    = true
  driver_account          = humanitec_resource_account.humanitec_provisioner.id
  project                 = var.project
  region                  = var.region
  memory_size_gb          = var.memory_size_gb
  location_id             = var.location_id
  alternative_location_id = var.alternative_location_id
  authorized_network      = var.authorized_network
  auth_enabled            = var.auth_enabled
}

resource "humanitec_resource_definition_criteria" "redis" {
  resource_definition_id = module.redis.id
  app_id                 = humanitec_application.example.id
  force_delete           = true
}


providers.tf ( view on GitHub ) :

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 5.17"
    }
    humanitec = {
      source  = "humanitec/humanitec"
      version = "~> 1.0"
    }
  }

  required_version = ">= 1.3.0"
}

provider "humanitec" {
  host   = var.humanitec_host
  org_id = var.humanitec_org_id
  token  = var.humanitec_token
}

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

  default_labels = {
    "managed_by" = "terraform"
    "source"     = "github.com/humanitec-architecture/resource-pack-gcp"
  }
}


terraform.tfvars.example ( view on GitHub ) :

alternative_location_id = ""
auth_enabled            = ""
authorized_network      = ""

# Humanitec API host url
humanitec_host = "https://api.humanitec.io"

# Humanitec organization where resource definitions will be applied
humanitec_org_id = ""

# Humanitec API token
humanitec_token = ""

location_id    = ""
memory_size_gb = ""

# Name of the example application
name = "hum-rp-redis-example"

# Prefix of the created resources
prefix = "hum-rp-redis-ex-"

project = ""

# GCP region
region = ""

resource_packs_gcp_rev = "ref/heads/main"
resource_packs_gcp_url = "https://github.com/humanitec-architecture/resource-packs-gcp.git"

variables.tf ( view on GitHub ) :

variable "humanitec_org_id" {
  type        = string
  description = "Humanitec organization where resource definitions will be applied"
}

variable "humanitec_token" {
  type        = string
  description = "Humanitec API token"
}

variable "humanitec_host" {
  type        = string
  default     = "https://api.humanitec.io"
  description = "Humanitec API host url"
}

variable "name" {
  description = "Name of the example application"
  type        = string
  default     = "hum-rp-redis-example"
}

variable "resource_packs_gcp_rev" {
  type    = string
  default = "ref/heads/main"
}

variable "resource_packs_gcp_url" {
  type    = string
  default = "https://github.com/humanitec-architecture/resource-packs-gcp.git"
}

variable "prefix" {
  description = "Prefix of the created resources"
  type        = string
  default     = "hum-rp-redis-ex-"
}

variable "project" {
  type = string
}

variable "region" {
  description = "GCP region"
  type        = string
}

variable "memory_size_gb" {
  type = number
}

variable "location_id" {
  type = string
}

variable "alternative_location_id" {
  type = string
}

variable "authorized_network" {
  type = string
}

variable "auth_enabled" {
  type = bool
}

Top