Skip to content

Introduction

In this guide, you will learn the basics of creating OpenStack resources using Terraform. It shows the deployment process by deploying a couple of instances and at the end of the section under Additional Configuration Files, you will find more examples on how to create external and internal networks, as well as flavors, projects and the same instance deployment example used in the terraform process.

Install Terraform using Homebrew on OS X

  1. Install the HashiCorp tap, a repository of all Homebrew packages.
brew tap hashicorp/tap
  1. Install Terraform
brew install hashicorp/tap/terraform
  1. Verify the installation
terraform -help
terraform version

If you want to install it on another Operating System, review Install Terraform

Terraform Components

Arguments and Blocks

The Terraform language syntax is built around two key syntax constructs: arguments and blocks.

Arguments: an argument assigns a value to a particular name. Example:

image_id = "2eeac780-86ef-40a3-886f-f599927dd6f4"

Blocks: a block is a container for other content. Example: a resource block describes one or more infrastructure objects, such as instances, virtual networks, volumes, files, etc.

Components

Providers

The provider block configures the specified provider, in this case OpenStack. A provider is a plugin that Terraform uses to create and manage your resources.

Resources

Use resource blocks to define components of your infrastructure. A resource might be a virtual component such as an Openstack instance, Virtual Networks or Volumes.

Modules

Terraform refers to a module as a container for multiple resources that are used together. A module is basically a .tf file where you have defined one or more resources. If you want to review more details of Terraform components, review Overview - Configuration Language

Create Configuration Files

Step 1. Create a configuration directory

Each Terraform configuration must be in its own working directory. In this example, there are two working directories one for resources that generally can be provisioned only by the admin and the other directory for tenant resources.

Admin-Resources

Create a directory to provision admin resources such as external networks, routers, images, flavours, tenant projects, quotas, etc.

mkdir   Admin-Resources

Change into the directory

cd Admin-Resources

Inside the Admin folder a child folder is created for each type of resource as follows:

  • flavors
  • images
  • projects
  • external-networks

Tenant-Resources

Create a directory to provisioning tenant resources such as internal networks, instances, flavours, etc.

mkdir   Tenant-Resources

Change into the directory

cd Tenant-Resources

Inside the Tenant folder a child folder is created for each type of resource as follows:

  • internal-network
  • instances

For more reference go to the additional configuration files in the last section.

Step 2. Define the provider

Create a file called providers.tf to define the OpenStack provider and credentials to access the cloud. You can specify the desired or latest terraform-provider- version. Terraform Registry

Registry

vi providers.tf


# TERRAFORM CONFIG

terraform {
  required_version = ">= 1.2.3"
  required_providers {
    openstack = {
      source  = "terraform-provider-openstack/openstack"
      version = "~> 1.51.1"
    }
  }
}

# PROVIDERS

provider "openstack" {
  project_domain_name = "Default"
  user_domain_name    = "Default"
  tenant_name         = "breqwatr"
  user_name           = "brequser1"
  password            = "Test1245!"
  auth_url            = "http://stindev.breqwatr.com:5000/v3"
  endpoint_type       = "publicURL"
  region              = "RegionOne"
}

Step 3. Define input variables

Create a file called variables.tf to define the variables for the deployment.


variable "openstack_project_domain_name" {
  type        = string
  description = "Openstack Project Domain Name"
  default     = "Default"
}

variable "openstack_user_domain_name" {
  type        = string
  description = "Openstack User Domain Name"
  default     = "Default"
}

variable "openstack_tenant_name" {
  type        = string
  description = "Openstack Tenant Name"
  default     = "breqwatr"
}

variable "openstack_user_name" {
  type        = string
  description = "Openstack User Name"
  default     = "brequser1"
}

variable "openstack_password" {
  type        = string
  description = "Openstack Password"
  sensitive   = true
}

variable "openstack_auth_url" {
  type        = string
  description = "Openstack Auth URL"
  default     = "http://stindev.breqwatr.com:5000/v3"
}

variable "openstack_endpoint_type" {
  type        = string
  description = "Openstack Endpoint Type"
  default     = "publicURL"
}

variable "openstack_region" {
  type        = string
  description = "Openstack Region"
  default     = "RegionOne"
}

You can now update the OpenStack provider values with the variables defined in the variables.tf file

##################################################################################
# PROVIDERS
##################################################################################

provider "openstack" {
  project_domain_name = var.openstack_project_domain_name
  user_domain_name    = var.openstack_user_domain_name
  tenant_name         = var.openstack_tenant_name
  user_name           = var.openstack_user_name
  password            = var.openstack_password
  auth_url            = var.openstack_auth_url
  endpoint_type       = var.openstack_endpoint_type
  region              = var.openstack_region
}

Step 4. Define the instance deployment

Create a file called main.tf to define the resource to inject the public key that we are using for the newly deployed instances and push it to OpenStack.


# PUBLIC KEY

resource "openstack_compute_keypair_v2" "ssh_key" {
  name       = "ssh_key"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDLFFx4fLeNeLLJnhGaiWkaXGjBkMK1DXNibwYvn4BdU3GXYvSLSN5poSRwtkcgswn+G6wWbb16BdNvWyf4rYqhUWWF1OBODYfy77kwY8Se/fwtt6Z+0t8ua4bC7hVhmVo1k3pDlg1iPqAx4bO0/IO/CleYciDayrA5LiDrlnM0Hr8Q+Ok+F4k26ACV5YXYeHrhd0wBQ4RmNqCoG0iaEW5Ff2nUwgm15guPFREXHP+AQ3gT0+1bNyThUiW93qwStDL226l652U6TpYLznf3cQqcI/qnNzfeQjpTatfOclgffc6DtD/PRaHp9xk4Fu94cUGNN2eJQiy9RZPOcYew4u0LDZBEKzDO97ycQdk5+P/xJ+F8sNl8gqCIXzDldJ7ZmusbBUaPyBYBIyGGUoh880Qqsin0CAs+LUP8bS0l7+hjcchvjzfBAcbJQ4d0k8GkWZs5p5S5wLmQUdN1odpgkSVS97q1aNjuUTAe0GQMjEyn2sfu0MAixr+flkTdihA8CnE= ruth@MacBook-Pro.local"
}

Update the variables.tf file to add the variables associated to the resource to deploy two instances.

variable "flavor_name" {
  type        = string
  description = "Flavor Name"
  default     = "tiny-0"
}

variable "image_id" {
  type        = string
  description = "Image ID"
  default     = "2eeac780-86ef-40a3-886f-f599927dd6f4"
}

variable "openstack_blockstorage_volume_v3_volume_size" {
  type        = number
  description = "Volume Size (GB)"
  default     = 2
}

variable "vlan_name" {
  type        = string
  description = "VLAN Name"
  default     = "private-net"
}

Define the name of the server, flavour and other arguments to deploy an instance. The script below will deploy two instances.


# INSTANCES

resource "openstack_compute_instance_v2" "cirros" {
  count           = 2
  name            = "cirros-${count.index}"
  flavor_name     = var.flavor_name
  key_pair        = openstack_compute_keypair_v2.ssh_key.name
  security_groups = ["default"]

  block_device {

    uuid                  = var.image_id
    source_type           = "image"
    volume_size           = var.openstack_blockstorage_volume_v3_volume_size
    boot_index            = 0
    destination_type      = "volume"
    delete_on_termination = true

  }

  metadata = {
    Imported_VM = true
    Owner       = "John Smith"
  }

  network {
    name = var.vlan_name
  }

}

Initialize the Directory

When you create a new configuration — or check out an existing configuration from version control — you need to initialize the directory with terraform init.

Initializing a configuration directory downloads and installs the providers defined in the configuration, which in this case is the OpenStack provider.

Initialize the directory.

~/terraform-deploy-instance> terraform init

Initialize_1

Terraform downloads the OpenStack provider and installs it in a hidden subdirectory of your current working directory, named The terraform init command prints out which version of the provider was installed. Terraform also creates a lock file named .terraform.lock.hcl which specifies the exact provider versions used, so that you can control when you want to update the providers used for your project.

Initialize_2

Format and validate the configuration

We recommend using consistent formatting in all of your configuration files. The terraform fmt command automatically updates configurations in the current directory for readability and consistency.

Format your configuration. Terraform will print out the names of the files it modified, if any. In this case, your configuration file was already formatted correctly, so Terraform won't return any file names.

~/terraform-deploy-instance> terraform fmt

You can also make sure your configuration is syntactically valid and internally consistent by using the terraform validate command.

Validate your configuration. The example configuration provided above is valid, so Terraform will return a success message.

~/terraform-deploy-instance> terraform validate

Plan the deployment

The command terraform plan is a dry run of all configuration files(.tf files) that are in that folder and does not make any real changes to your resources or state. Instead, a terraform plan is used to create an execution plan. Terraform performs a refresh and then determines what actions are necessary to achieve the desired state. In the example below, we are specifying the OpenStack password to access the cloud and the plan file.

~/terraform-deploy-instance> terraform plan -var=openstack_password="Test1245!" -out terraform-deploy-instance.tfplan

Plan_1

At the end of the output it will show the command to apply the changes terraform apply "terraform-deploy-instance.tfplan"

Plan_2

Deploy the instances

Type in the following command to make the desired changes to your infrastructure:

~/terraform-deploy-instance> terraform apply "terraform-deploy-instance.tfplan"

Deploy_1

You can confirm the creation of the instances either on the breqwatr portal or via OpenStack.

Deploy_2

Deploy_3

Destroy the instances (Optional)

If you want to run some tests or just want to see if this tutorial works, Terraform has an easy way to clean up the infra you have just deployed.

Simply run the following command:

~/terraform-deploy-instance> terraform destroy

It will prompt for your confirmation enter the value yes to destroy the resources created.

Destroy_1

Additional Configuration Files

Admin-Resources

Flavors

main.tf

# OpenStack Flavors

resource "openstack_compute_flavor_v2" "tiny-1-flavor" {
  name      = var.tiny-1-name
  ram       = var.ram_mb_1024
  vcpus     = var.vcpu_1
  disk      = var.disk_20
  is_public = var.flavor_is_public
}

resource "openstack_compute_flavor_v2" "tiny-2-flavor" {
  name      = var.tiny-2-name
  ram       = var.ram_mb_2048
  vcpus     = var.vcpu_1
  disk      = var.disk_20
  is_public = var.flavor_is_public
}

resource "openstack_compute_flavor_v2" "small-1-flavor" {
  name      = var.small-1-name
  ram       = var.ram_mb_2048
  vcpus     = var.vcpu_2
  disk      = var.disk_20
  is_public = var.flavor_is_public
}

resource "openstack_compute_flavor_v2" "small-2-flavor" {
  name      = var.small-2-name
  ram       = var.ram_mb_4096
  vcpus     = var.vcpu_2
  disk      = var.disk_20
  is_public = var.flavor_is_public
}

resource "openstack_compute_flavor_v2" "small-3-flavor" {
  name      = var.small-3-name
  ram       = var.ram_mb_8192
  vcpus     = var.vcpu_2
  disk      = var.disk_20
  is_public = var.flavor_is_public
}

resource "openstack_compute_flavor_v2" "medium-1-flavor" {
  name      = var.medium-1-name
  ram       = var.ram_mb_16384
  vcpus     = var.vcpu_2
  disk      = var.disk_40
  is_public = var.flavor_is_public
}

resource "openstack_compute_flavor_v2" "medium-2-flavor" {
  name      = var.medium-2-name
  ram       = var.ram_mb_8192
  vcpus     = var.vcpu_4
  disk      = var.disk_40
  is_public = var.flavor_is_public
}

resource "openstack_compute_flavor_v2" "medium-3-flavor" {
  name      = var.medium-3-name
  ram       = var.ram_mb_16384
  vcpus     = var.vcpu_4
  disk      = var.disk_40
  is_public = var.flavor_is_public
}

resource "openstack_compute_flavor_v2" "large-1-flavor" {
  name      = var.large-1-name
  ram       = var.ram_mb_32768
  vcpus     = var.vcpu_4
  disk      = var.disk_40
  is_public = var.flavor_is_public
}

resource "openstack_compute_flavor_v2" "large-2-flavor" {
  name      = var.large-2-name
  ram       = var.ram_mb_65536
  vcpus     = var.vcpu_8
  disk      = var.disk_40
  is_public = var.flavor_is_public
}

variables.tf

## Flavor names

variable "tiny-1-name" {
  type        = string
  description = "Openstack Flavor - tiny-1"
  default     = "tiny-1"
}

variable "tiny-2-name" {
  type        = string
  description = "Openstack Flavor - tiny-2"
  default     = "tiny-2"
}

variable "small-1-name" {
  type        = string
  description = "Openstack Flavor - small-1"
  default     = "small-1"
}

variable "small-2-name" {
  type        = string
  description = "Openstack Flavor - small-2"
  default     = "small-2"
}

variable "small-3-name" {
  type        = string
  description = "Openstack Flavor - small-3"
  default     = "small-3"
}

variable "medium-1-name" {
  type        = string
  description = "Openstack Flavor - medium-1"
  default     = "medium-1"
}

variable "medium-2-name" {
  type        = string
  description = "Openstack Flavor - medium-2"
  default     = "medium-2"
}

variable "medium-3-name" {
  type        = string
  description = "Openstack Flavor - medium-3"
  default     = "medium-3"
}

variable "large-1-name" {
  type        = string
  description = "Openstack Flavor - large-1"
  default     = "large-1"
}

variable "large-2-name" {
  type        = string
  description = "Openstack Flavor - large-2"
  default     = "large-2"
}

## RAM values

variable "ram_mb_1024" {
  type        = number
  description = "Openstack Flavor - 1024 ram_mb"
  default     = 1024
}

variable "ram_mb_2048" {
  type        = number
  description = "Openstack Flavor - 2048 ram_mb"
  default     = 2048
}

variable "ram_mb_4096" {
  type        = number
  description = "Openstack Flavor - 4096 ram_mb"
  default     = 4096
}

variable "ram_mb_8192" {
  type        = number
  description = "Openstack Flavor - 8192 ram_mb"
  default     = 8192
}

variable "ram_mb_16384" {
  type        = number
  description = "Openstack Flavor - 16384 ram_mb"
  default     = 16384
}

variable "ram_mb_32768" {
  type        = number
  description = "Openstack Flavor - 32768 ram_mb"
  default     = 32768
}

variable "ram_mb_65536" {
  type        = number
  description = "Openstack Flavor - 65536 ram_mb"
  default     = 65536
}

## vCPU values

variable "vcpu_1" {
  type        = number
  description = "Openstack Flavor - 1 vcpu"
  default     = 1
}

variable "vcpu_2" {
  type        = number
  description = "Openstack Flavor - 2 vcpus"
  default     = 2
}

variable "vcpu_4" {
  type        = number
  description = "Openstack Flavor - 4 vcpus"
  default     = 4
}

variable "vcpu_8" {
  type        = number
  description = "Openstack Flavor - 8 vcpus"
  default     = 8
}

## vDisk values

variable "disk_20" {
  type        = number
  description = "Openstack Flavor - 20 disk_gb"
  default     = 20
}

variable "disk_40" {
  type        = number
  description = "Openstack Flavor - 40 disk_gb"
  default     = 40
}

## Visibility

variable "flavor_is_public" {
  type        = bool
  description = "Openstack Flavor is visible on all projects"
  default     = true
}

variable "flavor_is_not_public" {
  type        = bool
  description = "Openstack Flavor is not visible on all projects"
  default     = false
}

Project

main.tf

# OpenStack Project

resource "openstack_identity_project_v3" "breqwatr-project" {
  name        = var.breqwatr-project-name
  description = var.breqwatr-project-description
}

# OpenStack User

resource "openstack_identity_user_v3" "breqwatr-user" {
  name               = var.breqwatr-user-name
  description        = var.breqwatr-user-description
  default_project_id = openstack_identity_project_v3.breqwatr-project.id
  password           = var.breqwatr-user-password
  extra = {
    email = var.breqwatr-user-email
  }
}

# Group Membership and Role assignment

resource "openstack_identity_group_v3" "breqwatr-admins" {
  name        = var.breqwatr-admins-group
  description = var.breqwatr-admins-group-description
}

data "openstack_identity_role_v3" "admin" {
  name = var.admin-role-name
}

resource "openstack_identity_user_membership_v3" "breqwatr-user-group-membership" {
  user_id  = openstack_identity_user_v3.breqwatr-user.id
  group_id = openstack_identity_group_v3.breqwatr-admins.id
}


resource "openstack_identity_role_assignment_v3" "breqwatr-admins-role-assignment" {
  group_id   = openstack_identity_group_v3.breqwatr-admins.id
  project_id = openstack_identity_project_v3.breqwatr-project.id
  role_id    = data.openstack_identity_role_v3.admin.id
}

# Server Quota

resource "openstack_compute_quotaset_v2" "breqwatr-server-quota" {
  project_id = openstack_identity_project_v3.breqwatr-project.id
  instances  = var.unlimited
  cores      = var.unlimited
  ram        = var.unlimited
}

# Volume Quota

resource "openstack_blockstorage_quotaset_v3" "breqwatr-volume-quota" {
  project_id           = openstack_identity_project_v3.breqwatr-project.id
  volumes              = var.unlimited
  snapshots            = var.unlimited
  gigabytes            = var.unlimited
  per_volume_gigabytes = var.unlimited
  backups              = var.unlimited
  backup_gigabytes     = var.unlimited
  groups               = var.unlimited
}

variables.tf

## Project

variable "breqwatr-project-name" {
  type        = string
  description = "Breqwatr Project"
  default     = "breqwatr-project"
}

variable "breqwatr-project-description" {
  type        = string
  description = "Breqwatr Project"
  default     = "Default project for Breqwatr"
}

## User

variable "breqwatr-user-name" {
  type        = string
  description = "Breqwatr User"
  default     = "breqwatr-user"
}

variable "breqwatr-user-description" {
  type        = string
  description = "Breqwatr User"
  default     = "Breqwatr User"
}

variable "breqwatr-user-password" {
  type        = string
  description = "Breqwatr User's password"
  default     = "Bsles78"
}

variable "breqwatr-user-email" {
  type        = string
  description = "Breqwatr User's email"
  default     = "breqwatr-user@local"
}

## Group

variable "breqwatr-admins-group" {
  type        = string
  description = "Breqwatr Admins Group"
  default     = "breqwatr-admins"
}

variable "breqwatr-admins-group-description" {
  type        = string
  description = "Breqwatr Admins Group"
  default     = "Breqwatr Admins Group"
}

# Role

variable "admin-role-name" {
  type        = string
  description = "Admin Role"
  default     = "admin"
}

# Quota

variable "unlimited" {
  type        = number
  description = "Unlimited"
  default     = -1
}
External Networks

main.tf

# OpenStack External Network

resource "openstack_networking_network_v2" "VLAN23-net" {
  name           = var.VLAN23-net-name
  description    = var.VLAN23-net-description
  admin_state_up = var.enabled
  shared         = var.enabled
  external       = var.enabled
  mtu            = var.VLAN23-net-mtu
  segments {
    network_type     = var.VLAN23-net-network_type
    physical_network = var.VLAN23-net-physical_network
  }
}

# Openstack External Subnet

resource "openstack_networking_subnet_v2" "VLAN23-subnet" {
  name            = var.VLAN23-subnet-name
  description     = var.VLAN23-subnet-description
  network_id      = openstack_networking_network_v2.VLAN23-net.id
  cidr            = var.VLAN23-subnet-cidr
  gateway_ip      = var.VLAN23-subnet-gateway_ip
  enable_dhcp     = var.enabled
  dns_nameservers = var.VLAN23-subnet-dns_nameservers
  allocation_pool {
    start = var.VLAN23-subnet-allocation_pool_start
    end   = var.VLAN23-subnet-allocation_pool_end
  }
}

# Openstack Security Group & Rules

data "openstack_identity_project_v3" "secgroup-breqwatr-project" {
  name = var.breqwatr-project
}

data "openstack_networking_secgroup_v2" "secgroup-default-breqwatr" {
  name      = var.breqwatr-project-security-group-name
  tenant_id = data.openstack_identity_project_v3.secgroup-breqwatr-project.id
}

resource "openstack_networking_secgroup_rule_v2" "secgroup-rule-default-breqwatr-tcp" {
  direction         = var.direction-ingress
  ethertype         = var.ethertype-IPv4
  protocol          = var.protocol-tcp
  port_range_min    = var.tcp-udp-port-range-min
  port_range_max    = var.tcp-udp-port-range-max
  remote_ip_prefix  = var.tcp-udp-remote-ip-prefix
  security_group_id = data.openstack_networking_secgroup_v2.secgroup-default-breqwatr.id
}

resource "openstack_networking_secgroup_rule_v2" "secgroup-rule-default-breqwatr-udp" {
  direction         = var.direction-ingress
  ethertype         = var.ethertype-IPv4
  protocol          = var.protocol-udp
  port_range_min    = var.tcp-udp-port-range-min
  port_range_max    = var.tcp-udp-port-range-max
  remote_ip_prefix  = var.tcp-udp-remote-ip-prefix
  security_group_id = data.openstack_networking_secgroup_v2.secgroup-default-breqwatr.id
}

resource "openstack_networking_secgroup_rule_v2" "secgroup_rule_icmp" {
  direction         = var.direction-ingress
  ethertype         = var.ethertype-IPv4
  protocol          = var.protocol-icmp
  security_group_id = data.openstack_networking_secgroup_v2.secgroup-default-breqwatr.id
}

variables.tf

## External Network

variable "VLAN23-net-name" {
  type        = string
  description = "External Network - VLAN23-net"
  default     = "VLAN23-net"
}

variable "VLAN23-net-description" {
  type        = string
  description = "External Network - VLAN23-net"
  default     = "External Network - VLAN23-net"
}

variable "VLAN23-net-mtu" {
  type        = number
  description = "External Network MTU- VLAN23-net"
  default     = 1500
}

variable "VLAN23-net-network_type" {
  type        = string
  description = "External Network Type - VLAN23-net"
  default     = "flat"
}

variable "VLAN23-net-physical_network" {
  type        = string
  description = "External Physical Network - VLAN23-net"
  default     = "physnet1"
}

## External Subnet

variable "VLAN23-subnet-name" {
  type        = string
  description = "External Subnet - VLAN23-subnet"
  default     = "VLAN23-subnet"
}

variable "VLAN23-subnet-description" {
  type        = string
  description = "External Subnet - VLAN23-subnet"
  default     = "External Subnet - VLAN23-subnet"
}

variable "VLAN23-subnet-cidr" {
  type        = string
  description = "External Subnet cidr - VLAN23-subnet"
  default     = "192.160.23.0/24"
}

variable "VLAN23-subnet-gateway_ip" {
  type        = string
  description = "External Subnet gateway_ip - VLAN23-subnet"
  default     = "192.160.23.1"
}

variable "VLAN23-subnet-dns_nameservers" {
  type        = list(string)
  description = "External Subnet dns_nameservers - VLAN23-subnet"
  default     = ["8.8.8.8", "8.8.4.4"]
}

variable "VLAN23-subnet-allocation_pool_start" {
  type        = string
  description = "External Subnet allocation_pool - VLAN23-subnet"
  default     = "192.160.23.130"
}

variable "VLAN23-subnet-allocation_pool_end" {
  type        = string
  description = "External Subnet allocation_pool - VLAN23-subnet"
  default     = "192.160.23.160"
}

variable "enabled" {
  type        = bool
  description = "The argument is enabled"
  default     = "true"
}

variable "disabled" {
  type        = bool
  description = "The argument is disabled"
  default     = "false"
}

## Security Group

variable "breqwatr-project" {
  type        = string
  description = "Breqwatr Project"
  default     = "breqwatr-project"
}

variable "breqwatr-project-security-group-name" {
  type        = string
  description = "Breqwatr Project - Security Group"
  default     = "default"
}

variable "breqwatr-project-security-group-id" {
  type        = string
  description = "Breqwatr Project - Security Group"
  default     = "breqwatr-project"
}

## Rules

variable "direction-ingress" {
  type        = string
  description = "Direction"
  default     = "ingress"
}

variable "ethertype-IPv4" {
  type        = string
  description = "IPv4"
  default     = "IPv4"
}

variable "protocol-tcp" {
  type        = string
  description = "tcp"
  default     = "tcp"
}

variable "protocol-udp" {
  type        = string
  description = "udp"
  default     = "udp"
}

variable "protocol-icmp" {
  type        = string
  description = "icmp"
  default     = "icmp"
}

variable "tcp-udp-port-range-min" {
  type        = number
  description = "TCP/UDP port range min"
  default     = 1
}

variable "tcp-udp-port-range-max" {
  type        = number
  description = "TCP/UDP port range min"
  default     = 65535
}

variable "tcp-udp-remote-ip-prefix" {
  type        = string
  description = "TCP/UDP remote ip prefix"
  default     = "0.0.0.0/0"
}

Tenant-Resources

Internal Networks

main.tf

# OpenStack Internal Network

resource "openstack_networking_network_v2" "breqwatr-network" {
  name           = var.breqwatr-net-name
  admin_state_up = var.enabled
  segments {
    network_type = var.breqwatr-net-network_type
  }
}

# Openstack Internal Subnet

resource "openstack_networking_subnet_v2" "breqwatr-subnet" {
  name            = var.breqwatr-subnet-name
  description     = var.breqwatr-subnet-description
  network_id      = openstack_networking_network_v2.breqwatr-network.id
  cidr            = var.breqwatr-subnet-cidr
  gateway_ip      = var.breqwatr-subnet-gateway_ip
  enable_dhcp     = var.enabled
  dns_nameservers = var.breqwatr-subnet-dns_nameservers
  allocation_pool {
    start = var.breqwatr-subnet-allocation_pool_start
    end   = var.breqwatr-subnet-allocation_pool_end
  }
}

# Openstack Router

data "openstack_networking_network_v2" "VLAN23-network" {
  name = var.VLAN23-net-name
}

resource "openstack_networking_router_v2" "breqwatr-router" {
  name                = var.breqwatr-router-name
  description         = var.breqwatr-router-description
  admin_state_up      = var.enabled
  external_network_id = data.openstack_networking_network_v2.VLAN23-network.id
  enable_snat         = var.enabled
}

data "openstack_networking_router_v2" "breqwatr-router" {
  name = var.breqwatr-router-name

  depends_on = [
    openstack_networking_router_v2.breqwatr-router
  ]
}

data "openstack_networking_subnet_v2" "breqwatr-subnet" {
  name = var.breqwatr-subnet-name

  depends_on = [
    openstack_networking_subnet_v2.breqwatr-subnet
  ]
}

## breqwatr router interface

resource "openstack_networking_router_interface_v2" "breqwatr-router-interface" {
  router_id = data.openstack_networking_router_v2.breqwatr-router.id
  subnet_id = data.openstack_networking_subnet_v2.breqwatr-subnet.id
}

variables.tf

## External Network

variable "VLAN23-net-name" {
  type        = string
  description = "External Network - VLAN23-net"
  default     = "VLAN23-net"
}

## Internal Network

variable "breqwatr-net-name" {
  type        = string
  description = "Internal Network - breqwatr-net"
  default     = "breqwatr-net"
}

variable "breqwatr-net-description" {
  type        = string
  description = "Internal Network - breqwatr-net"
  default     = "Internal Network - breqwatr-net"
}

variable "breqwatr-net-network_type" {
  type        = string
  description = "Internal Network Type - breqwatr-net"
  default     = "vxlan"
}

variable "enabled" {
  type        = bool
  description = "The argument is enabled"
  default     = "true"
}

variable "disabled" {
  type        = bool
  description = "The argument is disabled"
  default     = "false"
}

## Internal Subnet

variable "breqwatr-subnet-name" {
  type        = string
  description = "Internal Subnet - breqwatr-subnet"
  default     = "breqwatr-subnet"
}

variable "breqwatr-subnet-description" {
  type        = string
  description = "Internal Subnet - breqwatr-subnet"
  default     = "Internal Subnet - breqwatr-subnet"
}

variable "breqwatr-subnet-cidr" {
  type        = string
  description = "Internal Subnet cidr - breqwatr-subnet"
  default     = "172.16.0.0/24"
}

variable "breqwatr-subnet-gateway_ip" {
  type        = string
  description = "Internal Subnet gateway_ip - breqwatr-subnet"
  default     = "172.16.0.1"
}

variable "breqwatr-subnet-dns_nameservers" {
  type        = list(string)
  description = "Internal Subnet dns_nameservers - breqwatr-subnet"
  default     = ["8.8.8.8", "8.8.4.4"]
}

variable "breqwatr-subnet-allocation_pool_start" {
  type        = string
  description = "Internal Subnet allocation_pool - breqwatr-subnet"
  default     = "172.16.0.2"
}

variable "breqwatr-subnet-allocation_pool_end" {
  type        = string
  description = "Internal Subnet allocation_pool - breqwatr-subnet"
  default     = "172.16.0.254"
}

## Router

variable "breqwatr-router-name" {
  type        = string
  description = "Router - breqwatr-router"
  default     = "breqwatr-router"
}

variable "breqwatr-router-description" {
  type        = string
  description = "Router  - breqwatr-router"
  default     = "Router  - breqwatr-router"
}
Instances

main.tf

# Public Key

resource "openstack_compute_keypair_v2" "ssh_key" {
  name       = "ssh_key"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDLFFx4fLeNeLLJnhGaiWkaXGjBkMK1DXNibwYvn4BdU3GXYvSLSN5poSRwtkcgswn+G6wWbb16BdNvWyf4rYqhUWWF1OBODYfy77kwY8Se/fwtt6Z+0t8ua4bC7hVhmVo1k3pDlg1iPqAx4bO0/IO/CleYciDayrA5LiDrlnM0Hr8Q+Ok+F4k26ACV5YXYeHrhd0wBQ4RmNqCoG0iaEW5Ff2nUwgm15guPFREXHP+AQ3gT0+1bNyThUiW93qwStDL226l652U6TpYLznf3cQqcI/qnEERESSSNzfeQjpTatfOclgffc6DtD/seresERE+P/xJ+F8sNl8gqCIXzDldJ7ZmusbBUaPyBYBIyGGUoh880Qqsin0CAs+LUP8bS0l7+hjcchvjzfBAcbJQ4d0k8GkWZs5p5S5wLmQUdN1odpgkSVS97q1aNjuUTAe0GQMjEyn2sfu0MAixr+flkTdihA8CnE= breqwatr@MacBook-Pro.local"
}

# Instance

resource "openstack_compute_instance_v2" "cirros" {
  count           = 2
  name            = "cirros-${count.index}"
  flavor_name     = var.flavor_name
  key_pair        = openstack_compute_keypair_v2.ssh_key.name
  security_groups = ["default"]

  block_device {

    uuid                  = var.image_name
    source_type           = "image"
    volume_size           = var.openstack_blockstorage_volume_v3_volume_size
    boot_index            = 0
    destination_type      = "volume"
    delete_on_termination = true

  }

  metadata = {
    Imported_VM = true
    Owner       = "John Smith"
  }

  network {
    name = var.vlan_name
  }

}

variables.tf

## Instance

variable "flavor_name" {
  type        = string
  description = "Flavor Name"
  default     = "tiny-1"
}

variable "image_name" {
  type        = string
  description = "Image Name"
  default     = "7d962e24-2436-457d-9161-178c342d4db4"
}

variable "openstack_blockstorage_volume_v3_volume_size" {
  type        = number
  description = "Volume Size (GB)"
  default     = 2
}

variable "vlan_name" {
  type        = string
  description = "VLAN Name"
  default     = "breqwatr-net"
}