This tutorial shows how to get started with Compute Engine. Follow this tutorial by deploying a Hello World Go 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.
- Use Cloud Build to build 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 and Cloud Build APIs.
-
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 and Cloud Build APIs.
-
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
Running the app in Cloud Shell
In Cloud Shell, start a local web server:
go build -o app ./app
In Cloud Shell, click Web preview , and select Preview on port 8080. This opens a new window with your running app.
In your web browser, you see
Hello, World!
.When you're ready to move on, stop the local web server by pressing Control+C in Cloud Shell.
Deploying 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 Cloud Build to build the app
Cloud Build is used to build the app, compress it in a tar file, and upload the file to a Cloud Storage bucket. Buckets are the basic containers that hold your data in Cloud Storage.
In your terminal window, create a Cloud Storage bucket, where
YOUR_BUCKET_NAME
represents the name of your bucket:gcloud storage buckets create gs://YOUR_BUCKET_NAME
You can choose any name for your Cloud Storage bucket. It's a good practice to give your bucket the same name as your project ID. Bucket names must be unique across all of Google Cloud, so it's possible that you can't use your project ID as the bucket name.
Start the Cloud Build process:
gcloud builds submit --substitutions=_DEPLOY_DIR=gs://
YOUR_BUCKET_NAME
,_DEPLOY_FILENAME=app.tar.gzThe
gcloud builds submit
command uses--substitutions
to configure the location that the resulting tar file is uploaded to. Later, the tar file is downloaded to the Compute Engine instance.Cloud Build uses a YAML configuration file to define the steps the build requires.
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.
A startup script runs when an instance first boots.
The startup script performs these tasks:
Installs the Cloud Logging agent and configures it to monitor the app logs.
Downloads and extracts the deployment tar file.
Starts a
systemd
service to run the app.
Create and configure a Compute Engine instance
Create a Compute Engine instance:
Linux/macOS
gcloud compute instances create my-app-instance \ --image-family=debian-10 \ --image-project=debian-cloud \ --machine-type=g1-small \ --scopes userinfo-email,cloud-platform \ --metadata-from-file startup-script=startup-script.sh \ --metadata app-location="gs://YOUR_BUCKET_NAME/app.tar.gz" \ --zone YOUR_ZONE \ --tags http-server
Replace
YOUR_ZONE
with a development zone, for exampleus-central1-a
. For more information on regions and zones, see Geography and regions.The
--metadata app-location
flag tells the startup script where to download the app tar file.Windows
gcloud compute instances create my-app-instance ^ --image-family=debian-10 ^ --image-project=debian-cloud ^ --machine-type=g1-small ^ --scopes userinfo-email,cloud-platform ^ --metadata-from-file startup-script=startup-script.sh ^ --metadata app-location="gs://YOUR_BUCKET_NAME/app.tar.gz" ^ --zone YOUR_ZONE ^ --tags http-server
Replace
YOUR_ZONE
with a development zone, for exampleus-central1-a
. For more information on regions and zones, see Geography and regions.The
--metadata app-location
flag tells the startup script where to download the app tar file.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
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-80 \ --allow tcp:80 \ --source-ranges 0.0.0.0/0 \ --target-tags http-server \ --description "Allow port 80 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
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-80
What's next
Explore reference architectures, diagrams, and best practices about Google Cloud. Take a look at our Cloud Architecture Center.
For other Go 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.
- Deploy an app to Google Kubernetes Engine
- Explore other Google Cloud services.