Tutorial: Deploy to AWS

In this tutorial, we will deploy an existing Go CDK application called Guestbook to Amazon Web Services (AWS).

Guestbook is a sample application that records visitors’ messages and displays a cloud banner with an administrative message. The main business logic is written in a cloud-agnostic manner using MySQL, the generic blob API, and the generic runtimevar API. All platform-specific code is set up by Wire.


You will need to install the following software for this tutorial:

Prerequisite: Cloning the Guestbook Sample🔗

Before you can do anything else, you need to clone the Go CDK repository. The repository contains the Guestbook sample.

git clone https://github.com/google/go-cloud.git
cd go-cloud/samples/guestbook


Run the following in the samples/guestbook directory:

go generate && go build

Running Locally🔗

You will need to run a local MySQL database server and create a local message of the day. localdb/main.go runs the local MySQL database server using Docker:

go get ./localdb/... # Get package dependencies.
go run localdb/main.go

In another terminal, run the guestbook application:

# Set a local Message of the Day.
echo 'Hello, World!' > motd.txt

# Run the server.
# For blob, it uses fileblob, pointing at the local directory ./blobs.
# For runtimevar, it uses filevar, pointing at the local file ./motd.txt.
#   You can update the ./motd.txt while the server is running, refresh
#   the page, and see it change.
./guestbook -env=local -bucket=blobs -motd_var=motd.txt

Your server is now running on http://localhost:8080/.

You can stop the MySQL database server with Ctrl-\. MySQL ignores Ctrl-C (SIGINT).

Running on Amazon Web Services (AWS)🔗

If you want to run this sample on AWS, you need to set up an account, download the AWS command line interface, and log in. There’s help here if you need it.

aws configure

Agree to the Debian Terms and Conditions🔗

You have to agree to the Debian Terms and Conditions in order to provision the resources. Click through the “Continue to Subscribe” button at the top, then log in to your AWS account and subscribe to Debian.

SSH Key🔗

You will also need an SSH key to SSH into the EC2 instance. If you don’t already have one, you can follow this guide from GitHub. Follow the instructions for “Adding your key to the ssh-agent” if you want the key to persist across terminal sessions.


Provision resources with Terraform🔗

You can now use Terraform, a tool for initializing cloud resources, to set up your project. This will create an EC2 instance you can SSH to and run your binary.

# Build for deploying on the AWS Linux VM.
GOOS=linux GOARCH=amd64 go build

# Enter AWS directory from samples/guestbook.
cd aws
terraform init

# Provisioning can take up to 10 minutes.
# Keep track of the output of this command as it is needed later.
# You can replace us-west-1 with whatever region you want.
terraform apply -var region=us-west-1 -var ssh_public_key="$(cat ~/.ssh/id_rsa.pub)"

Connect to the new server and run the guestbook binary🔗

You now need to connect to the new remote server to execute the guestbook binary. The final output of terraform apply lists the variables guestbook requires as arguments. Here’s an example, with actual strings replaced with [redacted]:

# Output from "terraform apply" command....


bucket = [redacted]
database_host = [redacted]
database_root_password = <sensitive>
instance_host = [redacted]
paramstore_var = /guestbook/motd
region = us-west-1

# Print out the database root password, since we'll need it below
# Terraform hides it by default in the Outputs above.
localhost$ terraform output database_root_password

# SSH into the EC2 instance.
localhost$ ssh "admin@$( terraform output instance_host )"

# Fill in each command-line argument with the values from the
# Terraform output above.
server$ AWS_REGION=<your region> ./guestbook \
  -env=aws \
  -bucket=<your bucket> \
  -db_host=<your database_host> \
  -db_user=root \
  -db_password=<your database_root_password> \

View the guestbook application🔗

You can now visit the server at http://<your instance_host>:8080/.


To clean up the created resources, run terraform destroy inside the aws directory using the same variables you entered during terraform apply.


The Go gopher was designed by Renee French and used under the Creative Commons 3.0 Attributions license.