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.
Histórico
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.
O Repositório do GitHub 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.
Pré-requisitos
-
- Terraform >= 1.5.2
- Go >= 1.20
- A Capella paid account.
Observação 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:
- Crie um arquivo chamado variables.tf and add the following variables definitions. We will use these variables within our config file.
1 2 3 4 5 6 7 |
variável "organization_id" { descrição = "Capella Organization ID" } variável "auth_token" { descrição = "Authentication API Key" } |
2. Crie um arquivo chamado 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 ou por meio do 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 Repositório do GitHub 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.
-
- Crie um arquivo chamado 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 |
terraformação { required_providers { couchbase-capela = { fonte = "registry.terraform.io/couchbasecloud/couchbase-capella" } } } # Configurar o Couchbase Capela Provedor usando predefined variáveis provedor "couchbase-capella" { authentication_token = var.autenticação_token } # Criar exemplo projeto recurso recurso "couchbase-capella_project" "new_project" { organization_id = var.organização_id nome = "Terraform Demo Project" descrição = "A Capella Project that will host a Capella cluster" } # Lojas o projeto nome em e saída variável. # Pode ser viewed usando `terraform output project` comando saída "projeto" { valor = couchbase-capella_project.new_project.nome } # Criar agrupamento recurso recurso "couchbase-capella_cluster" "new_cluster" { organization_id = var.organização_id projeto_id = couchbase-capella_project.new_project.id nome = "Terraform Demo Cluster" descrição = "Test cluster created with Terraform" cloud_provider = { tipo = "aws" região = "us-east-1" cidr = "192.168.10.0/23" } servidor couchbase = { versão = "7.2" } service_groups = [ { nó = { computar = { CPU = 4 ram = 16 } disco = { armazenamento = 50 tipo = "io2" iops = 5000 } } num_of_nodes = 3 serviços = ["dados", "índice", "query" (consulta)] } ] disponibilidade = { "tipo" : "multi" } suporte = { plano = "desenvolvedor profissional" fuso horário = "PT" } } # Lojas o agrupamento detalhes em e saída variável. # Pode ser viewed usando `terraform output cluster` comando saída "cluster" { valor = couchbase-capella_cluster.novo_agrupamento } # Criar balde em agrupamento recurso "couchbase-capella_bucket" "new_bucket" { nome = "terraform_bucket" organização_id = var.organização_id projeto_id = couchbase-capella_project.new_project.id agrupamento_id = couchbase-capella_cluster.novo_cluster.id tipo = "couchbase" armazenamento_backend = "couchstore" memory_allocation_in_mb = 100 bucket_conflict_resolution = "seqno" durabilidade_nível = "nenhum" réplicas = 1 descarga = falso time_to_live_in_segundos = 0 despejo_política = "fullEviction" } # Lojas o balde nome em e saída variável. # Pode ser viewed usando `terraform output bucket` comando saída "bucket" (balde) { valor = couchbase-capella_bucket.new_bucket.nome } |
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 |
terraformação inicial |
2. Review the Terraform plan
Use the following command to review the resources that will be deployed:
1 |
terraformação plano -var-arquivo terraformação.modelo.tfvars |
3. Execute the Terraform Plan
Deploy the Couchbase Capella resources using the following command:
1 |
terraformação aplicar -var-arquivo terraformação.modelo.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: Criação de... capella_project.new_project: Creation completo após 0s [id=c9151819-2f75-41dd-b944-7e33d12163ea] capella_cluster.novo_cluster: Criação de... capella_cluster.novo_cluster: Still criando... [10s elapsed] capella_cluster.novo_cluster: Still criando... [30s elapsed] ....... capella_cluster.novo_cluster: Still criando... [2m50s elapsed] capella_cluster.novo_cluster: Still criando... [3m0s elapsed] capella_cluster.novo_cluster: Creation completo após 3m1s [id=29ebb043-xxxx-xxxx-xxxx-xxxxxxxxxxxx] capella_bucket.new_bucket: Criação de... capella_bucket.new_bucket: Creation completo após 0s [id=dGVycmFmb3JtXXXXXXXXXX=] Aplicar completo! Recursos: 3 adicionado, 0 alterado, 0 destroyed. |
4. Get the current state of the resources
1 |
terraformação estado lista |
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.novo_cluster couchbase-capella_project.new_project |
5. Get the detailed state of any deployed resource
1 |
terraformação estado 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: recurso "couchbase-capella_project" "new_project" { auditoria = { criado_em = "2023-11-19 22:59:59.695367442 +0000 UTC" criado_por = "WFXyD6xRlAyCwKcpLGs6pKVScBGK299c" modified_at = "2023-11-19 22:59:59.695380869 +0000 UTC" modified_by = "WFXyD6xRlAyCwKcpLGs6pKVScBGK299c" versão = 1 } descrição = "A Capella Project that will host a Capella cluster" etag = "Version: 1" id = "5ab4f4b5-756c-4f12-97ec-xxxxxyyyyyzzzz" nome = "Terraform Demo Project" organization_id = "8b05f96d-45ba-zzzz-xxxx-fa55555555" } |
6. Destroy the resources
Execute the following command to destroy the resources:
1 |
terraformação destruir -var-arquivo terraformação.modelo.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 completo após 1s capella_cluster.novo_cluster: Destroying... [id=e64b8ba9-46a1-46f1-9fc3-7a412508ce4b] capella_cluster.novo_cluster: Still destroying... [id=e64b8ba9-46a1-46f1-9fc3-7a412508ce4b, 10s elapsed] ..... capella_cluster.novo_cluster: Still destroying... [id=e64b8ba9-46a1-46f1-9fc3-7a412508ce4b, 50s elapsed] .... [id=e64b8ba9-46a1-46f1-9fc3-7a412508ce4b, 3m20s elapsed] capella_cluster.novo_cluster: Destruction completo após 3m26s capella_project.new_project: Destroying... [id=5ab4f4b5-756c-4f12-97ec-8e2427c7c1ab] capella_project.new_project: Destruction completo após 1s |
Recursos e próximas etapas
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:
Se você tiver dúvidas ou comentários, deixe um comentário abaixo. Os Fóruns do Couchbase ou Discórdia do Couchbase Os canais são outro bom lugar para entrar em contato com perguntas.