Skip to content

Instantly share code, notes, and snippets.

@greyhoundforty
Last active January 19, 2023 20:27
Show Gist options
  • Save greyhoundforty/5b807ded9102b2f22c5d209803a024bc to your computer and use it in GitHub Desktop.
Save greyhoundforty/5b807ded9102b2f22c5d209803a024bc to your computer and use it in GitHub Desktop.
Sample IBM Cloud VPC with Terraform

providers.tf

Check the IBM Provider page for the latest version available.

terraform {
  required_providers {
    ibm = {
      source  = "IBM-Cloud/ibm"
      version = "1.49.0"
    }
  }
}

provider "ibm" {
  region = var.region
}

variables.tf

variable "prefix" {
  description = "Prefix for all resources"
  type        = string
}

variable "region" {
  description = "Region for all resources"
  type        = string
}

variable "resource_group" {
  description = "Resource group for all resources"
  type        = string
}

variable "owner" {
  description = "Owner of all resources"
  type        = string
}

variable "classic_access" {
  description = "Classic access for all resources"
  type        = bool
  default     = false
}

variable "address_prefix_management" {
  description = "Address prefix management for all resources"
  type        = string
  default     = "auto"
}

main.tf

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

tags = [
    "region:${var.region}",
    "owner:${var.owner}",
    "tf_workspace:${terraform.workspace}"
  ]
}

data "ibm_is_zones" "regional" {
  region = var.region
}

data "ibm_resource_group" "group" {
  name = var.resource_group
}

resource "ibm_is_vpc" "vpc" {
  name                        = "${var.prefix}-vpc"
  resource_group              = data.ibm_resource_group.group.id
  classic_access              = var.classic_access
  address_prefix_management   = var.address_prefix_management
  default_network_acl_name    = "${var.prefix}-default-acl"
  default_security_group_name = "${var.prefix}-default-sg"
  default_routing_table_name  = "${var.prefix}-default-rt"
  tags = local.tags 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment