Developers and DevOps teams commonly utilize Infrastructure as Code (IaC) tools and scripts to manage their software and infrastructure deployments in a repeatable and consistent manner. With IaC tools, users can automate the life cycle of deployments including provisioning, configuration, deployment and destruction of resources. Hashicorp Terraform is a very popular IaC tool.
We’re pleased to announce the general availability of version 1.0 of the Couchbase Capella Terraform Provider, which will allow users to programmatically manage Couchbase Capella deployments. This version supersedes the v0.2 of the Capella provider which is now deprecated and will be unsupported shortly.
In this post, we give you a quick example walkthrough of the Capella Terraform provider.
Background
Earlier this year, we announced the launch of our new version of Capella Management API – a revamped version of the Capella public API that allows users to programmatically control Couchbase Capella resources through RESTful interface.
The Couchbase Capella Terraform provider v1.0 leverages the Capella Management API. As such, the set of Capella resources that can be managed via the provider is dependent on the underlying API. As the API evolves to support new endpoints. The Terraform provider will be extended correspondingly in order to support the management of the corresponding resources exposed via the endpoints.
Provider Walkthrough
In this example, we will demonstrate the use of the Capella Terraform provider to deploy a Capella project, a cluster and provision a bucket.
The GitHub repo has an extensive set of examples for managing each of the supported resources including users, projects, clusters, buckets, database credentials, allowed CIDRs, App Services, backup/restore and more.
Prerequisites
-
- Terraform >= 1.5.2
- Go >= 1.20
- A Capella paid account.
Note that the current version of the provider is not supported on Capella free trials. Support for trials will be available when the underlying Capella management API supports orchestration of trial deployments.
Authentication & Authorization
All operations by the Capella Terraform provider are authenticated and authorized via Capella Management API key. In a production environment, you will use something like HashiCorp Vault or a secrets manager offered by a Cloud Service Provider such as AWS Secrets Manager to manage your API keys. Reference to the secrets manager would be specified as input to the Terraform provider.
For purposes of this demonstration, we will set the credentials in a local environment variables file:
- Create a file named variables.tf and add the following variables definitions. We will use these variables within our config file.
1 2 3 4 5 6 7 |
variable "organization_id" { description = "Capella Organization ID" } variable "auth_token" { description = "Authentication API Key" } |
2. Create a file named terraform.template.tfvars and add the following lines. Here, we specify the values of key variables associated with the deployment
1 2 |
auth_token = "<replace-with-v4-api-key-secret>" organization_id = "<replace-with-the-oid-of-your-tenant>" |
-
- auth_token: You can create the API key via Capella UI or via the management API. Depending on the scope of the resources that is managed by the provider, you must create an Organization level or Project level API key with the right set of roles.
- organization_id: You can get this from organization management API or from the browser URL of Capella UI (look for “oid” parameter)
https://cloud.couchbase.com/databases?oid=0783f698-ac58–4018–84a3-xxxxxxxxxxxxx
Configuration for sample deployment
As mentioned earlier, the GitHub repo of the Provider has an extensive set of configuration templates. In this post, I am using a simple example to demonstrate the use of the provider to create a profile, deploy a cluster and a bucket within the cluster.
-
- Create a file named capella.tf and add the following configuration. The configuration does the following
- Creates project within specified Organization
- Creates a cluster within the project
- Creates bucket within the cluster
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
terraform { required_providers { couchbase-capella = { source = "registry.terraform.io/couchbasecloud/couchbase-capella" } } } # Configure the Couchbase Capella Provider using predefined variables provider "couchbase-capella" { authentication_token = var.auth_token } # Create example project resource resource "couchbase-capella_project" "new_project" { organization_id = var.organization_id name = "Terraform Demo Project" description = "A Capella Project that will host a Capella cluster" } # Stores the project name in an output variable. # Can be viewed using `terraform output project` command output "project" { value = couchbase-capella_project.new_project.name } # Create cluster resource resource "couchbase-capella_cluster" "new_cluster" { organization_id = var.organization_id project_id = couchbase-capella_project.new_project.id name = "Terraform Demo Cluster" description = "Test cluster created with Terraform" cloud_provider = { type = "aws" region = "us-east-1" cidr = "192.168.10.0/23" } couchbase_server = { version = "7.2" } service_groups = [ { node = { compute = { cpu = 4 ram = 16 } disk = { storage = 50 type = "io2" iops = 5000 } } num_of_nodes = 3 services = ["data", "index", "query"] } ] availability = { "type" : "multi" } support = { plan = "developer pro" timezone = "PT" } } # Stores the cluster details in an output variable. # Can be viewed using `terraform output cluster` command output "cluster" { value = couchbase-capella_cluster.new_cluster } # Create bucket in cluster resource "couchbase-capella_bucket" "new_bucket" { name = "terraform_bucket" organization_id = var.organization_id project_id = couchbase-capella_project.new_project.id cluster_id = couchbase-capella_cluster.new_cluster.id type = "couchbase" storage_backend = "couchstore" memory_allocation_in_mb = 100 bucket_conflict_resolution = "seqno" durability_level = "none" replicas = 1 flush = false time_to_live_in_seconds = 0 eviction_policy = "fullEviction" } # Stores the bucket name in an output variable. # Can be viewed using `terraform output bucket` command output "bucket" { value = couchbase-capella_bucket.new_bucket.name } |
Deploy and Manage Resources
Use standard Terraform commands to initialize and deploy the resources
1. Initialize the Terraform provider
Terraform must be initialized the very first time you use the provider:
1 |
terraform init |
2. Review the Terraform plan
Use the following command to review the resources that will be deployed:
1 |
terraform plan -var-file terraform.template.tfvars |
3. Execute the Terraform Plan
Deploy the Couchbase Capella resources using the following command:
1 |
terraform apply -var-file terraform.template.tfvars |
You should see output similar to the following. It will take a few minutes to deploy the resources:
1 2 3 4 5 6 7 8 9 10 11 12 |
capella_project.new_project: Creating... capella_project.new_project: Creation complete after 0s [id=c9151819-2f75-41dd-b944-7e33d12163ea] capella_cluster.new_cluster: Creating... capella_cluster.new_cluster: Still creating... [10s elapsed] capella_cluster.new_cluster: Still creating... [30s elapsed] ....... capella_cluster.new_cluster: Still creating... [2m50s elapsed] capella_cluster.new_cluster: Still creating... [3m0s elapsed] capella_cluster.new_cluster: Creation complete after 3m1s [id=29ebb043-xxxx-xxxx-xxxx-xxxxxxxxxxxx] capella_bucket.new_bucket: Creating... capella_bucket.new_bucket: Creation complete after 0s [id=dGVycmFmb3JtXXXXXXXXXX=] Apply complete! Resources: 3 added, 0 changed, 0 destroyed. |
4. Get the current state of the resources
1 |
terraform state list |
You should see output similar to the following which shows the three resources that are created:
1 2 3 |
couchbase-capella_bucket.new_bucket couchbase-capella_cluster.new_cluster couchbase-capella_project.new_project |
5. Get the detailed state of any deployed resource
1 |
terraform state show couchbase-capella_project.new_project |
You should see output similar to the following which shows the three resources that are created:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# capella_project.new_project: resource "couchbase-capella_project" "new_project" { audit = { created_at = "2023-11-19 22:59:59.695367442 +0000 UTC" created_by = "WFXyD6xRlAyCwKcpLGs6pKVScBGK299c" modified_at = "2023-11-19 22:59:59.695380869 +0000 UTC" modified_by = "WFXyD6xRlAyCwKcpLGs6pKVScBGK299c" version = 1 } description = "A Capella Project that will host a Capella cluster" etag = "Version: 1" id = "5ab4f4b5-756c-4f12-97ec-xxxxxyyyyyzzzz" name = "Terraform Demo Project" organization_id = "8b05f96d-45ba-zzzz-xxxx-fa55555555" } |
6. Destroy the resources
Execute the following command to destroy the resources:
1 |
terraform destroy -var-file terraform.template.tfvars |
You should see output similar to the following. It will take a few minutes to destroy the resources.
1 2 3 4 5 6 7 8 9 10 11 |
capella_bucket.new_bucket: Destroying... [id=dGVycmFmb3JtX2J1Y2tldA==] capella_bucket.new_bucket: Destruction complete after 1s capella_cluster.new_cluster: Destroying... [id=e64b8ba9-46a1-46f1-9fc3-7a412508ce4b] capella_cluster.new_cluster: Still destroying... [id=e64b8ba9-46a1-46f1-9fc3-7a412508ce4b, 10s elapsed] ..... capella_cluster.new_cluster: Still destroying... [id=e64b8ba9-46a1-46f1-9fc3-7a412508ce4b, 50s elapsed] .... [id=e64b8ba9-46a1-46f1-9fc3-7a412508ce4b, 3m20s elapsed] capella_cluster.new_cluster: Destruction complete after 3m26s capella_project.new_project: Destroying... [id=5ab4f4b5-756c-4f12-97ec-8e2427c7c1ab] capella_project.new_project: Destruction complete after 1s |
Resources and Next Steps
Version 1.0 of the Provider supersedes the v0.0.2 of the Capella provider which is deprecated and will be removed shortly. So, if you are using the old provider, you should plan to migrate to the new provider.
Here are direct links to a few helpful resources:
If you have questions or feedback, please leave a comment below. The Couchbase Forums or Couchbase discord channels are another good place to reach out with questions.