Skip to content

Instantly share code, notes, and snippets.

@greyhoundforty
Created February 20, 2023 23:03
Show Gist options
  • Save greyhoundforty/a285bc9ad1404683eed02f0a8e44b30a to your computer and use it in GitHub Desktop.
Save greyhoundforty/a285bc9ad1404683eed02f0a8e44b30a to your computer and use it in GitHub Desktop.
VPC GPU with Terraform

data.tf

data "ibm_is_ssh_key" "sshkey" {
  name = var.existing_ssh_key
}

# Pull in the zones in the region
data "ibm_is_zones" "regional" {
  region = var.region
}

data "ibm_is_image" "base" {
  name = var.image_name
}

variables.tf

variable "prefix" {
  type        = string
  description = "Name to be prepended to all resources"
}

variable "region" {
  type        = string
  description = "Region to deploy the resource"
  default     = "jp-osa"
}

variable "existing_resource_group" {
  type        = string
  description = "Name of the existing resource group"
  default     = "CDE"
}

variable "existing_ssh_key" {
  type        = string
  description = "Name of the existing ssh key"
}

variable "owner" {}


variable "image_name" {
  description = "The name of an existing OS image to use. You can list available images with the command 'ibmcloud is images'."
  type        = string
  default     = "ibm-ubuntu-22-04-1-minimal-amd64-3"
}

main.tf

locals {
  tags = [
    "owner:${var.owner}",
    "provider:ibm",
    "region:${var.region}"
  ]

  zones = length(data.ibm_is_zones.regional.zones)
  vpc_zones = {
    for zone in range(local.zones) : zone => {
      zone = "${var.region}-${zone + 1}"
    }
  }
}

module "resource_group" {
  source                       = "git::https://github.com/terraform-ibm-modules/terraform-ibm-resource-group.git?ref=v1.0.5"
  resource_group_name          = var.existing_resource_group == null ? "${var.prefix}-resource-group" : null
  existing_resource_group_name = var.existing_resource_group
}

module "vpc" {
  source = "terraform-ibm-modules/vpc/ibm//modules/vpc"

  create_vpc                  = true
  vpc_name                    = "${var.prefix}-vpc"
  resource_group_id           = module.resource_group.resource_group_id
  classic_access              = false
  default_address_prefix      = "auto"
  default_network_acl_name    = "${var.prefix}-default-network-acl"
  default_security_group_name = "${var.prefix}-default-security-group"
  default_routing_table_name  = "${var.prefix}-default-routing-table"
  vpc_tags                    = local.tags
  locations                   = [local.vpc_zones[0].zone]
  subnet_name                 = "${var.prefix}-subnet"
  number_of_addresses         = 64
  create_gateway              = true
  public_gateway_name         = "${var.prefix}-pub-gw"
  gateway_tags                = local.tags
}

resource "ibm_is_instance" "gpu" {
  name                     = "${var.prefix}-gpu"
  vpc                      = module.vpc.vpc_id[0]
  image                    = data.ibm_is_image.base.id
  profile                  = "gx2-8x64x1v100"
  resource_group           = module.resource_group.resource_group_id
  metadata_service_enabled = true

  boot_volume {
    name = "${var.prefix}-boot-volume"
  }

  primary_network_interface {
    subnet            = module.vpc.subnet_ids[0]
    allow_ip_spoofing = true
    security_groups   = [module.vpc.vpc_default_security_group[0]]
  }

  zone = local.vpc_zones[0].zone
  keys = [data.ibm_is_ssh_key.sshkey.id]
  tags = concat(local.tags, ["zone:${local.vpc_zones[0].zone}"])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment