Tutorial: Connect to Azure

In this tutorial, we will run an existing Go CDK application called Guestbook backed by services running on Microsoft Azure. The Go CDK doesn’t have support for SQL on Azure yet (#1305), so we’ll run MySQL and the binary locally. This tutorial will show how to use Azure storage for the MOTD and Gopher logo.

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.

Prerequisites🔗

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

Building🔗

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 Azure🔗

If you want to run this sample on Azure, you first need to set up an Azure account. Use the az CLI to log in.

az login

Provision resources with Terraform🔗

We’ll use Terraform, a tool for initializing cloud resources, to set up your project.

# Enter the Azure directory from samples/guestbook.
cd azure
terraform init

# Provisioning can take up to 10 minutes.
# Keep track of the output of this command as it is needed later.
terraform apply -var location="West US"

<snip>
Outputs:

access_key = [redacted]
storage_account = [redacated]
storage_container = [redacted]

Running🔗

You will need to run a local MySQL database server, similar to what we did for running locally earlier. Open a new terminal window, and run:

cd .. # back up to samples/guestbook
go get ./localdb/... # Get package dependencies.
go run localdb/main.go

In the original terminal, add your Azure credentials to the environment and run the guestbook application:

# You should be in the "samples/guestbook/azure" directory.

# Enter the storage_account from the Terraform output earlier.
export AZURE_STORAGE_ACCOUNT=<your storage_account>
# Enter the access_key from the Terraform output earlier.
export AZURE_STORAGE_KEY=<your access_key>

# Run the binary.
# Fill in the -bucket command-line argument with the value from the Terraform
# output.
#
./guestbook -env=azure -bucket=<your storage_container> -motd_var=motd

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

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

Cleanup🔗

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

Gophers🔗

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