Setting Up Terraform on Debian
Managing Okta by clicking through the Admin Console works, until you need a second environment or want to know who changed what. In this post I put an Okta org under Terraform on a minimal Debian 13 server, step by step. Every command is followed by a short explanation, and every code box has a Copy button.
What you need first
- A Debian 13 machine (I used a minimal install over SSH) and an Okta org with admin access.
- An OAuth service app with a private key, not an API token. A token has the full rights of the admin who created it and expires after 30 days unused. A service app only gets the scopes and role you give it.
Step 1: Install Terraform
Minimal Debian has no sudo. If you get sudo: command not found, run su -, then apt-get install -y sudo and usermod -aG sudo YOURUSER, then log out and back in.
First, install the tools needed to add HashiCorp's package repository:
sudo apt-get update && sudo apt-get install -y gnupg curl lsb-releaseapt-get update refreshes the list of available packages. && runs the second command only if the first succeeds. The install adds three tools:
gnupgchecks cryptographic signaturescurldownloads fileslsb-releasereports your Debian codename (for example trixie)
Download HashiCorp's signing key:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpgThis downloads the public key (wget -O- prints it instead of saving it), pipes it (|) into gpg --dearmor, which converts it to the binary format apt expects, and saves it in the keyrings folder. apt uses it to verify that packages really come from HashiCorp.
Add the HashiCorp repository:
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.listThis writes one line into a new apt source file, telling Debian where HashiCorp's packages live and which key to trust. $(lsb_release -cs) fills in your Debian codename, and tee with sudo lets you write into a protected folder. If you get a 404 later, replace the codename with bookworm.
Install Terraform and check the version:
sudo apt-get update && sudo apt-get install -y terraform
terraform -versionThe first line refreshes the package list (now including HashiCorp) and installs Terraform. The second prints the version, which proves it works. This guide needs 1.10 or higher.
Step 2: Create the service app in Okta
In the Admin Console, go to Applications > Applications > Create App Integration. Choose API Services, name it Terraform, and keep the default Okta-generated client ID.
On the General tab, under Client Credentials, choose Public key / Private key.
Now add a public key: click Add > Generate new key and choose the key size (I used 4096-bit).
The private key is shown only once. Select the PEM tab (not JSON) and click Copy to clipboard. Keep this dialog open while you save the key on your server (next step).
Next, on the Okta API Scopes tab, grant the scopes you need, for example okta.groups.manage. A typical set:
okta.groups.manage
okta.apps.manage
okta.policies.manage
okta.authorizationServers.manage
okta.users.manage
okta.idps.manage
okta.trustedOrigins.manage
okta.schemas.manage
okta.networkZones.manage
okta.brands.manageScopes alone are not enough: the app also needs an admin role. On Admin roles, assign Super Administrator to start with, and narrow it to a custom role later.
Finally, copy the Client ID from the top of the General tab. It is not a secret.
Step 3: Store the credentials on the server
Back in the terminal. Create a private folder for secrets in your home directory:
mkdir -p ~/.okta && chmod 700 ~/.okta-p means no error if it already exists. chmod 700 makes the folder accessible only to you.
Create the key file, paste the PEM you copied, press Ctrl+O then Enter to save, and Ctrl+X to exit:
nano ~/.okta/terraform-okta-integrator-4574553.pemMake the key readable and writable by you only, then check the first line without showing the key:
chmod 600 ~/.okta/terraform-okta-integrator-4574553.pem
head -1 ~/.okta/terraform-okta-integrator-4574553.pemThe second command should print -----BEGIN PRIVATE KEY-----.
Create the credentials file:
nano ~/.okta/okta-integrator-4574553.envPaste this, replacing the Client ID and KID with your real values from Okta (not the placeholders):
export OKTA_ORG_NAME="integrator-4574553" # yourorg.okta.com -> "yourorg"
export OKTA_BASE_URL="okta.com" # or oktapreview.com / okta-emea.com
export OKTA_API_CLIENT_ID="YOUR_CLIENT_ID"
export OKTA_API_PRIVATE_KEY_ID="YOUR_KID"
export OKTA_API_PRIVATE_KEY="$(cat ~/.okta/terraform-okta-integrator-4574553.pem)"
export OKTA_API_SCOPES="okta.groups.manage,okta.apps.manage,okta.policies.manage,okta.authorizationServers.manage,okta.users.manage"What each line does:
OKTA_ORG_NAME: the first part of your Okta URLOKTA_BASE_URL: the domain, usually okta.comOKTA_API_CLIENT_ID: which app is logging inOKTA_API_PRIVATE_KEY_ID: the KID of the registered keyOKTA_API_PRIVATE_KEY: the contents of the PEM, read in by$(cat ...)OKTA_API_SCOPES: the permissions requested
Restrict the file to you only:
chmod 600 ~/.okta/okta-integrator-4574553.envLoad the variables into your current terminal:
source ~/.okta/okta-integrator-4574553.envRun this in every new terminal, and again after editing the file, because a terminal keeps the old values until you reload.
Optional: load the credentials automatically
To avoid typing source each time, add it to your shell startup file:
nano ~/.bashrcAdd this line at the very bottom, save, then load it in the current terminal:
source ~/.okta/okta-integrator-4574553.envsource ~/.bashrcStep 4: Set up Git and the project folders
Install Git, which tracks changes to your files:
sudo apt-get install -y gitTell Git who you are (used in commit history) and name the default branch main:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch mainCreate the project folder, with one subfolder per Okta org. That keeps each org's code and state separate:
mkdir -p ~/okta-terraform/envs/okta-integrator-4574553Move into the project root and turn it into a Git repository:
cd ~/okta-terraform
git initCreate the file that tells Git what never to track:
nano .gitignore.terraform/
*.tfstate
*.tfstate.*
*.tfvars
*.pem
*.env
key.json
crash.log.pem and .env files in ~/.okta, outside the repo, and make any GitHub repo private. Do not ignore .terraform.lock.hcl; that one should be committed.Save the ignore file as the first commit:
git add .gitignore
git commit -m "Add .gitignore"git add stages the file and git commit records it in history with a message.
Step 5: Write the Terraform files
Move into the folder where Terraform runs. All terraform commands go here:
cd ~/okta-terraform/envs/okta-integrator-4574553versions.tf pins the versions. It needs Terraform 1.10 or newer and the Okta provider from the 5.x series. With no backend block, state stays in a local file.
nano versions.tfterraform {
required_version = ">= 1.10"
required_providers {
okta = {
source = "okta/okta"
version = "~> 5.0"
}
}
}provider.tf sets rate-limit and retry behaviour. It uses at most 50% of Okta's API rate limit and waits and retries when limited. There are no credentials here, because they come from the OKTA_* variables.
nano provider.tfprovider "okta" {
max_api_capacity = 50
backoff = true
min_wait_seconds = 5
max_wait_seconds = 60
max_retries = 5
}main.tf holds the actual resources, a small first test:
nano main.tfdata "okta_group" "everyone" {
name = "Everyone"
}
resource "okta_group" "engineering" {
name = "Engineering"
description = "Managed by Terraform"
}
resource "okta_group_rule" "engineering" {
name = "Engineering by department"
status = "ACTIVE"
group_assignments = [okta_group.engineering.id]
expression_type = "urn:okta:expression:1.0"
expression_value = "user.department==\"Engineering\""
}data "okta_group"only reads the built-in Everyone group and changes nothing. Built-in objects cannot be created or deleted, so you reference them with a data source.resource "okta_group"creates a group named Engineering.resource "okta_group_rule"automatically adds users whose department equals Engineering.
Step 6: Run Terraform
Download the Okta provider plugin and create .terraform.lock.hcl, which records the exact versions. This step needs no credentials:
terraform initCheck the files for syntax errors without contacting Okta:
terraform validateShow what would be created, changed or deleted, without changing anything. This is the first step that logs in to Okta, so credential problems appear here:
source ~/.okta/okta-integrator-4574553.env
terraform planA good result ends with Plan: 2 to add, 0 to change, 0 to destroy. Then apply it:
terraform applyTerraform shows the plan again and, after you type yes, creates the group and rule. Check them in the Admin Console under Directory > Groups.
Commit your work
Go to the project root and check what Git sees. No .pem, .env or .tfstate file should appear in the list:
cd ~/okta-terraform
git statusAdd the files by name (safer than git add .) and commit:
git add envs/okta-integrator-4574553/*.tf envs/okta-integrator-4574553/.terraform.lock.hcl
git commit -m "Add Okta Terraform config"








No comments:
Post a Comment