This tutorial shows how to get started with Compute Engine. Follow this tutorial by deploying a Hello World Node.js web app to Compute Engine. For help getting started with App Engine, see the App Engine standard environment.
Objectives
- Use Cloud Shell to download and deploy a Hello World sample app.
- Deploy a Hello World sample app to a single Compute Engine instance.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Compute Engine API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Compute Engine API.
-
In the Google Cloud console, open the app in Cloud Shell.
Cloud Shell provides command-line access to your cloud resources directly from the browser.
-
If you agree to clone the repository, click Confirm to download the sample code and change into the app directory.
-
In Cloud Shell, configure the gcloud CLI to use
your new Google Cloud project:
# Configure gcloud for your project gcloud config set project YOUR_PROJECT_ID
Run the app in Cloud Shell
If you clicked Go to Cloud Shell in the preceding section, Cloud Shell
cloned the nodejs-getting-started
repository to your instance. After the instance
launches, your working directory is
~/cloudshell_open/nodejs-getting-started/gce
. If you are not in this
directory, see the steps in Before you begin.
Install dependencies:
npm install
Run the app:
npm start
In Cloud Shell, click Web preview , and select Preview on port 8080. This opens a new window with your running app.
To stop the local web server, press Control+C.
Set up Cloud Source Repositories
Enable the Cloud Source Repositories API.
In the Google Cloud console, go to Cloud Source Repositories.
Select Add repository.
Select Create new repository, and then click Continue.
In the Repository name field, enter
new-repo
.In the Project field, enter the project ID of the project that you created or selected for this tutorial, and then click Continue.
Select Clone your repository to a local Git repository.
In Cloud Shell, return to root and clone the repository by following the instructions in the Google Cloud SDK tab, and keep this tab open.
Before completing the final step under the instructions, copy your app into your new repository in Cloud Shell:
cd new-repo cp ../cloudshell_open/nodejs-getting-started/gce/app.js app.js cp ../cloudshell_open/nodejs-getting-started/gce/package.json package.json
Follow the remaining instructions in the Google Cloud SDK tab to commit your changes to your repository. Confirm that you have added your code to the repository by refreshing the browser and checking that your code was added to the repo
new-repo
.
Deploy to a single instance
This section walks you through running a single instance of your app on Compute Engine.
From Cloud Shell, you can deploy to a single Compute Engine instance virtual machine (VM) which runs your app.
Use a startup script to initialize an instance
You need a way to instruct your instance to download and run your code. An instance can have a startup script that runs whenever the instance is started or restarted.
Here is the startup script that is included in the Hello World sample app:
The startup script performs the following tasks:
Installs the Cloud Logging agent. The agent automatically collects logs from syslog.
Installs Supervisor to run the app as a daemon.
Clones the app's source code from Cloud Source Repositories and installs dependencies.
Configures Supervisor to run the app. Supervisor makes sure the app is restarted if it exits unexpectedly or is terminated by an admin or other process. It also sends the app's
stdout
andstderr
to syslog to be collected by the Logging agent.
Create and configure a Compute Engine instance
Create a Compute Engine instance:
Linux/macOS
gcloud compute instances create my-app-instance \ --image-family=debian-9 \ --image-project=debian-cloud \ --machine-type=g1-small \ --scopes userinfo-email,cloud-platform \ --metadata app-location=us-central1-f \ --metadata-from-file startup-script=gce/startup-script.sh \ --zone us-central1-f \ --tags http-server
Windows
gcloud compute instances create my-app-instance ^ --image-family=debian-9 ^ --image-project=debian-cloud ^ --machine-type=g1-small ^ --scopes userinfo-email,cloud-platform ^ --metadata-from-file startup-script=gce/startup-script.sh ^ --zone us-central1-f ^ --tags http-server
Replace the zone with a development zone, for example
us-central1-a
. For more information on regions and zones, see Geography and regions.This creates a new instance, allows it to access Google Cloud services, and runs your startup script. The instance name is
my-app-instance
.Check the progress of the instance creation:
gcloud compute instances get-serial-port-output my-app-instance --zone YOUR_ZONE
Replace
YOUR_ZONE
with the zone you deployed your instance to.When the startup script is complete, you see the following message:
startup-script: INFO Finished running startup scripts.
Create a firewall rule to allow traffic to your instance:
gcloud compute firewall-rules create default-allow-http-8080 \ --allow tcp:8080 \ --source-ranges 0.0.0.0/0 \ --target-tags http-server \ --description "Allow port 8080 access to http-server"
Get the external IP address of your instance:
gcloud compute instances list
To see your app running, enter this URL in your browser:
http://YOUR_INSTANCE_IP:8080
Replace
YOUR_INSTANCE_IP
with the external IP address of your instance.
Manage and monitor an instance
You can use the Google Cloud console to monitor and manage your instance.
- In the Google Cloud console, go to the VM instances page.
- In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
- To view all of the logs generated by your Compute Engine resources,
go to the Logs Explorer page.
Go to Logs Explorer
Cloud Logging is automatically configured to gather logs from various common services, including
syslog
.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Delete the project
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Delete the individual resources
gcloud compute instances delete my-app-instance --zone=YOUR_ZONE --delete-disks=all gcloud compute firewall-rules delete default-allow-http-8080
What's next
Explore reference architectures, diagrams, and best practices about Google Cloud. Take a look at our Cloud Architecture Center.
For other Node.js resources for building your apps, see the following:
- Manage and monitor your Instance Group deployment at Compute Engine > Instance groups.
- Manage your load balancing configuration, including URL maps and backend services, at Network services > Load balancing.
- Handle sessions with Firestore
- Authenticate users with Identity-Aware Proxy
- Deploy an app to Google Kubernetes Engine
- Explore other Google Cloud services.