Skip to content

LAB4_Set environment variables in your application

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

⚒️ Set environment variables in your application

Exercise time: ~5 minutes

Objectives

In this step, we will:

  • Configure a .env file with database environment variables

We will cover:

  1. Create and paste variables into an env file

1. Create and paste variables into an env file

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 create the .env file in our application to store our database information.

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

✅ Step 1a: Ensure that you are in the "battlestax" directory

✅ Step 1b: Copy the contents of the .env.example file into an .env file.

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.

📘 Command to execute

cat .env.example > .env

✅ Step 1c: Go back to the Astra UI and click the CONNECT button above the display of the database you just created.

This will bring you to the Connect page.

Database connect

✅ Step 1d: Click the clipboard button to the far right to copy all of your environment variables to the copy buffer.

The Document API option should already be chosen by default. Notice the copy widget on the right of the UI.

Document API

✅ Step 1e: Open the .env file you created a moment ago.

You can simply double click the .env file to open and edit it in the GitPod IDE.

Gitpod paste to env

✅ Step 1f: When you open the .env file notice the GAMES_COLLECTION=games variable. Don't overwrite this. We'll need it here in a moment.

✅ Step 1h: Now paste in the values you copied from Astra.

✅ Step 1i: Ensure to delete the word "export" from each line as we don't need those inside our .env file.

It should look something like this

Gitpod paste to env with games

After deleting "export" from each line, then this

Gitpod paste to env end result

✅ Step 1j: Finally, update the ASTRA_DB_PASSWORD value <database_password> with the password you used for your database.

✅ Step 1k: Save your changes.

When completed, your .env file should look something like this: Netlify Setup Example

Now you are set to run your application locally against your Astra database. We'll test all of that in an upcoming section. For now, let's move to the next section.

Clone this wiki locally