thiagowfx's avatar

¬ just serendipity 🍀 (not just serendipity)

1Password CLI + direnv integration

• 246 words • 2 min • updated

⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.

Let’s stay you’re working on a terraform module that has a sensitive variable named api_key in variables.tf.

When running terraform apply, you’ll need to supply its value. Every. Single. Time.

We can make it persistent by setting export TF_VAR_api_key={my value}. However this is only persistent within the current shell.

To make it persistent across multiple sessions, create a .envrc file at the root module:

shell
#!/bin/sh
export TF_VAR_api_key={my value}

Then source it: source ~/.envrc. Now you have to source it every single time you open a new shell.

We can use direnv to do so automatically. It has been previously covered. But…now we have a secret stored as plain text in our filesystem.

We could store it in a more secure place, like a password manager, and then have a mechanism automatically fetch its value.

First approach #

When using 1Password, the 1Password CLI is such a mechanism.

shell
#!/bin/sh
export TF_VAR_api_key=$(op run --no-masking -- op read "op://{vault name}/{entry name}/{item name}")

You can fetch the secret reference path from 1Password: docs. Click “Copy Secret Reference” in the corresponding entry item.

Second approach #

The above is one possibility, and it works flawlessly, but there’s a second approach that uses op run alone with a secret reference that is dynamically replaced.

Create an .env file:

shell
TF_VAR_api_key="op://{vault name}/{entry name}/{item name}"

Update the .envrc file:

shell
#!/bin/sh
dotenv

Now run terraform apply via op run, as follows:

shell
op run --env-file=.env -- terraform apply

References #