Static Application Configuration
Warning
This content is part of the legacy version of Waypoint that is no longer actively maintained. For additional information on the new vision of Waypoint, check out this blog post and the HCP Waypoint documentation.
Modern cloud infrastructure is built of reusable components that can be modified at build or deploy time for a specific purpose. Waypoint can build applications with custom configuration and can even restart applications when configuration values change.
Challenge
Applications might read configuration values from a database, from a shared key/value service, or from the immediate runtime environment. Waypoint can provide values to applications via a configuration file or from the command line.
Solution
In this tutorial you will run a Go language application and configure it in two ways.
- Write a configuration value in
waypoint.hcl
- Set a value from the command line with
waypoint config set
Both values will be displayed on a web page that is served by the Go application.
Note
These techniques work for non-sensitive information. This tutorial does not demonstrate Waypoint's ability to sync values from Vault or Kubernetes.
Prerequisites
You should be familiar with the basics of running a Waypoint application with
the waypoint
binary. Follow the [Waypoint on Docker tutorials][/collections/waypoint/get-started-docker] to learn the basics.
You must also:
- Clone the code examples repository
- Install and run Docker or Docker Desktop
Clone the examples repository
This tutorial uses a Go language application to display environment variables you set with Waypoint. You do not need to know how to write Go applications. We will show a few lines of code in multiple languages so you can become familiar with working with environment variables using Waypoint.
Clone the Waypoint examples repository which contains the sample Go application.
Navigate to the waypoint-examples/learn/static-application-configuration
directory.
Open the static-application-configuration
directory in a text editor.
Install and run Docker Desktop
Install the most recent stable version of Docker or Docker Desktop.
Start the Docker daemon on Linux.
Set up the Waypoint server
Waypoint runs as a server and a client, even if only run locally. For this tutorial, you'll run the server in Docker or Docker Desktop.
Install the Waypoint Server
Install the Waypoint server to Docker or Docker Desktop on your computer. This allows you to view the user interface, collaborate on deployments, and more.
Run the following command to install the Waypoint server. The -accept-tos
flag
confirms that you accept the terms of service for our application URL
service.
Examine the Go code
The main.go file defines a Go application that serves a minimal web page which displays information about a sale. You can configure the discount (10%, 20%, 50%) and the day that the sale ends on (Monday, Wednesday, Friday).
This code will:
- Read one or more environment variables
- Use the value of the variables to modify the behavior of the application
In order to read environment variables, you must import the os
module.
The main
function defines a saleViewHandler
which will render an HTML string
that displays the sale percentage and the day on which the sale ends.
Go and other languages are built with support for reading environment variables.
The saleViewHandler
function uses os.Getenv
to read the values of
SALE_PERCENT
and SALE_ENDS_ON
from the environment.
The Fprintf
line builds an HTML string to display these values. In a
production scenario, you would use a templating language such as Go
templates to define an HTML document with variables.
Use a hard-coded static configuration value
Now that you have a Go application that can display values, you will:
- Write a
waypoint.hcl
to populate theSALE_PERCENT
variable - Deploy the application
- Set a value with
waypoint config set
- View the updated application in your web browser
Examine the waypoint.hcl
file
We've provided a waypoint.hcl
file that sets a hard coded value for SALE_PERCENT
.
Configuration can be set at the project level or at the application level. For
this project, define an application-level variable in the config
section under
env
. You will only set the SALE_PERCENT
value in this way.
Deploy the application
You can debug or run the Go application locally with the go run
command. For
this tutorial, we will continue on to deploying the application with Waypoint.
Run the init
command to initialize the application.
Run waypoint up
to deploy the application to your local Docker instance.
Waypoint will compile the Go application inside a container and then deploy it
to Docker.
Visit the URL shown in the output.
You will see an application that displays the SALE_PERCENT
.
Set a configuration value on an existing application
The page looks incomplete since there is no value after "Until."
In the code for the Go application, we read a value for SALE_ENDS_ON
, but that
value has not been set. Let's set it from the command line. The sale will end on
Monday.
Waypoint will instantly restart the container with the new value. You must manually refresh the web page to view it.
You can also verify that the change occurred by opening the Waypoint admin console and looking at the logs.
You will observe lines that indicate Waypoint noticed a change in an environment variable.
Destroy the application
After you have completed this tutorial, destroy the deployment.
Next steps
Hard-coded configuration values in waypoint.hcl
are useful for values that do
not change often but that do change between scenarios. They can also be used if
you want to deploy a pre-built container with custom configuration.
For example, you could use Waypoint to deploy the generic MySQL Docker container with a random password and a database named "education."
When the container is deployed, the Waypoint logs will display the generated password and the name of the database that was created on startup. This can be achieved with an existing MySQL container and a few environment variables specified with Waypoint.
Reference
In this tutorial you learned to define Waypoint configuration values in the
waypoint.hcl
file or on the command line with waypoint config set
.
Refer to the documentation for other details on Waypoint configuration.