Storing Your Terraform Code in GitLab
In Part 1 I put an Okta org under Terraform on a Debian server. In this part I store that code in GitLab, so it is versioned, backed up and ready to share. Every command is followed by a short explanation, and every code box has a Copy button.
terraform plan and terraform apply myself, and then commit and push the result to GitLab. The push does not change Okta. A pipeline that runs Terraform from GitLab is a later step.Step 1: Create an SSH key
GitLab needs to know that pushes come from you. An SSH key pair does that without typing a password each time.
ssh-keygen -t ed25519 -C "theo@home-theo" -f ~/.ssh/id_ed25519_gitlabThis creates a key pair. -t ed25519 is a modern, short key type, -C is just a label, and -f sets the file name. It asks for a passphrase: choose one, because it protects the key if the file is ever copied.
The fingerprint and the random square of characters that appear are safe to share. The private key file (id_ed25519_gitlab, without .pub) and your passphrase never are.
Print the public key and copy the whole line, which starts with ssh-ed25519:
cat ~/.ssh/id_ed25519_gitlab.pubStep 2: Add the public key to GitLab
In GitLab, click your avatar (top left), then Edit profile.
Go to SSH Keys and click Add new key. Paste the public key, set the title to home-theo, and click Add key.
Step 3: Tell SSH to use the key for GitLab
nano ~/.ssh/configThis opens (or creates) the file that controls how SSH connects to each server. Paste this, then save with Ctrl+O, Enter, and exit with Ctrl+X:
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab
IdentitiesOnly yesWhenever you connect to gitlab.com, SSH logs in as the user git and uses only this key.
Restrict the file so only you can read it:
chmod 600 ~/.ssh/configTest the connection:
ssh -T git@gitlab.comThe first time, answer yes to the fingerprint question and enter your passphrase if you set one. Success looks like Welcome to GitLab, @yourusername!
Step 4: Create the project in GitLab
Click New project, then Create blank project.
Set Visibility Level to Private.
After you click Create project, GitLab shows instructions for an empty project. Use the section Push an existing Git repository. Ignore the other two, because they would create a second repository.
Step 5: Link your local repository and push
Go to the local repository and set the name and email recorded on your commits, for this repo only:
cd ~/okta-terraform
git config --local user.name "Your Name"
git config --local user.email "you@example.com"Link the local repository to the GitLab project. Copy the address from Code > Clone with SSH in GitLab:
git remote add origin git@gitlab.com:it-systems-theo/Terraform-integrator-4574553.gitThis tells Git that the GitLab project is called origin. Check it:
git remote -vIt lists two lines, one for fetch and one for push, with the same address.
git ls-files. It lists every tracked file, and you should see only .gitignore, your .tf files and .terraform.lock.hcl. If a .pem, .env or .tfstate file appears, stop and remove it from Git first.git ls-filesPush your main branch:
git push --set-upstream origin mainThis uploads your commits. --set-upstream remembers the link, so later a plain git push is enough. If your key has a passphrase, it asks for it now (nothing shows as you type).
Step 6: Make a change: add a group and a rule
Now the real workflow. Edit the Terraform code, apply it locally, then store the change in GitLab.
Move to the Terraform folder. This is not the project root:
cd ~/okta-terraform/envs/okta-integrator-4574553Open main.tf (or edit it in VS Code):
nano main.tfAdd this at the very bottom, below the existing blocks. Don't delete anything:
resource "okta_group" "it_support" {
name = "IT Support"
description = "Managed by Terraform"
}
resource "okta_group_rule" "it_support" {
name = "IT Support by department"
status = "ACTIVE"
group_assignments = [okta_group.it_support.id]
expression_type = "urn:okta:expression:1.0"
expression_value = "user.department==\"IT\""
}The first block creates a group named IT Support. The second block automatically adds users whose department equals IT to that group. It refers to the group with okta_group.it_support.id, so Terraform creates the group first.
Step 7: Check, plan and apply locally
Load your Okta credentials, tidy the formatting and check the syntax:
source ~/.okta/okta-integrator-4574553.env
terraform fmt
terraform validatesource loads your credentials into this terminal. terraform fmt formats the files. terraform validate checks the syntax without contacting Okta, and you want Success! The configuration is valid.
Preview the changes:
terraform planThis logs in to Okta and shows what would change, without changing anything. A good result ends with Plan: 2 to add, 0 to change, 0 to destroy.
Apply the changes:
terraform applyType yes when asked. It should end with Apply complete! Resources: 2 added, 0 changed, 0 destroyed. Then check Directory > Groups in the Okta Admin Console.
Step 8: Commit and push the change to GitLab
Only now does the change go to GitLab, as a record of what you applied. Go to the project root and check what changed:
cd ~/okta-terraform
git statusgit status should list only main.tf as modified. Stage it by name, commit it and push it:
git add envs/okta-integrator-4574553/main.tf
git commit -m "Add IT Support group and rule"
git pushgit addstages the filegit commitrecords it in local history with a messagegit pushuploads the commit to GitLab
Your daily routine from now on:
git status
git add FILENAME
git commit -m "What changed"
git pushWhat GitLab does and does not do here
- GitLab: stores and versions the code, so you have history and a backup.
- The server: runs Terraform, holds the credentials in
~/.oktaand keeps the state file. - You: are the automation. A push to GitLab does not change Okta.
Next: a GitLab pipeline that runs terraform plan on merge requests and apply on main. That needs remote state, a runner and the Okta credentials stored as CI variables, so I'll cover it in the next part.










