Using autoscaling for highly scalable applications Stay organized with collections Save and categorize content based on your preferences.
This tutorial explains how to useautoscalingto automatically adjust the number of VM instances that are hosting yourapplication, allowing your application to adapt to varying amounts of traffic.
To use autoscaling, host your application on a managed instancegroup. Amanaged instance groupis a collection of instances that are all running the same applicationand can be managed as a single entity. When a managed instance group hasautoscaling enabled, the number of VMs in the instance group automaticallyincreases (scales out) or decreases (scales in) according to thetarget value that you specifyfor your autoscaling policy.
This tutorial includes detailed steps for launching a web applicationon a managed instance group, setting up autoscaling, configuring network access,and observing autoscaling by simulating load spikes and drops. Depending on yourexperience with these features, this tutorial takes about 20 minutes tocomplete.
Objectives
- Launch a demo web application on a managed instance group.
- Observe the effects of autoscaling by simulating traffic spikes and drops.
Costs
In this document, you use the following billable components of Google Cloud:
- Compute Engine
To generate a cost estimate based on your projected usage, use thepricing calculator.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission.Learn how to grant roles.
Verify that billing is enabled for your Google Cloud project.
Application architecture
The application includes the following Compute Engine components:
- Firewall rule:a Google Cloudfirewall that lets you allow or deny traffic to your instances.
- Instance template:a template used to create each VM instance in the managed instance group.
- Regional managed instance group:a group of VM instances running the same application across multiple zones.
Launching the web application
This tutorial uses a web application that is stored on GitHub. If you wouldlike learn more about how the application was implemented, see theGoogleCloudPlatform/python-docs-samples repository on GitHub.
Launch the web application on every VM in a managed instance group by includinga startup script in an instance template. To allow HTTP traffic to the webapplication, create a firewall rule.
Create a firewall rule
Note: This firewall rule allows ingress HTTP traffic for all instances that areon the default network and have thehttp-server networking tag.Create a firewall rule to allow HTTP traffic to the web application:
In the Google Cloud console, go to theFirewalls page.
ClickCreate firewall rule.
UnderName, enter
default-allow-http.SetNetwork to
default.SetTargets to select
Specified target tags.UnderTarget Tags, enter
http-server.SetSource filter to
IPv4 ranges.UnderSource IPv4 ranges, enter
0.0.0.0/0to allow access for all IP addresses.
UnderProtocols and ports, selectSpecified protocols and ports.Then, selectTCP and enter
80toallow access for HTTP traffic.ClickCreate.
Create an instance template
Create an instance template that launches the demo web application on startup:
In the Google Cloud console, go to theInstance templates page.
ClickCreate instance template.
UnderName, enter
autoscaling-web-app-template.UnderMachine configuration, set theMachine type to
e2-standard-2.UnderFirewall, select theAllow HTTP traffic checkbox. This appliesthe
http-servernetworking tag to each instance created from thistemplate.Expand theAdvanced options section to see advancedsettings.
Expand theManagement section.
In theAutomation section, enter the following startup script:
sudo apt update && sudo apt -y install git gunicorn3 python3-pipgit clone https://github.com/GoogleCloudPlatform/python-docs-samples.gitcd python-docs-samples/compute/managed-instances/demosudo pip3 install -r requirements.txtsudo gunicorn3 --bind 0.0.0.0:80 app:app --daemon
This script causes each VM to run the web application during startup.
ClickCreate.
Create a managed instance group
Create a regional instance group to begin running the web application:
In the Google Cloud console, go to theInstance groups page.
ClickCreate instance group to create a new instance group.
SelectNew managed instance group (stateless)."
ForName, enter
autoscaling-web-app-group.ForInstance template, select
autoscaling-web-app-template.ForLocation, selectMultiple zones.
Pro Tip: To ensure your application is available during extreme events, like zonal outages, Compute Engine recommends that you distribute your application across multiple zones.
ForRegion, selectus-central1.
ForZones, select the following zones from the drop-down list:
- us-central1-b
- us-central1-c
- us-central1-f
Configure autoscaling for the instance group:
- ForAutoscaling mode, selectOn: add and remove instances tothe group.
Set theMinimum number of instances to
3.Pro Tip:When creating a regional managed instance group, Compute Enginerecommends that you provision enough instances so that, if all of theinstances in any one zone are unavailable, the remaining instancesstill meet the minimum number of instances that you require.However, provisioning more instances than you need might incuradditional costs. For more information, seeSelecting instance group size to ensure availability.
Set theMaximum number of instances to
6.Set theInitialization period to
120seconds.Pro Tip: Theinitialization periodis the number of seconds after an instance is created that theautoscaler should wait before using information about the instance forscaling decisions. When a VM is initializing, the CPU usage is notreliable for autoscaling. To prevent the autoscaler from scaling basedon inaccurate data, make sure the initialization period is longer thanthe time than the time it takes for the CPU utilization of your VM toinitially stabilize. For more information, seeInitialization periodandMonitoring autoscaling chartsand logs.
UnderAutoscaling Metrics, selectCPU utilization as themetric type.To learn more about autoscaling metrics, seeAutoscaling policy.
Set theTarget CPU utilization to
60.ClickDone.
UnderAutohealing, selectNo health check from theHealth checkdrop-down list.
ClickCreate. This redirects you to theInstance groups page.
Note: Wait a few minutes until all of the instances in thegroup are running and ready to display the web application.To verify that your instances are running:
- On theInstance groups page in the Google Cloud console, click
autoscaling-web-app-groupto see the instances in that group. UnderExternal IP, click on an IP address to connect that instance.A new browser tab opens displaying the demo web application:

Note: If you are unable toconnect to the web application after waiting a few minutes, verifythe instance status and network settings:
- Verify that the instance group is ready. If the application fails to load with an ERR_CONNECTION_REFUSED status, wait a few minutes for the startup script to finish running.
- Verify that the group's instance template hasAllow HTTP traffic enabled. Then, verify that
allow-web-app-httpfirewall rule was created correctly.
When you are done, close the browser tab for the demo web application.
- On theInstance groups page in the Google Cloud console, click
Observing autoscaling
For more information about autoscaling behaviors, seeUnderstanding autoscaling decisions.
Monitor autoscaling
The instance group you created uses anAutoscaling policy based onCPU usage. This means that autoscaler grows or shrinks the group as neededto maintain the target CPU utilization of60%.
To monitor the size and CPU utilization of your instance group, usetheautoscaling chartsin the Google Cloud console:
- On theInstance groups page for the
autoscaling-web-app-groupinstance group, click theMonitoring tab. - You can monitor autoscaling from theGroup size chart.The graph displaysInstances, which represents thenumber of VM instances in the group over time.
Optional: To monitor autoscaled capacity versus utilization, see theAutoscaler utilization (CPU) chart. The graph displaysUtilization, which is the total CPU utilization of VM instances in thegroup, andCapacity, which is the cumulative target CPU utilization ofthe group (target CPU utilization multiplied by the number of VM instances).
Autoscaling attempts to makeCapacity matchUtilization by changingthe number ofInstances, when possible.
Keep this window open.
Simulate scale out
Scale out occurs when the average CPU utilization of the instance group issignificantly higher than the target value. During scale out, autoscalergradually increases the size of the instance group until CPU utilizationdecreases to the target CPU utilization value or until the instance group sizeequals theMaximum number of instances, which was set to6.
To trigger scale out, increase the CPU utilization for your instances:
In the Google Cloud console, openCloud Shell.
Cloud Shell opens on the bottom of the Google Cloud console. It can take a few seconds for the session to initialize.
Create a local bash variable for the project ID:
export PROJECT_ID=[PROJECT_ID]
where
PROJECT_IDis the project ID for your current project, whichis displayed on each new line in the Cloud Shell:user@cloudshell:~ ([PROJECT_ID])$
Run the following bash script. This script causes the demo web applicationinstances to have an increased load, which increases CPU utilization.After a few minutes, the CPU utilization will surpass the target value,prompting the autoscaling to increase the instance group size.
export MACHINES=$(gcloud --project=$PROJECT_ID compute instances list --format="csv(name,networkInterfaces[0].accessConfigs[0].natIP)" | grep "autoscaling-web-app-group")for i in $MACHINES;do NAME=$(echo "$i" | cut -f1 -d,) IP=$(echo "$i" | cut -f2 -d,) echo "Simulating high load for instance $NAME" curl -q -s "http://$IP/startLoad" >/dev/null --retry 2done
Open theMonitoring tab in the Google Cloud console.
After a few minutes, theMonitoring tab displays that the CPUUtilization increased, which triggers autoscaling to increaseCapacity by increasing the number ofInstances.
Note: You might need to refresh the page to see the most recent chart.You might also notice that 6 instances are now listed under theOverview tab.
Keep both windows open.
Simulate scale in
Scale in occurs when the average CPU utilization of the instance group issignificantly lower than the target value. During scale in, autoscalergradually decreases the size of the instance group until CPU utilizationincreases to the target CPU utilization or until the instancegroup size equals theMinimum number of instances, which was set to3.
To trigger scale in, decrease the CPU utilization for your instances:
Run the following bash script. This script causes the demo web applicationinstances to have a decreased load, which decreases CPU utilization.After a few minutes, the CPU utilization will fall below the target value,prompting the autoscaler to decrease the instance group size.
export MACHINES=$(gcloud --project=$PROJECT_ID compute instances list --format="csv(name,networkInterfaces[0].accessConfigs[0].natIP)" | grep "autoscaling-web-app-group")for i in $MACHINES;do NAME=$(echo "$i" | cut -f1 -d,) IP=$(echo "$i" | cut -f2 -d,) echo "Simulating low load for instance $NAME" curl -q -s "http://$IP/stopLoad" >/dev/null --retry 2done
Open theMonitoring tab in the Google Cloud console.
After a few minutes, theMonitoring tab displays that the CPUUtilization decreased. After thestabilization period,which verifies that the load is consistently less,autoscaling decreasesCapacity by decreasing the number ofInstances.
Note: You might need to refresh the page to see the most recent chart.You might also notice that only 3 instances are listed under theOverview tab.
Close both windows when you have finished.
Clean up
After you finish the tutorial, you can clean up the resources that you created so that they stop using quota and incurring charges. The following sections describe how to delete or turn off these resources.
If you created a separate project for this tutorial, delete the entire project.Otherwise, if the project has resources that you want to keep, only delete theresources created in this tutorial.
Deleting the project
Deleting specific resources
Deleting the instance group
- In the Google Cloud console, go to theInstance groups page.
- Select the checkbox for your
autoscaling-web-app-groupinstance group. - To delete the instance group, clickDelete.
Deleting the instance template
Note: You must finish deleting the instance group before deleting the instancetemplate. You cannot delete an instance template if a managed instance groupis using it.In the Google Cloud console, go to theInstance templates page.
Click the checkbox next to the
autoscaling-web-app-template.ClickDeleteat the top of the page. In the new window, clickDelete toconfirm the deletion.
Deleting the firewall rule
In the Google Cloud console, go to theFirewall rules page.
Click the checkbox next to the firewall rule named
default-allow-http.ClickDelete. In the newwindow, clickDelete to confirm the deletion.
What's next
- Try another tutorial:
- Learn more aboutManaged Instance Groups.
- Learn more aboutAutoscaling.
- Learn more aboutDesigning Robust Systems.
- Learn more aboutBuilding Scalable and Resilient Web Applications on Google Cloud.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.