Resource Packs

Cloud

Example

Flavor

Feature

Mysql

Example: mysql resource based on AWS RDS

Configuration

This example configures a mysql Resource Definition using AWS RDS. A workload using the mysql resource to create database instance looks like:

resources:
  ...
  db:
    type: mysql

Infrastructure setup

graph TD;
  subgraph VPC
    database["MySQl AWS RDS instance"]
    subgraph EKS Cluster
      pod[workload pod]
    end
    database -- security group --> pod
  end

Orchestrator setup

graph LR;
  workload_1 --> db_1["db_1, resource_type: mysql"]
  workload_2 --> db_2["db_2, resource_type: mysql"]
  workload_2 --> shared.db_1["shared.db_1, resource_type: mysql"]
  workload_3 --> shared.db_1["shared.db_1, resource_type: mysql"]  

Terraform docs

Requirements

Name Version
terraform >= 1.3.0
aws ~> 5.0
humanitec ~> 1.0
random ~> 3.5

Providers

Name Version
aws ~> 5.0
humanitec ~> 1.0
random ~> 3.5

Modules

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

Resources

Name Type
aws_iam_role.humanitec_provisioner resource
aws_iam_role_policy_attachment.humanitec_provisioner resource
aws_security_group.mysql resource
aws_vpc_security_group_ingress_rule.k8s_node_mysql resource
humanitec_application.app resource
humanitec_resource_account.humanitec_provisioner resource
humanitec_resource_definition_criteria.mysql resource
random_password.external_id resource
aws_iam_policy_document.instance_assume_role_policy data source

Inputs

Name Description Type Default Required
k8s_node_security_group_id AWS Security Group ID of the kubernetes nodes to allow access to the AWS RDS cluster string n/a yes
region AWS Region to create resources string n/a yes
subnet_ids AWS Subnet IDs to use for the AWS RDS cluster set(string) n/a yes
vpc_id AWS VPC ID string n/a yes
name Name of the example application string "hum-rp-mysql-example" no
prefix Prefix of the created resources string "hum-rp-mysql-ex-" no
resource_packs_aws_rev AWS Resource Pack git branch string "refs/heads/main" no
resource_packs_aws_url AWS Resource Pack git url string "https://github.com/humanitec-architecture/resource-packs-aws.git" no

main.tf (view on GitHub) :

# AWS IAM role used by Humanitec to provision resources

locals {
  admin_policy_arn   = "arn:aws:iam::aws:policy/AdministratorAccess"
  humanitec_user_arn = "arn:aws:iam::767398028804:user/humanitec"
}

resource "random_password" "external_id" {
  length  = 16
  special = false
}

data "aws_iam_policy_document" "instance_assume_role_policy" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type        = "AWS"
      identifiers = [local.humanitec_user_arn]
    }

    condition {
      test     = "StringEquals"
      variable = "sts:ExternalId"
      values   = [random_password.external_id.result]
    }
  }
}

resource "aws_iam_role" "humanitec_provisioner" {
  name = var.name

  assume_role_policy = data.aws_iam_policy_document.instance_assume_role_policy.json
}

resource "aws_iam_role_policy_attachment" "humanitec_provisioner" {
  role       = aws_iam_role.humanitec_provisioner.name
  policy_arn = local.admin_policy_arn
}

resource "humanitec_resource_account" "humanitec_provisioner" {
  id   = var.name
  name = var.name
  type = "aws-role"
  credentials = jsonencode({
    aws_role    = aws_iam_role.humanitec_provisioner.arn
    external_id = random_password.external_id.result
  })

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

# Example application and resource definition criteria

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

module "mysql" {
  source = "github.com/humanitec-architecture/resource-packs-aws//humanitec-resource-defs/mysql/basic"

  resource_packs_aws_url = var.resource_packs_aws_url
  resource_packs_aws_rev = var.resource_packs_aws_rev
  append_logs_to_error   = true
  driver_account         = humanitec_resource_account.humanitec_provisioner.id

  region = var.region

  prefix        = var.prefix
  name          = var.name
  database_name = "my_database"
  username      = "username"
  password      = "password"

  create_db_subnet_group = true
  subnet_ids             = var.subnet_ids

  vpc_security_group_ids = [aws_security_group.mysql.id]
}

resource "humanitec_resource_definition_criteria" "mysql" {
  resource_definition_id = module.mysql.id
  app_id                 = humanitec_application.app.id

  force_delete = true
}

resource "aws_security_group" "mysql" {
  name        = "mysql"
  description = "mysql"
  vpc_id      = var.vpc_id
}

resource "aws_vpc_security_group_ingress_rule" "k8s_node_mysql" {
  security_group_id = aws_security_group.mysql.id

  referenced_security_group_id = var.k8s_node_security_group_id
  from_port                    = 3306
  ip_protocol                  = "tcp"
  to_port                      = 3306
}


providers.tf (view on GitHub) :

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
    humanitec = {
      source  = "humanitec/humanitec"
      version = "~> 1.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.5"
    }
  }

  required_version = ">= 1.3.0"
}

provider "aws" {
  default_tags {
    tags = {
      "managed_by" = "terraform"
      "source"     = "github.com/humanitec-architecture/resource-pack-aws"
    }
  }
}

provider "humanitec" {}

provider "random" {}


terraform.tfvars.example (view on GitHub) :


# AWS Security Group ID of the kubernetes nodes to allow access to the AWS RDS cluster
k8s_node_security_group_id = ""

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

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

# AWS Region to create resources
region = ""

# AWS Resource Pack git branch
resource_packs_aws_rev = "refs/heads/main"

# AWS Resource Pack git url
resource_packs_aws_url = "https://github.com/humanitec-architecture/resource-packs-aws.git"

# AWS Subnet IDs to use for the AWS RDS cluster
subnet_ids = ""

# AWS VPC ID
vpc_id = ""

variables.tf (view on GitHub) :

variable "region" {
  type        = string
  description = "AWS Region to create resources"
}

variable "resource_packs_aws_url" {
  description = "AWS Resource Pack git url"
  type        = string
  default     = "https://github.com/humanitec-architecture/resource-packs-aws.git"
}

variable "resource_packs_aws_rev" {
  description = "AWS Resource Pack git branch"
  type        = string
  default     = "refs/heads/main"
}

variable "vpc_id" {
  description = "AWS VPC ID"
  type        = string
}

variable "subnet_ids" {
  description = "AWS Subnet IDs to use for the AWS RDS cluster"
  type        = set(string)
}

variable "k8s_node_security_group_id" {
  description = "AWS Security Group ID of the kubernetes nodes to allow access to the AWS RDS cluster"
  type        = string
}

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

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

Top