| 1 |
--- |
| 2 |
title: "Tutorial: Guestbook Application" |
| 3 |
linkTitle: "Guestbook" |
| 4 |
date: 2019-07-10T16:58:00-07:00 |
| 5 |
weight: 2 |
| 6 |
toc: true |
| 7 |
--- |
| 8 |
|
| 9 |
In this tutorial, we will deploy an existing Go CDK application called Guestbook |
| 10 |
locally, then on Google Cloud Platform (GCP), Amazon Web Services (AWS), and |
| 11 |
Azure. |
| 12 |
|
| 13 |
<!--more--> |
| 14 |
|
| 15 |
Guestbook is a sample application that records visitors' messages and displays a |
| 16 |
cloud banner with an administrative message. The main business logic is written |
| 17 |
in a cloud-agnostic manner using MySQL, the generic blob API, and the generic |
| 18 |
runtimevar API. All platform-specific code is set up by |
| 19 |
[Wire](https://github.com/google/wire). |
| 20 |
|
| 21 |
## Prerequisites |
| 22 |
|
| 23 |
You will need to install the following software for this tutorial: |
| 24 |
|
| 25 |
- [Git](https://git-scm.com/) |
| 26 |
- [Go](https://golang.org/doc/install) |
| 27 |
- [Wire](https://github.com/google/wire/blob/master/README.md#installing) |
| 28 |
- [Docker Desktop](https://docs.docker.com/install/) |
| 29 |
- [Terraform](https://www.terraform.io/intro/getting-started/install.html) |
| 30 |
|
| 31 |
### Cloning the Sample |
| 32 |
|
| 33 |
Before you can do anything else, you need to clone the Go CDK repository. The |
| 34 |
repository contains the Guestbook sample. |
| 35 |
|
| 36 |
```shell |
| 37 |
git clone https://github.com/google/go-cloud.git |
| 38 |
cd go-cloud/samples/guestbook |
| 39 |
``` |
| 40 |
|
| 41 |
## Building |
| 42 |
|
| 43 |
Run the following in the `samples/guestbook` directory: |
| 44 |
|
| 45 |
```shell |
| 46 |
go generate && go build |
| 47 |
``` |
| 48 |
|
| 49 |
## Running Locally |
| 50 |
|
| 51 |
You will need to run a local MySQL database server and create a local message of |
| 52 |
the day. `localdb/main.go` runs the local MySQL database server using Docker: |
| 53 |
|
| 54 |
```shell |
| 55 |
go get ./localdb/... # Get package dependencies. |
| 56 |
go run localdb/main.go |
| 57 |
``` |
| 58 |
|
| 59 |
In another terminal, run the `guestbook` application: |
| 60 |
|
| 61 |
```shell |
| 62 |
# Set a local Message of the Day. |
| 63 |
echo 'Hello, World!' > motd.txt |
| 64 |
|
| 65 |
# Run the server. |
| 66 |
# For blob, it uses fileblob, pointing at the local directory ./blobs. |
| 67 |
# For runtimevar, it uses filevar, pointing at the local file ./motd.txt. |
| 68 |
# You can update the ./motd.txt while the server is running, refresh |
| 69 |
# the page, and see it change. |
| 70 |
./guestbook -env=local -bucket=blobs -motd_var=motd.txt |
| 71 |
``` |
| 72 |
|
| 73 |
Your server is now running on http://localhost:8080/. |
| 74 |
|
| 75 |
You can stop the MySQL database server with Ctrl-\\. MySQL ignores Ctrl-C |
| 76 |
(SIGINT). |
| 77 |
|
| 78 |
## Running on Google Cloud Platform (GCP) |
| 79 |
|
| 80 |
If you want to run this sample on GCP, you need to create a project, download |
| 81 |
the [gcloud CLI][], install `kubectl` and log in. |
| 82 |
|
| 83 |
[gcloud CLI]: https://cloud.google.com/sdk/downloads |
| 84 |
|
| 85 |
```shell |
| 86 |
# Install kubectl. |
| 87 |
gcloud components install kubectl |
| 88 |
|
| 89 |
# Opens a browser to log you into GCP. |
| 90 |
gcloud auth login |
| 91 |
``` |
| 92 |
|
| 93 |
### Provision resources |
| 94 |
|
| 95 |
You can now use Terraform to set up your project. |
| 96 |
|
| 97 |
```shell |
| 98 |
gcloud auth application-default login |
| 99 |
cd gcp |
| 100 |
terraform init |
| 101 |
|
| 102 |
# Terraform will prompt you for your GCP project ID, desired region, |
| 103 |
# and desired zone. |
| 104 |
terraform apply |
| 105 |
``` |
| 106 |
|
| 107 |
### Deploy the application |
| 108 |
|
| 109 |
This sample provides a script for building the Guestbook binary and deploying it |
| 110 |
to the Kubernetes cluster created by Terraform. |
| 111 |
|
| 112 |
```shell |
| 113 |
go run deploy/main.go |
| 114 |
``` |
| 115 |
|
| 116 |
The deploy script will display the URL of your running service. |
| 117 |
|
| 118 |
### Cleanup |
| 119 |
|
| 120 |
To clean up the created resources, run `terraform destroy` inside the `gcp` |
| 121 |
directory using the same variables you entered during `terraform apply`. |
| 122 |
|
| 123 |
## Running on Amazon Web Services (AWS) |
| 124 |
|
| 125 |
If you want to run this sample on AWS, you need to set up an account, download |
| 126 |
the [AWS command line interface][], and log in. There's |
| 127 |
[help here][AWS Config Help] if you need it. |
| 128 |
|
| 129 |
```shell |
| 130 |
aws configure |
| 131 |
``` |
| 132 |
|
| 133 |
[AWS command line interface]: https://docs.aws.amazon.com/cli/latest/userguide/installing.html |
| 134 |
[AWS Config Help]: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html |
| 135 |
|
| 136 |
### Agree to the Debian Terms and Conditions |
| 137 |
|
| 138 |
You have to agree to the [Debian Terms and Conditions][] in order to provision |
| 139 |
the resources. Click through the "Continue to Subscribe" button at the top, then |
| 140 |
log in to your AWS account and subscribe to Debian. |
| 141 |
|
| 142 |
[Debian Terms and Conditions]: https://aws.amazon.com/marketplace/pp?sku=55q52qvgjfpdj2fpfy9mb1lo4 |
| 143 |
|
| 144 |
### SSH Key |
| 145 |
|
| 146 |
You will also need an SSH key to SSH into the EC2 instance. If you don't already |
| 147 |
have one, you can follow [this guide from GitHub][GitHub SSH]. Follow the |
| 148 |
instructions for "Adding your key to the ssh-agent" if you want the key to |
| 149 |
persist across terminal sessions. |
| 150 |
|
| 151 |
```shell |
| 152 |
ssh-add |
| 153 |
``` |
| 154 |
|
| 155 |
[GitHub SSH]: https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/ |
| 156 |
|
| 157 |
### Provision resources |
| 158 |
|
| 159 |
You can now use Terraform to set up your project. This will create an EC2 |
| 160 |
instance you can SSH to and run your binary. |
| 161 |
|
| 162 |
```shell |
| 163 |
# Build for deploying on the AWS Linux VM. |
| 164 |
GOOS=linux GOARCH=amd64 go build |
| 165 |
|
| 166 |
# Enter AWS directory from samples/guestbook. |
| 167 |
cd aws |
| 168 |
terraform init |
| 169 |
|
| 170 |
# Provisioning can take up to 10 minutes. |
| 171 |
# Keep track of the output of this command as it is needed later. |
| 172 |
# You can replace us-west-1 with whatever region you want. |
| 173 |
terraform apply -var region=us-west-1 -var ssh_public_key="$(cat ~/.ssh/id_rsa.pub)" |
| 174 |
``` |
| 175 |
|
| 176 |
### Deploy the application |
| 177 |
|
| 178 |
You now need to connect to the new remote server to execute the `guestbook` |
| 179 |
binary. The final output of `terraform apply` lists the variables `guestbook` |
| 180 |
requires as arguments. Here's an example, with actual strings replaced with |
| 181 |
`[redacted]`: |
| 182 |
|
| 183 |
```shell |
| 184 |
# Output from "terraform apply" command.... |
| 185 |
<snip> |
| 186 |
|
| 187 |
Outputs: |
| 188 |
|
| 189 |
bucket = [redacted] |
| 190 |
database_host = [redacted] |
| 191 |
database_root_password = <sensitive> |
| 192 |
instance_host = [redacted] |
| 193 |
paramstore_var = /guestbook/motd |
| 194 |
region = us-west-1 |
| 195 |
|
| 196 |
# Print out the database root password, since we'll need it below |
| 197 |
# Terraform hides it by default in the Outputs above. |
| 198 |
localhost$ terraform output database_root_password |
| 199 |
[redacted] |
| 200 |
|
| 201 |
# SSH into the EC2 instance. |
| 202 |
localhost$ ssh "admin@$( terraform output instance_host )" |
| 203 |
|
| 204 |
# Fill in each command-line argument with the values from the |
| 205 |
# Terraform output above. |
| 206 |
server$ AWS_REGION=<your region> ./guestbook \ |
| 207 |
-env=aws \ |
| 208 |
-bucket=<your bucket> \ |
| 209 |
-db_host=<your database_host> \ |
| 210 |
-db_user=root \ |
| 211 |
-db_password=<your database_root_password> \ |
| 212 |
-motd_var=/guestbook/motd |
| 213 |
``` |
| 214 |
|
| 215 |
You can now visit the server at `http://<your instance_host>:8080/`. |
| 216 |
|
| 217 |
### Cleanup |
| 218 |
|
| 219 |
To clean up the created resources, run `terraform destroy` inside the `aws` |
| 220 |
directory using the same variables you entered during `terraform apply`. |
| 221 |
|
| 222 |
## Running on Azure |
| 223 |
|
| 224 |
The Go CDK doesn't have support for SQL on Azure yet ([#1305][]), so we'll run |
| 225 |
MySQL and the binary locally. This tutorial will show how to use Azure storage |
| 226 |
for the MOTD and Gopher logo. |
| 227 |
|
| 228 |
You first need to set up an Azure account and install the [Azure CLI][]. Then, |
| 229 |
use the `az` CLI to log in. |
| 230 |
|
| 231 |
[#1305]: https://github.com/google/go-cloud/issues/1305 |
| 232 |
[Azure CLI]: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest |
| 233 |
|
| 234 |
```shell |
| 235 |
az login |
| 236 |
``` |
| 237 |
|
| 238 |
### Provision resources |
| 239 |
|
| 240 |
We'll use Terraform to set up your project. |
| 241 |
|
| 242 |
```shell |
| 243 |
# Enter the Azure directory from samples/guestbook. |
| 244 |
cd azure |
| 245 |
terraform init |
| 246 |
|
| 247 |
# Provisioning can take up to 10 minutes. |
| 248 |
# Keep track of the output of this command as it is needed later. |
| 249 |
terraform apply -var location="West US" |
| 250 |
|
| 251 |
<snip> |
| 252 |
Outputs: |
| 253 |
|
| 254 |
access_key = [redacted] |
| 255 |
storage_account = [redacated] |
| 256 |
storage_container = [redacted] |
| 257 |
``` |
| 258 |
|
| 259 |
### Deploy the application |
| 260 |
|
| 261 |
You will need to run a local MySQL database server, similar to what we did for |
| 262 |
running locally earlier. Open a new terminal window, and run: |
| 263 |
|
| 264 |
```shell |
| 265 |
cd .. # back up to samples/guestbook |
| 266 |
go get ./localdb/... # Get package dependencies. |
| 267 |
go run localdb/main.go |
| 268 |
``` |
| 269 |
|
| 270 |
In the original terminal, add your Azure credentials to the environment and run |
| 271 |
the `guestbook` application: |
| 272 |
|
| 273 |
```shell |
| 274 |
# You should be in the "samples/guestbook/azure" directory. |
| 275 |
|
| 276 |
# Enter the storage_account from the Terraform output earlier. |
| 277 |
export AZURE_STORAGE_ACCOUNT=<your storage_account> |
| 278 |
# Enter the access_key from the Terraform output earlier. |
| 279 |
export AZURE_STORAGE_KEY=<your access_key> |
| 280 |
|
| 281 |
# Run the binary. |
| 282 |
# Fill in the -bucket command-line argument with the value from the Terraform |
| 283 |
# output. |
| 284 |
# |
| 285 |
./guestbook -env=azure -bucket=<your storage_container> -motd_var=motd |
| 286 |
``` |
| 287 |
|
| 288 |
Your server is now running on http://localhost:8080/. |
| 289 |
|
| 290 |
You can stop the MySQL database server with Ctrl-\\. MySQL ignores Ctrl-C |
| 291 |
(SIGINT). |
| 292 |
|
| 293 |
### Cleanup |
| 294 |
|
| 295 |
To clean up the created resources, run `terraform destroy` inside the `azure` |
| 296 |
directory using the same variables you entered during `terraform apply`. |
| 297 |
|
| 298 |
## Gophers |
| 299 |
|
| 300 |
The Go gopher was designed by Renee French and used under the |
| 301 |
[Creative Commons 3.0 Attributions](https://creativecommons.org/licenses/by/3.0/) |
| 302 |
license. |