---
# System prepended metadata

title: Heat Scale Up and Down for OpenStack Train

---

# Heat Scale Up and Down for OpenStack Train
## Heat Template
devided into 5 files, 3 for heat, 2 for API trigger sample.
1. cirros.yaml
    1.1. define VM
2. environment.yaml
    2.1. define environment
3. template.yaml
    3.1. define scale policy
4. API Trigger Samples
    4.1. Scale Up
    4.2. Scale Down
## cirros.yaml
```
heat_template_version: 2016-10-14
description: Template to spawn an cirros instance.
parameters:
  metadata:
    type: json
  image:
    type: string
    description: image used to create instance
    default: cirros
  flavor:
    type: string
    description: instance flavor to be used
    default: m1.tiny
  key_name:
    type: string
    description: keypair to be used
    default: nova
  network:
    type: string
    description: project network to attach instance to
    default: private-net
  external_network:
    type: string
    description: network used for floating IPs
    default: ext-net
resources:
  server:
    type: OS::Nova::Server
    properties:
      block_device_mapping:
        - device_name: vda
          delete_on_termination: true
          volume_id: { get_resource: volume }
      flavor: {get_param: flavor}
      key_name: {get_param: key_name}
      metadata: {get_param: metadata}
      networks:
        - port: { get_resource: port }
  port:
    type: OS::Neutron::Port
    properties:
      network: {get_param: network}
      security_groups:
        - default
  floating_ip:
    type: OS::Neutron::FloatingIP
    properties:
      floating_network: {get_param: external_network}
  floating_ip_assoc:
    type: OS::Neutron::FloatingIPAssociation
    properties:
      floatingip_id: { get_resource: floating_ip }
      port_id: { get_resource: port }
  volume:
    type: OS::Cinder::Volume
    properties:
      image: {get_param: image}
      size: 1
```
## environment.yaml
```
resource_registry:
  "OS::Nova::Server::Cirros": cirros.yaml
```
## template.yaml
```
heat_template_version: 2016-10-14
description: Example auto scale group, policy and alarm
resources:
  scaleup_group:
    type: OS::Heat::AutoScalingGroup
    properties:
      cooldown: 300
      desired_capacity: 1
      max_size: 3
      min_size: 1
      resource:
        type: OS::Nova::Server::Cirros
        properties:
          metadata: {"metering.server_group": {get_param:
"OS::stack_id"}}
  scaleup_policy:
    type: OS::Heat::ScalingPolicy
    properties:
      adjustment_type: change_in_capacity
      auto_scaling_group_id: { get_resource: scaleup_group }
      cooldown: 300
      scaling_adjustment: 1
  scaledown_policy:
    type: OS::Heat::ScalingPolicy
    properties:
      adjustment_type: change_in_capacity
      auto_scaling_group_id: { get_resource: scaleup_group }
      cooldown: 300
      scaling_adjustment: -1
outputs:
  scaleup_policy_signal_url:
    value: {get_attr: [scaleup_policy, signal_url]}
  scaledown_policy_signal_url:
    value: {get_attr: [scaledown_policy, signal_url]}
```
## API Trigger Samples
get token
```
curl -i \
  -H "Content-Type: application/json" \
  -d '
{ "auth": {
    "identity": {
      "methods": ["password"],
      "password": {
        "user": {
          "name": "demo",
          "domain": { "id": "default" },
          "password": "openstack"
        }
      }
    },
    "scope": {
      "project": {
        "name": "demo",
        "domain": { "id": "default" }
      }
    }
  }
}' \
  "http://192.168.122.20:5000/v3/auth/tokens"
```
Scale Up
```
curl -g -i -X POST http://192.168.122.20:8004/v1/811904f2d746428cb1fdf4f3a1befd93/stacks/SAMPLE/6a2ce35f-891d-49a4-8718-ccd3ea874d54/resources/scaleup_policy/signal \
-H "X-Auth-Token: $token"
```
Scale Down
```
curl -g -i -X POST http://192.168.122.20:8004/v1/811904f2d746428cb1fdf4f3a1befd93/stacks/SAMPLE/6a2ce35f-891d-49a4-8718-ccd3ea874d54/resources/scaledown_policy/signal \
-H "X-Auth-Token: $token"
```