Today I Learned

9 posts by łukaszpawlik

Terraform Cloud settings for multiconfiguration repo

If you have a repository with more then one Terraform configuration you need to set Terraform Working Directory in workspace general settings to the subdirectory that the configuration you want to run.

When running Terraform remote operations they will be executed from the working directory if it’s empty it defaults to the root directory. Before starting a Terraform run Terraform Cloud will change to this directory so all the relative modules paths that you had previously defined will work correctly.

Conditional .gitconfig

You can have conditional configuration based on path that you are in for git. For example if you have two emails defined one for work and one personal you can tell git to load gitconfig based on path you are in.

In your .gitconfig add the following lines:

[includeIf "gitdir:~/work/"]              
        path = ~/work/.gitconfig           

And then create a .gitconfig file in the path above with whatever settings you need. A simple one would be to change the email of the user.

        email = ""

Git will load this file also for all the subdirectories of the work dir.

Persisting files between jobs in CircleCI's worklow

When you have a file or files that you want to persist between jobs in Workflow you can add the step persist_to_workspace in the job that creates the files.

For example:

Assuming that you have a job that builds JS project and creates a zip under builds/ in the workdir directory you can add step:

      - persist_to_workspace:
          root: .
            - builds/*

And in the followup jobs you need to call

      - attach_workspace:
          at: ./

to have access to those files. The files will be accessible under ./builds directory.


Use templatefile function instead of template_file

Since terraform v 0.12 it’s recommended to use templatefile(path, vars) function instead of data source template_file.

As data source provider has it’s own copy of template file engine that is separate from the Terraform itself. The engine depends on the version of Terraform that the provider was compiled with and not the one run on your system.

Using templatefile(path, vars) function will give you more consistent results as it depends on the version of Terraform you are using.

Example usage:

templatefile("${path.module}/backends.tmpl", { port = 8080, ip_addrs = ["", ""] })


Get key's value from Vault secret with multiple keys

Let’s assume that you have defined a secret in vault with multiple key’s defined:

➜ vault kv get secret/example
====== Metadata ======
Key              Value
---              -----
created_time     2020-03-12T12:25:24.912324854Z
deletion_time    n/a
destroyed        false
version          3

==== Data ====
Key      Value
---      -----
test     test1234
test2    dawdawaw

You can retrieve value from a specific key with a flag -field=key_name. For example:

➜ vault kv get -field=test2 secret/example

How to make path accessible in Rails Engine

Since routes in engine are isolated by default from main application so that you can have the same routes without clashing using default route helpers like about_path from main app will result in an error when you try to load it from inside the engine or access engine root from main app.

To fix that issue assuming you have an engine mounted:

Blorgh::Engine.routes.draw do
  resources :articles

to enforce using engine roots you need to change about_path to blorgh.about_path and to enforce using main application routes you need to change it to main_app.about_path