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](https://docs.cloud.google.com/go/getting-started/tutorial-app).

## 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:


- [Compute Engine](https://cloud.google.com/compute/all-pricing)
- [Cloud Build](https://docs.cloud.google.com/build/pricing)


To generate a cost estimate based on your projected usage,
use the [pricing calculator](https://docs.cloud.google.com/products/calculator).
New Google Cloud users might be eligible for a [free trial](https://docs.cloud.google.com/free).

<br />

## Before you begin

1.
   In the Google Cloud console, open the app in [Cloud Shell](https://docs.cloud.google.com/shell).


   [Go to Cloud Shell](https://docs.cloud.google.com/console/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/golang-samples&working_dir=getting-started/gce)


   Cloud Shell provides command-line access to your cloud resources
   directly from the browser.
2. If you agree to clone the repository, click
   **Confirm** to download the sample code and change into the app
   directory.

3. In Cloud Shell, configure the gcloud CLI to use your new Google Cloud project:

   ```bash
   # Configure gcloud for your project
   gcloud config set project YOUR_PROJECT_ID
   ```


## Running the app in Cloud Shell


1. In Cloud Shell, start a local web server:

   ```go
   go build -o app
   ./app
   ```
2. 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!`.
3. When you're ready to move on, stop the local web server by pressing
   <kbd>Control+C</kbd> in Cloud Shell.

<br />

## Deploying to a single instance


This section walks you through running a single instance of your app
on Compute Engine.

![Single-instance deployment.](https://docs.cloud.google.com/static/languages/images/compute-single-instance.svg)

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.

1. In your terminal window, create a Cloud Storage bucket, where
   `YOUR_BUCKET_NAME` represents the name of your bucket:

   ```bash
   gcloud storage buckets create gs://YOUR_BUCKET_NAME
   ```

   You can choose
   [any name](https://docs.cloud.google.com/storage/docs/buckets.md.txt#naming)
   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.
2. Start the Cloud Build process:


   ```bash
   gcloud builds submit --substitutions=_DEPLOY_DIR=gs://YOUR_BUCKET_NAME,_DEPLOY_FILENAME=app.tar.gz
   ```

   The `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.

   <br />

   Cloud Build uses a
   [YAML configuration file](https://docs.cloud.google.com/build/docs/build-config)
   to define the steps the build requires.


   ```yaml
   steps:
     # Print the Go version being used.
     - name: 'mirror.gcr.io/library/golang'
       args: ['go', 'version']
     # Make a deploy directory we'll tar after building the app.
     - name: 'debian'
       args: ['mkdir', '-p', 'deploy/etc/systemd/system/', 'deploy/usr/bin']
     # Build the app.
     - name: 'mirror.gcr.io/library/golang'
       env: [
         'GO111MODULE=on',
         'GOPROXY=https://proxy.golang.org,direct',
         'GOOS=linux',
         'GOARCH=amd64'
       ]
       args: ['go', 'build', '-o', 'deploy/usr/bin/app', '.']
     # Copy the systemd service file into the deploy directory.
     - name: 'debian'
       args: ['cp', 'my-app.service', 'deploy/etc/systemd/system/']
     # Compress the deploy directory.
     - name: 'debian'
       args: ['tar', '-czf', '${_DEPLOY_FILENAME}', '-C', './deploy', '.']
   # Upload the tarball to Cloud Storage.
   artifacts:
     objects:
       location: '${_DEPLOY_DIR}'
       paths: ['${_DEPLOY_FILENAME}']
   ```

   <br />

### 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.

```bash
set -ex

# Install logging monitor. The monitor will automatically pickup logs sent to syslog.
curl "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" --output google-fluentd-install.sh
checksum=$(sha256sum google-fluentd-install.sh | awk '{print $1;}')
if [ "$checksum" != "ec78e9067f45f6653a6749cf922dbc9d79f80027d098c90da02f71532b5cc967" ]; then
    echo "Checksum does not match"
    exit 1
fi
chmod +x google-fluentd-install.sh && ./google-fluentd-install.sh
service google-fluentd restart &

APP_LOCATION=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/app-location" -H "Metadata-Flavor: Google")
gcloud storage cp "$APP_LOCATION" app.tar.gz
tar -xzf app.tar.gz

# Start the service included in app.tar.gz.
service my-app start
```

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.

<br />

### Create and configure a Compute Engine instance


1. Create a Compute Engine instance:


   ### Linux/macOS

   ```bash
   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
   example `us-central1-a`. For more information on regions and zones, see
   [Geography and regions](https://docs.cloud.google.com/docs/geography-and-regions.md.txt).

   The `--metadata app-location` flag tells the startup script where to download
   the app tar file.

   ### Windows

   ```powershell
   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
   example `us-central1-a`. For more information on regions and zones, see
   [Geography and regions](https://docs.cloud.google.com/docs/geography-and-regions.md.txt).

   The `--metadata app-location` flag tells the startup script where to download
   the app tar file.

   <br />

   <br />

   This creates a new instance, allows it to access Google Cloud
   services, and runs your startup script. The instance name is
   `my-app-instance`.
2. Check the progress of the instance creation:

   ```
   gcloud compute instances get-serial-port-output my-app-instance --zone YOUR_ZONE
   ```

   <br />

   When the startup script is complete, you see the following message:

   ```
   startup-script: INFO Finished running startup scripts.
   ```
3. Create a firewall rule to allow traffic to your instance:


   ```go
   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"
   ```

   <br />

4. Get the external IP address of your instance:

   ```go
   gcloud compute instances list
   ```
5. 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.

<br />

### Manage and monitor an instance

You can use the Google Cloud console to monitor and manage your instance.

1. In the Google Cloud console, go to the **VM instances** page.

   [Go to VM instances](https://console.cloud.google.com/compute/instances)
2. In the list of virtual machine instances, click **SSH** in the row of the instance that you want to connect to.
3. To view all of the logs generated by your Compute Engine resources,
   go to the **Logs Explorer** page.

   [Go to Logs Explorer](https://console.cloud.google.com/logs/query)


   [Cloud Logging](https://docs.cloud.google.com/logging) is automatically configured to gather logs from
   various common services, including `syslog`.

<br />

<br />

## 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

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

1. In the Google Cloud console, go to the **Manage resources** page.

   [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)
2. In the project list, select the project that you want to delete, and then click **Delete**.
3. In the dialog, type the project ID, and then click **Shut down** to delete the project.

### Delete the individual resources

```bash
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](https://docs.cloud.google.com/architecture).

- For other Go resources for building your apps, see the following:

  - Manage and monitor your Instance Group deployment at [Compute Engine \> Instance groups](https://console.cloud.google.com/compute/instanceGroups).
  - Manage your load balancing configuration, including URL maps and backend services, at [Network services \> Load balancing](https://console.cloud.google.com/net-services/loadbalancing/list).
  - [Deploy an app to Google Kubernetes Engine](https://docs.cloud.google.com/kubernetes-engine/docs/quickstarts/deploy-app-container-image)
  - Explore other [Google Cloud services](https://docs.cloud.google.com/).