24sp ver.

Note: this is for the Spring 2024 iteration of CSE 391. Looking for a different quarter? Please visit https://courses.cs.washington.edu/courses/cse391/.

Here’s a tutorial for spinning up a simple Go web server in the cloud! You’re welcome to substitute a simple server in a different language if you’d like.

A simple Go server

Go has gained popularity in recent years for being a simple, readable language with powerful standard library support. We choose to use it in this demo because it is easy to install on different platforms and equally easy to spin up a simple Hello World web server.

Installing Go

Navigate to https://go.dev/doc/install and follow instructions for installing Go on your machine.

Running a local web server

Download and unzip the lecture10 zip from the website:

wget https://courses.cs.washington.edu/courses/cse391/24sp/lectures/10/lecture10.zip
unzip lecture10.zip

In the lecture10/simple_server directory there is a file simple_server.go that was copied from the Go how-to docs on web servers.

cd into the lecture10/simple_server directory and run the server using the following command:

go run simple_server.go

Now, in a browser, you should be able to navigate to http://localhost:8080/<path> (e.g. http://localhost:8080/ice_cream) and see a message displayed.

The simple server will write logs to a file simple_server.log in your current directory. You can follow the logs by running:

tail -f simple_server.log

Creating a GCP account

Navigate to https://cloud.google.com and create a free trial account.

When working with cloud products, you typically want to be extremely careful and considerate when creating accounts and resources. It is really easy to rack up expensive bills if you are not paying attention.

When creating a free trial account, GCP will ask you for payment information as part of the verification process. At the time of writing this, you will not be charged during your free trial, and you would have to explicitly opt-in to being charged when your free trial ends. It is worth confirming that this is still GCP’s policy when you create your free trial account.

Manually creating a GCP VM instance for our web server

Create a GCP VM instance in the web console

GCP allows you to provision and manage VMs for running applications in the cloud. Think of these as your own personal server, with higher level abstractions provided by GCP for managing them.

These settings are attempting to keep the costs in the “free tier” range (described here). If you are on a free trial, this won’t matter, but it will matter if you ever want to recreate this instance after the free trial period is over.

  1. Navigate to the GCP home page. Scroll down and click “Create a VM” (you may have to enable the API). You can also navigate directly to the VM instance console.
  2. Click “Create instance”.
  3. Name the instance instance-simple-server.
  4. Select region us-west1 and zone us-west1-a.
  5. Select E2 for the general machine type and e2-micro for the specific machine type.
  6. Use the Standard VM Provisioning model, and leave Display device unchecked.
  7. Skip Confidential VM service and Container configuration.
  8. Change the boot disk:
    • Operating system: Debian
    • Version: can leave default (this demo used Debian 12 bookworm)
    • Boot disk type: standard persistent disk
    • Size: 10 GB
  9. Leave the service account as the default
  10. For access scopes, select Allow full access to all cloud APIs. In production we’d want to be more careful about access permissions, but this is fine for the scope of our application.
  11. In Firewall, select Allow HTTP traffic and Allow HTTPS traffic.
  12. In observability, select Install Ops Agent for Monitoring and Logging
  13. Click create.

You should be redirected to the cloud console, where you will see your instance starting up. After a little bit, a green check mark will appear next to your created instance.

Create a firewall in the web console

By default, external traffic on port 8080 of our instance will not be allowed. In order to view our web server from external machines, we need to create a firewall rule that opens the port.

Navigate to the GCP console firewall manager and click “Create a firewall rule”:

  1. Set the name to allow-8080-traffic
  2. Turn off logs
  3. Set network to default and leave Priority at 1000
  4. Direction of traffic should be Ingress and Action on match should be Allow
  5. Enter allow-8080-traffic into Target tags
  6. Source IPv4 ranges should be 0.0.0.0/0 to allow all IPs to connect
  7. Under protocol and ports, select “Specified protocols and ports” and check TCP with port 8080.
  8. Click create
  9. Navigate back to the VM instance console.
  10. Click on the instance-simple-server instance.
  11. Click “Edit” at the top.
  12. Scroll down to “Network tags”.
  13. Add the allow-8080-traffic network tag.
  14. Click Save.

Installing our Go server onto the cloud instance

Authenticate with the Google Cloud CLI

Install the google cloud CLI by following the instructions at this url: https://cloud.google.com/sdk/docs/install.

Once you have the SDK installed, run gcloud init to setup the SDK. A few things to note:

  • Make sure you login with the same google account you created your GCP free trial with.
  • Make sure you select the correct project ID. You can find the project ID by clicking on the project name at the top of the GCP console and viewing the associated ID for your current project.
  • Make sure your default region and zone matches the ones you used to create the instance (in this case, it should be region us-west1 and zone us-west1-a).

If you need to re-authenticate in the future, you can do so by running:

gcloud auth login

Copy files onto the server

From the lecture10 directory, run the following command to copy the files to the server.

gcloud compute scp ./simple_server/* "instance-simple-server":~/

SSH onto server, install dependencies, run our application

Use SSH to connect to the server:

gcloud compute ssh "instance-simple-server"

Install the dependencies:

source install_deps.sh

Run the simple server:

./run_server.sh

View your web server running in the cloud

Navigate to the VM instance console and copy the external IP address for your server. You should now be able to visit your webpage by navigating to http://<external-ip>:8080/<path>, where <external-ip> is the external ip you retrieved from the instance, and <path> is the path you want to display.

Automating creating a VM instance w/terraform

Terraform is a tool for managing cloud infrastructure. It uses configuration files to allow you to create and manage cloud resources. It also builds and keeps track of a state of your current resources, allowing you to better understand what changes are being performed when making updates. Think of it as a “mapping” of your entire cloud infrastructure.

We can set up an instance to run our web server using terraform! In your lecture10 directory, there is a terraform directory that contains the necessary files for this. These config files might not be as intuitive as the UI console, but they are much more maintainable and re-usable.

Setup

You will need to download terraform for your system. Follow the instructions on this page for your machine’s operating system.

You will also need to delete the following GCP resources if you’ve already created them manually following the above instructions:

You likely also need to reauthenticate with google cloud with the following command:

gcloud auth application-default login

Create your cloud infrastructure with terraform

cd into the terraform directory. Start by modifying the main.tf file. Replace <project-id> on line 2 with the ID of your project. You can find the project ID by clicking on the project name at the top of the GCP console and viewing the associated ID for your current project.

Next, run terraform init to initialize the Terraform configuration for the working directory.

terraform init

You can then run terraform plan. This generates a summary of the changes that will be made when running terraform apply. Normally you’d want to make sure that all of the changes that are planned are actually changes you want to make. In this case, we can assume the changes are what we intend.

terraform plan

Finally, to actually create the infrastructure, we need to run terraform apply. This will actually execute our plan, and create/modify/delete cloud resources. In general, you probably should double check the changes are actually what you intend!

terraform apply

Confirm the changes by typing yes and hitting enter, then wait a bit for the resources to be created.

Once you are finished, navigate to the VM instance console. Notice how the instance-simple-server we deleted has now been created by terraform!

You can also see the allow-8080-traffic firewall rule has been recreated in the firewall manager.

For our next step, we will need the IP address of our server. We could use the VM instance console to view this, but we can also retrieve the value from terraform since it just created the instance!

Our outputs.tf defines outputs from our configuration. We can retrieve them with the terraform output command:

terraform output instance_ip_addr

Note this IP address for our next step.

Redeploy our web server

Now that our infrastructure is recreated, we can redeploy our web server!

There’s a handy script in the lecture10 directory that will run through the necessary gcloud scp and ssh commands for you:

./deploy_server.sh

Navigate to http://<external-ip>:8080/<path>, where <external-ip> is the external ip fetched from the terraform output command, and <path> is the path you want to display. You should have a functioning web page!