I Provisioned My Keycloak Infrastructure With Terraform. Here’s What I’d Automate
Keycloak Terraform automation gives teams a way to manage identity infrastructure through version-controlled configuration files. Realms, applications, roles, and the infrastructure supporting them all need consistent co
Keycloak Terraform automation gives teams a way to manage identity infrastructure through version-controlled configuration files. Realms, applications, roles, and the infrastructure supporting them all need consistent configuration, especially when a SaaS product moves from development to staging and production.
I wanted to explore how far I could take this approach with Skycloak. The goal was simple: define the Keycloak environment in Terraform, review the planned changes, and apply the configuration without manually building every component through the dashboard.
Skycloak has its own Terraform provider, which makes this workflow more interesting. It can provision a managed Keycloak cluster and configure parts of the platform around it. The provider also works alongside the community Keycloak provider, which handles deeper configuration inside an existing Keycloak instance.
In this article, I'll look at what Keycloak Terraform automation can handle, which tasks are worth putting into code, and how to start with a small configuration that is easy to understand and maintain.
TL;DR
You can automate Keycloak infrastructure with Terraform by declaring the resources you need in configuration files and applying them through a Terraform provider. With Skycloak's provider, you can provision a managed Keycloak cluster, create realms, configure supported platform features, and keep those changes in version control.
The most useful things to automate first are:
- Cluster provisioning: Define the Keycloak cluster, its size, version, and deployment region.
- Realm creation: Create and manage the realms your applications use.
- Identity configuration: Manage supported users, roles, groups, clients, and identity providers.
- Platform security: Configure supported features such as WAF rules and IP access controls.
- Environment consistency: Keep infrastructure configuration reviewable and repeatable across environments.
The best starting point is a small Terraform project that provisions one cluster and one realm. Once that workflow is clear, you can expand the configuration to cover more of your identity infrastructure.
What Can You Automate With Terraform and Keycloak?
Terraform uses declarative configuration: you describe the infrastructure and resources you want, and Terraform works out the changes required to reach that state.
For Keycloak, this matters because identity configuration tends to grow with an application. A development environment might start with one realm and one client, while production adds more applications, roles, identity providers, security rules, and customer environments.
Managing every change manually makes it harder to reproduce the same setup elsewhere. Keycloak Terraform automation gives the team a consistent configuration that can be reviewed, shared, and maintained alongside application code.
Two Terraform providers, two different responsibilities
There are two providers worth understanding before writing your first configuration.
The official Skycloak Terraform provider communicates with the Skycloak API. It can provision the managed Keycloak cluster itself and manage platform-level resources such as custom domains, cluster security, and branding. It also supports core realm resources, including realms, users, roles, groups, basic clients, and identity providers.
The community keycloak/keycloak provider connects to an existing Keycloak instance through its Admin API. It manages Keycloak configuration such as realms, clients, client scopes, authentication flows, and protocol mappers.
These providers solve different parts of the same problem. The Skycloak provider handles managed infrastructure and supported platform resources, while the community provider covers deeper Keycloak configuration. You can use both in one Terraform project when your setup requires them.
For a more detailed walkthrough, see Skycloak's guide on using Terraform to set up and configure Keycloak.
Using Both Providers in the Same Terraform Workflow
The two providers can also work together in one Terraform setup. The Skycloak provider provisions the managed Keycloak cluster, while the community provider uses the cluster's credentials to configure deeper Keycloak settings.
Skycloak provides the skycloak_cluster_credentials data source to retrieve the credentials needed by the community provider:
data "skycloak_cluster_credentials" "production" {
cluster_id = skycloak_cluster.production.id
}
provider "keycloak" {
url = skycloak_cluster.production.url
realm = "master"
client_id = data.skycloak_cluster_credentials.production.client_id
client_secret = data.skycloak_cluster_credentials.production.client_secret
}
There is one important deployment detail: the Keycloak cluster must exist before the community provider can connect to it. I would handle this in two stages: first, run terraform apply to provision the cluster with the Skycloak provider; then, run a second terraform apply to configure the realm with the community provider. You can also keep these stages in separate Terraform projects.
What a Terraform configuration looks like
A Terraform project starts with configuration files that declare the providers and resources. For example, this is the basic shape of a Skycloak configuration:
terraform {
required_providers {
skycloak = {
source = "sky-cloak/skycloak"
version = "~> 0.16"
}
}
}
provider "skycloak" {}
resource "skycloak_cluster" "production" {
name = "production"
type = "keycloak"
size = "small"
version = "26.8.2"
location = "us"
}
resource "skycloak_realm" "application" {
cluster_id = skycloak_cluster.production.id
name = "application"
display_name = "Application Realm"
enabled = true
}
This configuration declares a managed Keycloak cluster and a realm attached to that cluster. The realm's cluster_id references the cluster resource, so Terraform understands the relationship and creates the cluster before creating the realm.
The provider reads its API key from the SKYCLOAK_API_KEY environment variable. This keeps the credential outside the configuration file and avoids placing a secret directly in your Terraform code.
The example also pins the provider to a version range. Keeping provider versions controlled makes infrastructure changes easier to review and reduces unexpected differences when someone runs the project on another machine.
This is the foundation of Keycloak Terraform automation: describe the resources, connect their dependencies, and let Terraform manage the changes.
What I Would Automate First in a Keycloak Environment
The biggest benefit of Keycloak Terraform automation comes from automating the resources that need to stay consistent across environments. I would start with the foundation of the deployment and gradually add the configuration that supports authentication and application access.
Here are the first areas I would put under Terraform management.
1. Keycloak clusters and deployment settings
The cluster is the foundation of a managed Keycloak environment. Its configuration includes the cluster name, Keycloak version, instance size, and deployment region.
With the Skycloak provider, I can declare these settings in Terraform and keep them alongside the rest of the infrastructure configuration. If the team needs a separate staging environment, it can use another cluster resource with its own settings.
This is useful when a project has multiple environments that need a consistent setup. The configuration also gives the team a clear record of the version and infrastructure settings used for each deployment.
2. Realms and application identity
Realms organize users, applications, roles, and authentication settings in Keycloak. Creating them through Terraform makes the initial identity setup reproducible.
For example, a SaaS product might use an application realm for its main authentication environment and a separate realm for an internal tool. The Skycloak provider supports realm creation and management, so these resources can be declared alongside the cluster.
I would also automate the basic identity resources that belong to the initial setup, such as realm roles, groups, users, and supported application clients. This gives the team a consistent starting point when a new environment is created.
3. Identity providers and application connections
Many applications support sign-in through external identity providers such as Google or Microsoft. These integrations involve configuration that needs to match across environments.
The Skycloak provider supports identity provider resources, which lets the team manage supported integrations through Terraform. Provider credentials should be passed through protected variables or environment-based secrets, not written directly into configuration files.
This approach makes the integration configuration easier to review while keeping sensitive credentials out of the repository.
4. Platform security and access controls
Security settings are another strong candidate for automation because they affect the infrastructure that protects the Keycloak deployment.
Skycloak's provider includes resources for cluster security, including Web Application Firewall configuration and IP access controls. The provider also supports other platform-level controls, such as rate limiting and geographic or bot rules.
For example, a team can define a WAF configuration in Terraform and keep the selected protection settings under version control. When the configuration changes, the team can review the planned update before applying it.
These controls are worth managing as code because they form part of the environment's security baseline, not just an occasional dashboard adjustment.
How I Would Organize a Terraform Project
A Terraform project becomes harder to maintain when every provider, resource, variable, and secret is placed in one large file. I prefer a small structure where each file has a clear responsibility.
For a Keycloak environment managed through Skycloak, I would start with this layout:
keycloak-infrastructure/
├── versions.tf
├── providers.tf
├── variables.tf
├── main.tf
├── outputs.tf
├── terraform.tfvars.example
└── .gitignore
Each file has a specific purpose:
-
versions.tf: Declares the required Terraform and provider versions. -
providers.tf: Configures the Skycloak provider. -
variables.tf: Defines reusable inputs such as the cluster name, size, Keycloak version, and region. -
main.tf: Declares the cluster, realm, and other infrastructure resources. -
outputs.tf: Exposes useful values returned by Terraform, such as resource identifiers. -
terraform.tfvars.example: Shows the input values needed to run the project without containing actual secrets. -
.gitignore: Prevents local state files, variable files, and other generated Terraform files from entering version control.
This structure is small enough for a first project but gives the configuration room to grow.
Keep environment configuration separate
Development, staging, and production should not share every infrastructure value. A development cluster might use a smaller size, while production needs its own deployment settings and security configuration.
Terraform variables make these differences easier to manage. For example, the cluster name, region, size, and Keycloak version can be defined as inputs and supplied separately for each environment.
For a growing project, I would keep environment-specific variable files in separate directories or use Terraform workspaces with a clearly defined state strategy. The important part is keeping each environment's state and configuration distinct so a change intended for development does not accidentally affect production.
Protect Terraform state and credentials
Terraform state records the resources Terraform manages and the information it needs to compare the declared configuration with the deployed infrastructure. It can also contain sensitive values, so it deserves the same protection as other infrastructure credentials.
I would follow three basic rules:
- Store the Skycloak API key in the
SKYCLOAK_API_KEYenvironment variable. - Keep
terraform.tfstate, local state backups, and secret-bearing variable files out of Git. - Use a protected remote backend when the project is shared by multiple developers or managed through CI/CD.
The Skycloak provider reads its API key from SKYCLOAK_API_KEY. The key should have only the API scopes required by the Terraform resources in the project.
These practices make Keycloak Terraform automation easier to share without exposing the credentials that control the infrastructure.
The Terraform Workflow I Would Use for Every Change
Once the project structure is ready, the day-to-day workflow is straightforward. Terraform provides a small set of commands that help inspect and apply infrastructure changes in a controlled way.
I followed this sequence when provisioning my first Keycloak environment with Skycloak. The configuration defined one managed Keycloak cluster and one application realm, keeping the initial deployment small enough to verify each step.
1. Initialize the project
I started by running this command from the directory containing my Terraform configuration:
terraform init
Terraform downloaded the required Skycloak provider and prepared the working directory. The initialization completed successfully, so the project was ready to communicate with Skycloak.
You only need to initialize again when provider requirements or backend configuration change.
2. Validate the configuration
Next, I checked the configuration:
terraform validate
Terraform confirmed that the configuration was valid. This step checks the syntax and verifies that the declared resources and arguments match the provider schemas.
It catches configuration mistakes before Terraform attempts to create or update infrastructure.
3. Review the execution plan
Before provisioning anything, I ran:
terraform plan
Terraform compared the declared configuration with its current state and generated an execution plan showing the resources it intended to create.
My plan showed two new resources:
-
skycloak_cluster.test— the managed Keycloak cluster. -
skycloak_realm.test— the application realm linked to that cluster.
The final summary was:
Plan: 2 to add, 0 to change, 0 to destroy.
This was exactly what I expected. Terraform had identified the cluster and realm as new resources, with no existing infrastructure scheduled for modification or deletion.
4. Apply the configuration
With the plan confirmed, I ran:
terraform apply
Terraform displayed the proposed changes and requested approval. After I confirmed, it started provisioning the resources through the Skycloak provider.
The cluster was created first, followed by the application realm. The cluster took 41 seconds to provision, and the realm was created in another 4 seconds.
The deployment finished successfully:
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
5. Verify the deployed cluster and realm
After Terraform completed the deployment, I opened the Skycloak dashboard to inspect the resources.
The cluster appeared with the expected configuration:
-
Name:
terraform-test - Type: Keycloak
- Size: Small
- Version: 26.8.2
- Location: US
- Site: us-east
- Status: Available
The cluster reached the available status, confirming that it had finished provisioning.
I also checked the cluster's Realms section. The terraform-test realm was present with the display name Terraform Test Realm and its enabled status set to true.
This confirmed that Terraform had created the realm inside the cluster without requiring me to configure it manually through the dashboard.
The application realm was created automatically and linked to the Keycloak cluster provisioned through Terraform6. Confirm the Terraform state
The final step was checking whether Terraform had registered both resources in its state.
I ran:
terraform state list
The output showed:
skycloak_cluster.test
skycloak_realm.test
I also ran:
terraform show
This displayed the deployed infrastructure and its recorded configuration. The cluster had an available status, and the realm was enabled. Both resources were present in the state.
The result gave me a complete picture of the deployment: Terraform authenticated with Skycloak, created the cluster, provisioned its realm, and recorded both resources in its state.
For future changes, I would repeat the same sequence: initialize when needed, validate the configuration, review the execution plan, apply the intended changes, and verify the resulting infrastructure.
This workflow gives every infrastructure update a clear review point and keeps the deployed Keycloak environment aligned with the configuration in the repository.
Why Skycloak Makes This Workflow Useful
Terraform automation becomes more useful when the identity platform it manages is also straightforward to operate. Skycloak combines managed Keycloak infrastructure with an API and Terraform provider, so developers can provision supported platform resources through code while retaining access to the familiar Keycloak administration experience.
There are a few details worth knowing when choosing Skycloak for a SaaS application.
Unlimited users and full Keycloak access
Every Skycloak plan includes unlimited users and full Keycloak admin access. Your team also retains access to Keycloak's administration features, including realms, clients, users, roles, and authentication configuration.
Deployment regions and realm portability
Paid plans let you choose your deployment region, while the free trial runs in the US. This gives teams on paid plans flexibility when deciding where their identity infrastructure should run.
A 21-day free trial
Skycloak offers a 21-day free trial without requiring a credit card. The trial provides access to a managed Keycloak environment, giving developers an opportunity to explore the platform before choosing a paid plan.
You can explore Skycloak's managed Keycloak platform and its pricing and plans to see which setup fits your project.
Frequently Asked Questions
Can Terraform manage Keycloak infrastructure?Yes. Terraform can manage Keycloak infrastructure through providers. Skycloak's official provider provisions managed Keycloak clusters and supported platform resources, while the community Keycloak provider manages deeper configuration inside an existing Keycloak instance.
What can I automate with the Skycloak Terraform provider?
The Skycloak provider supports cluster provisioning, realms, users, roles, groups, applications, identity providers, and platform resources such as custom domains, cluster security, and branding. Deep realm configuration, including authentication flows and protocol mappers, is handled through the community Keycloak provider.
Do I need to know Terraform before automating Keycloak?
You need a basic understanding of Terraform configuration files, providers, resources, variables, and commands such as
terraform init, terraform plan, and terraform apply. A small project with one cluster and one realm is a practical way to learn the workflow.How do I authenticate the Skycloak Terraform provider?
Create a scoped API key in the Skycloak dashboard and expose it through the
SKYCLOAK_API_KEY environment variable. The provider reads the key automatically, so you do not need to place it directly in your Terraform configuration.Can I use the Skycloak and community Keycloak Terraform providers together?
Yes. They handle different responsibilities and can be used in the same Terraform project. The Skycloak provider manages the managed infrastructure and supported platform resources, while the community provider handles deeper Keycloak configuration such as client scopes, protocol mappers, and authentication flows.
Final Thoughts
Keycloak Terraform automation gives development teams a more consistent way to manage identity infrastructure. Cluster provisioning, realm creation, and supported security settings become part of a configuration that can be reviewed, versioned, and reused as an application grows.
I would start with a small deployment, understand how Terraform tracks the resources, and add more automation as the project needs it. That keeps the initial setup manageable while giving the team a foundation it can build on.
With Skycloak, developers get managed Keycloak infrastructure, full administrative access, and a Terraform provider for managing supported resources as code. That combination makes it easier to bring identity infrastructure into the same development workflow as the rest of the application.
| Thanks for reading! 🙏🏻 I hope you found this useful ✅ Please react and follow for more 😍 Made with 💙 by Hadil Ben Abdallah |
|
|---|
Hadil Ben AbdallahFollow
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.







