Skip to content

LAB4_Set environment variables in your application

David Jones-Gilardi edited this page Feb 16, 2021 · 24 revisions

⚒️ Set environment variables in your application

Objectives

In this step, we will:

  • Configure a .env file with database environment variables

We will cover:

  1. Set environment variables in code

✅ Step 1: Set environment variables in code

In the "hello world" section, we pushed a simple helloWorld "test" function to get a feel for how things work. Now, we are going to start working with the "real" code to get our game working. To do this, we need to set a group of environment variables referring to the database we just created with DataStax Astra, not only in our code, but across GitHub and Netlify as well.

Why are we doing this, you might ask? Because as part of our CI/CD pipeline our tests will attempt to connect to our data layer to ensure everything is hooked up and working. Not only that, but once you deploy your application to Netlify it will use these variables to hook up your production app and power your serverless functions.

We set these all ONE time and that's it, you are ready to go. With that, let's do it.

First things first, we need to get the values we are going to use for our variables. This section might look like a lot, but honestly, it's just a bunch of copy/paste.

✔ Go back to the Astra UI and click on the database you just created to get the details page.

Group 10

🟢 ASTRA_DB_USERNAME as battle_user (The user name we defined when creating the Astra instance)

🟢 ASTRA_DB_PASSWORD as battle_password1 (The password we defined when creating the Astra instance)

🟢 ASTRA_DB_KEYSPACE as battlestax (The keyspace we defined when creating the Astra instance)

🟢 ASTRA_DB_ID as the cluster ID of your Astra DB. To get your database ID and region go the ASTRA summary page. Locate the cluster ID and copy it by clicking the clickboard icon as shown above.

🟢 ASTRA_DB_REGION as the region you picked when creating the DB, It should be either us-east1 or europe-west1.

🟢 GAMES_COLLECTION as games (this is the collection where we will store all values)

Now, take these values and apply them to the following sections. You will use each one more than once FYI.

The following instructions are the same whether using GitPod or a local IDE.

✔ Ensure that you are in the ??battlestax??? directory

✔ Copy and paste the contents of the .env.template file into an .env file:

📘 Command to execute

cat .env.example > .env

✔ The .env file allows us to customize our own environmental variables. We set our Astra credentials to env variables, which are outside of our program. Fill in the .env file variables with the Astra variables you made a copy of above:

If you used different values for your database you will need to use those instead, otherwise just use the values we've provided.

ASTRA_DB_USERNAME=battle_user
ASTRA_DB_PASSWORD=battle_password1
ASTRA_DB_KEYSPACE=battlestax
ASTRA_DB_ID=[the value you retrieved above from YOUR database]
ASTRA_DB_REGION=[the value you retrieved above from YOUR database]
GAMES_COLLECTION=games

Netlify Setup Example

Clone this wiki locally