Change and scale your infrastructure
The needs of an application change over time. This page shows how to adjust running resources safely with Terraform: changes are made in place, resources are not recreated and no data is lost.
Prerequisites
- ✅ Your infrastructure is managed by Terraform, for example as in Build a complete web application infrastructure.
Read the plan before every change
Always run terraform plan and read the result before you apply:
| Plan shows | Meaning |
|---|---|
~ update in-place | The resource is updated and its data is kept. |
-/+ destroy and then create replacement | The resource is deleted and then recreated. For an Instance, the data on the root disk is lost. |
# forces replacement | Marks the argument that causes the recreation. |
Every change on this page is an in-place change.
Save the plan and then apply exactly that plan, so that what is applied matches exactly what you read:
terraform plan -out=tfplan
terraform apply tfplan
Change the vCPU and RAM configuration
Change the flavor to increase or decrease vCPU and RAM. If you select the flavor with a filter, you only need to edit the filter values:
data "fptcloud_flavor" "app" {
vpc_id = data.fptcloud_vpc.example.id
filter {
key = "cpu"
values = ["4"] # previously 2
}
filter {
key = "memory_mb"
values = ["8192"] # previously 4096
}
}
The plan shows ~ flavor_name for the Instances that use this flavor. After you apply, Terraform waits until the resize is complete. All data and configuration on the Instance are kept.
According to the Resize guide, before you resize:
- The Instance must be in the Running or Stopped state.
- Instances that use NVMe storage do not support resizing.
- An Instance with an attached GPU must have the GPU removed first.
Increase the root disk size
Increase the storage_size_gb of the Instance:
resource "fptcloud_instance" "app" {
# ...
storage_size_gb = 80 # previously 40
}
The root disk can only be increased. If you set a smaller value, terraform plan stops with an error before it changes anything.
After you apply, go into the operating system of the Instance and expand the matching partition, as described in Manage the Storage Disks of an Instance.
To move the root disk to another performance level, change the storage_policy_id of the Instance. This change is also made in place.
Increase the data disk size
Increase the size_gb of fptcloud_storage:
resource "fptcloud_storage" "app_data" {
# ...
size_gb = 200 # previously 100
}
The disk is expanded in place, and the data is kept. Then go into the operating system of the Instance and expand the matching partition.
The disk size can only be increased, not decreased. The provider does not block a smaller size_gb value at the terraform plan step, so check carefully before you apply.
Power Instances on and off
The status argument sets the power state. A common use is to power off development Instances outside working hours:
variable "dev_running" {
type = bool
default = true
}
resource "fptcloud_instance" "dev" {
# ...
status = var.dev_running ? "POWERED_ON" : "POWERED_OFF"
}
terraform apply -var="dev_running=false" # end of the day
terraform apply -var="dev_running=true" # start of the day
If your colleagues also power the Instance on and off on the Portal and you do not want Terraform to overwrite their changes, tell Terraform to ignore the power state:
resource "fptcloud_instance" "dev" {
# ...
status = "POWERED_ON"
lifecycle {
ignore_changes = [status]
}
}
Add Instances
When the Instances are created with count, increase the number to add Instances:
variable "web_count" {
type = number
default = 3 # previously 2
}
Terraform creates the new Instances, and the running Instances are not affected. If the Security Group uses apply_to with the IPs from the list of Instances, as in Build a complete web application infrastructure, the IP of the new Instance is added to the Security Group in the same apply.
Decreasing count deletes the Instances with the highest indexes. Read the plan carefully to know exactly which Instances are deleted. If you need to delete a specific Instance in the middle of the list, use for_each instead of count.
Recreate a resource on purpose
When an Instance has a problem and you want to recreate exactly that Instance from the configuration, use -replace:
terraform apply -replace="fptcloud_instance.web[1]"
Terraform recreates only the specified resource, and the other resources stay unchanged.
Protect important resources
Add prevent_destroy to resources whose accidental deletion would cause serious damage, for example a database server. Terraform then rejects every plan that deletes this resource, including a recreation caused by a change to image_name, subnet_id or ssh_key:
resource "fptcloud_instance" "db" {
# ...
lifecycle {
prevent_destroy = true
}
}
To delete this resource on purpose, remove the lifecycle block first.