Managing feature flags through Terraform Cloud
Sometimes, it’s fun and useful to branch out. Today, I’m using Terraform to manage and create feature flags in LaunchDarkly.
Hold on… but why?
Have worked on a project where it was hard to test because it used other SaaS products? Then each developer needs to create and manage their own test accounts?
The promise of using Terraform besides how well-built and easy it is to use, is the power of creating infrastructure across various providers. With this Launch Darkly provider, developers could get easily on-boarded and each of them could have their own feature flag environment.
Here’s a great blog post by the Launch Darkly folks talking more about the why and some other examples.
Simple project
If you don’t have a Terraform Cloud account, it’s easy enough to get one. You don’t have to pay anything to get started.
When using Terraform Cloud, you create a workspace and configure a GitHub account to watch. Terraform Cloud will plan
the changes from the infrastructure as code and ask for confirmation. Then it will apply
them.
For Launch Darkly, we’ll just have to set an environment variable so that Terraform can use your account.
In this sample project, we’ll use this sample repo containing a miniature Launch Darkly environment and use Terraform Cloud to provision it.
Pre-Requisites
- Fork this repo
- Create a Terraform Cloud account (it’s free)
- Create a LaunchDarkly account (they have free trials)
The Steps
First, create the workspace as show above.
Select GitHub as the Version Control System.
Choose the repository you forked.
Then give the workspace a name and create.
It will take a minute.
Configuring LaunchDarkly
Next, for Terraform Cloud to be able to talk to Launch Darkly, you ought to create an API key.
Go to your user profile on Launch Darkly.
Then Authorization and create a new Token.
Give the API Token a name and choose role of Admin.
Create the API Token.
Copy the API Token to your clipboard.
Back to Terraform Cloud
Configure a terraform variable called launchdarkly_access_token
with the value from the API Token. Feel free to mark it as Sensitive.
Lastly, go to Settings then General.
Set the Terraform working directory to iac
.
This will make sure that Terraform Cloud is only watching the iac
directory. The example GitHub repo has all the Terraform files under an iac
subdirectory.
Give it a go
Queue the plan.
Confirm and apply when it asks you if you want to continue.
Check LaunchDarkly, you should have a new project, environment, and feature flag that’s on!
Stretch goal - Turn off your feature flag by committing to the repo. Terraform Cloud will pick up the change and apply it. That goes for any change you make!