Skip to content

LAB4_Set environment variables in your application

David Jones-Gilardi edited this page Feb 23, 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
  2. Explore the API with HTTPie

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: 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 1b: 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 1c: Ensure that you are in the "battlestax" directory wherever you are editing code (local or gitpod) and execute the following command to add your database environment variables to your code project.

πŸ“˜ Command to execute

/workspace/battlestax/env.sh

βœ… Step 1d: Follow the prompts to paste your copied variables and enter your password.

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

Gitpod paste to env end result

2. Explore the API with HTTPie (OPTIONAL)

Just to note, this section is completely optional, but we wanted to offer you a quick and easy way to test against the database credentials we just set before we get to the code section. Feel free to move on to the next section if you would like to.

Test the database with HTTPie

βœ… Step 2a: Install HTTPie (LOCAL)

If you're using GitPod, we've already installed httpie for you, however, if you are running on a local IDE you'll need to install it.

πŸ“˜ Command to execute

pip3 install httpie-astra

βœ… Step 2b: Create a document

βœ… Step 2c: Retrieve the document you just created

πŸ“˜ Command to execute

http --auth-type astra -a default: :/rest/v2/namespaces/battlestax/collections/games | jq ".data"

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.

🏠 Home

🏁 I - What is the JamStack?

Introduction to the JAMStack Why this is cool ? Introduction to Netlify Want to learn more ? πŸ› οΈ II - Setup and deploy your first app

Create your BattleStax repository Setup Netlify account Summary πŸ› οΈ III - Create your Astra instance

Register and Sign In to Astra Configure and create your database Activate Cassandra awesome πŸ“š IV - What can Netlify do for you

Build, Package, deploy, host Advanced features Netlify Functions Want to learn more πŸ› οΈ V - Expose your "hello world" API

Setup your environment Make a serverless endpoint using Netlify functions Merge back to master Check your deployment in Netlify Summary πŸ“š VI - What are DataStax Astra and Stargate

Introduction to Astra Introduction to Stargate Want to know More πŸ› οΈ VII - Set environment variables in your application

Creating the .env file Explore the API with HTTPie πŸ› οΈ VIII - Set secrets in GitHub for CI/CD

Configure secrets in GitHub Verify your secrets How is this all working ? πŸ› οΈ IX - Set environment variables in Netlify

Set environment variables in Netlify Verify your environment variables Summary πŸ› οΈ X - Implement a CRUD Api in Astra

Creating the insertGame Netlify endpoint Connect to Astra Hook it all together Running TDD tests πŸ› οΈ XI - Verify and Deploy in Netlify

Merge back to master Verify your deployment in Netlify Feel the enormity of your accomplishment Super secret full game option πŸ“š XII - Resources

Clone this wiki locally