Skip to content

Conversation

Pearl1594
Copy link
Contributor

@Pearl1594 Pearl1594 commented Aug 29, 2025

Configuration:

terraform {
  required_providers {
    cloudstack = {
      source = "local/dev/cloudstack"
    }
  }
}

provider "cloudstack" {
  api_url    = "http://xx.xx.xx.xx:8080/client/api"
  api_key    = "LIN6rqXuaJwMPfGYFh13qDwYz5VNNz1J2J6qIOWcd3oLQOq0WtD4CwRundBL6rzXToa3lQOC_vKjI3nkHtiD8Q"
  secret_key = "R6QPwRUz09TVXBjXNwZk7grTjcPtsFRphH6xhN1oPvnc12YUk296t4KHytg8zRLczDA0X5NsLVi4d8rfMMx3yg"
  timeout    = 1800  # 30 minutes for template registration
}

resource "cloudstack_network" "autoscale_network" {
  name             = "autoscale-isolated-network"
  display_text     = "Isolated network for autoscale VMs"
  cidr             = "10.0.10.0/24"
  network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
  zone             = "ref-trl-9240-k-Mol8-pearl-dsilva"
}

resource "cloudstack_ipaddress" "lb_public_ip" {
  network_id = cloudstack_network.autoscale_network.id
  zone       = "ref-trl-9240-k-Mol8-pearl-dsilva"
}

resource "cloudstack_firewall" "http_firewall" {
  ip_address_id = cloudstack_ipaddress.lb_public_ip.id

  rule {
    cidr_list = ["0.0.0.0/0"]
    protocol  = "tcp"
    ports     = ["80"]
  }
}

resource "cloudstack_loadbalancer_rule" "autoscale_lb" {
  name          = "autoscale-lb-rule"
  description   = "Load balancer rule for autoscale group"
  ip_address_id = cloudstack_ipaddress.lb_public_ip.id
  network_id    = cloudstack_network.autoscale_network.id
  algorithm     = "roundrobin"
  private_port  = 80
  public_port   = 80
  member_ids    = []
}

resource "cloudstack_instance" "initial_vm" {
  name             = "initial-web-server"
  service_offering = "Small Instance"
  template         = cloudstack_template.ubuntu_template.name
  zone             = "ref-trl-9240-k-Mol8-pearl-dsilva"
  network_id       = cloudstack_network.autoscale_network.id
}

 resource "cloudstack_template" "ubuntu_template" {
  name         = "ubuntu-22.04-cks"
  display_text = "Ubuntu 22.04 CKS Template"
  format       = "QCOW2"
  hypervisor   = "KVM"
  os_type      = "Ubuntu 22.04 LTS"
  url          = "http://10.0.3.130/cks/custom_templates/ubuntu/22.04/cks-ubuntu-2204-kvm.qcow2.bz2"
  zone         = "ref-trl-9240-k-Mol8-pearl-dsilva"
  is_extractable = true
  is_featured    = false
  is_public      = true
  is_ready_timeout = 1800
}

data "cloudstack_counter" "existing_cpu_counter" {
  id = "cec1072a-784e-11f0-bc3a-1e0030000242"
}

resource "cloudstack_condition" "scale_up_condition" {
  counter_id          = data.cloudstack_counter.existing_cpu_counter.id
  relational_operator = "GT"
  threshold           = 80.0
  account_name        = "admin"
  domain_id           = "1"
}

resource "cloudstack_condition" "scale_down_condition" {
  counter_id          = data.cloudstack_counter.existing_cpu_counter.id
  relational_operator = "LT"
  threshold           = 20.0
  account_name        = "admin"
  domain_id           = "1"
}

resource "cloudstack_autoscale_policy" "scale_up_policy" {
  name         = "scale-up"
  action       = "scaleup"
  duration     = 300
  quiet_time   = 300
  condition_ids = [cloudstack_condition.scale_up_condition.id]
}

resource "cloudstack_autoscale_policy" "scale_down_policy" {
  name         = "scale-down"
  action       = "scaledown"
  duration     = 300
  quiet_time   = 300
  condition_ids = [cloudstack_condition.scale_down_condition.id]
}

resource "cloudstack_autoscale_vm_profile" "vm_profile" {
  service_offering         = "Small Instance"
  template                = cloudstack_template.ubuntu_template.name
  zone                    = "ref-trl-9240-k-Mol8-pearl-dsilva"
  destroy_vm_grace_period = "30s"
  
  counter_param_list = {
    "snmpcommunity" = "public"
    "snmpport"      = "161"
  }
  
  user_data = base64encode(<<-EOF
    #!/bin/bash
    apt-get update -y

    apt-get install -y apache2
    a2enmod cgi

    sed -i -e '/Options Indexes FollowSymLinks/s/$/ ExecCGI/' \
           -e 's/DirectoryIndex index.html/DirectoryIndex index.py/' /etc/apache2/apache2.conf

    cat << EOFCGI > /etc/apache2/conf-available/python-cgi.conf
    <Directory "/var/www/html">
        AddHandler cgi-script .py
        Options +ExecCGI
    </Directory>
    EOFCGI

    a2enconf python-cgi

    cat << 'EOFPY' > /var/www/html/index.py
    #!/usr/bin/env python3
    import time
    import socket
    time.sleep(5)
    print("Content-type: text/html\n\n")
    print('<p style="text-align: center;">Hello World!!!</p>')
    print('<p style="text-align: center;"><strong>Instance:</strong> {}</p>'.format(socket.gethostname()))
    EOFPY

    chmod 705 /var/www/html/index.py

    systemctl enable apache2
    systemctl restart apache2
    EOF
  )
  
  user_data_details = {
    "environment" = "autoscale"
    "application" = "web-server"
  }
  
  account_name     = "Admin"
  domain_id        = "1"
  
  display          = true
  
  other_deploy_params = {
    "rootdisksize" = "20"
  }
}

resource "cloudstack_autoscale_vm_group" "vm_group" {
  name             = "simple-autoscale-group"
  lbrule_id       = cloudstack_loadbalancer_rule.autoscale_lb.id
  min_members     = 1
  max_members     = 5
  vm_profile_id   = cloudstack_autoscale_vm_profile.vm_profile.id
  
  scaleup_policy_ids = [
    cloudstack_autoscale_policy.scale_up_policy.id
  ]
  
  scaledown_policy_ids = [
    cloudstack_autoscale_policy.scale_down_policy.id
  ]
}

output "network_id" {
  description = "ID of the created isolated network"
  value       = cloudstack_network.autoscale_network.id
}

output "network_cidr" {
  description = "CIDR of the isolated network"
  value       = cloudstack_network.autoscale_network.cidr
}

output "public_ip_address" {
  description = "Public IP address for the load balancer"
  value       = cloudstack_ipaddress.lb_public_ip.ip_address
}

output "load_balancer_url" {
  description = "URL to access the load balanced application"
  value       = "http://${cloudstack_ipaddress.lb_public_ip.ip_address}"
}

output "autoscale_group_id" {
  description = "ID of the autoscale VM group"
  value       = cloudstack_autoscale_vm_group.vm_group.id
}

output "template_id" {
  description = "ID of the registered Ubuntu template"
  value       = cloudstack_template.ubuntu_template.id
}

output "initial_vm_id" {
  description = "ID of the initial VM instance"
  value       = cloudstack_instance.initial_vm.id
}

output "initial_vm_ip" {
  description = "Private IP address of the initial VM"
  value       = cloudstack_instance.initial_vm.ip_address
}

output "load_balancer_members" {
  description = "Current members of the load balancer"
  value       = cloudstack_loadbalancer_rule.autoscale_lb.member_ids
}

On running the config, it successfully creates the resources and autoscale vm group:


Apply complete! Resources: 12 added, 0 changed, 0 destroyed.

Outputs:

autoscale_group_id = "1d571b8e-621f-4e5b-b854-944b182a816c"
initial_vm_id = "03097204-d52c-4e2a-97b5-10d2f82b92f2"
initial_vm_ip = "10.0.10.31"
load_balancer_url = "http://10.0.xx.xx"
network_cidr = "10.0.10.0/24"
network_id = "5162c6ea-6e36-44e5-81e5-cd99c1c4f62b"
public_ip_address = "10.0.xx.xx"
template_id = "650ababb-ee02-4d8c-9a1c-9cdb3fd4c4e1"


image

Depends on: apache/cloudstack-go#119 , to test locally using this fix, checkout to the branch fix-counter-response-object on the cloudstack-go repo and add the following to the go.mod file in the tf repo:

replace github.com/apache/cloudstack-go/v2 => <path to cloudstack repo locally>

@Pearl1594 Pearl1594 linked an issue Aug 29, 2025 that may be closed by this pull request
@Pearl1594
Copy link
Contributor Author

test failures due to: apache/cloudstack-go#119

@Pearl1594 Pearl1594 changed the title [WIP] Add support for autoscale VM group Add support for autoscale VM group Sep 2, 2025
@Pearl1594 Pearl1594 changed the title Add support for autoscale VM group Add support for autoscale VM groups Sep 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improvement issue for AutoScale VM Groups with Terraform.
1 participant