Skip to content

Commit 8a06c8e

Browse files
committed
Terraform: Implement suggestions by CodeRabbit
1 parent 7fee8e8 commit 8a06c8e

File tree

1 file changed

+94
-48
lines changed

1 file changed

+94
-48
lines changed

docs/integrate/terraform/tutorial.md

Lines changed: 94 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,49 @@
33

44
## Introduction
55

6-
CrateDB already has a wide variety of deployment options, ranging from
7-
{ref}`docker-compose` to {ref}`cloud setups <install-cloud>` on AWS and Azure.
6+
CrateDB offers multiple deployment options, from {ref}`docker-compose` to
7+
{ref}`cloud setups <install-cloud>` on AWS and Azure.
88

9-
To make cloud setups more manageable and predictable, we today add another
10-
option using [Terraform], an infrastructure-as-code tool.
11-
Terraform eliminates the need to create resources manually via the cloud console.
12-
Instead, it is based on a configuration language describing resources based
13-
on a given parametrization.
9+
To make cloud setups more manageable and predictable, this guide presents
10+
another option using [Terraform], an infrastructure-as-code tool.
11+
Terraform eliminates manual console steps to create resources.
12+
Instead, Terraform uses a configuration language that describes resources
13+
with parameters.
14+
15+
## Prerequisites
16+
17+
- Terraform CLI >= 1.5
18+
- An AWS account with credentials configured (AWS SSO/profile or env vars)
19+
and a default region or the provider block shown below
20+
- Existing VPC and subnet IDs in the chosen region (and their AZ mapping)
21+
- An EC2 key pair name to use for SSH
22+
- jq installed (used to pretty‑print JSON output)
1423

1524
## Example
1625

17-
The example below shows a 3 node cluster across several availability zones on AWS.
18-
Currently, AWS and Azure are supported as target platforms.
19-
The setup for Azure is very similar, please see the [README].
26+
This example deploys a 3node cluster across multiple Availability Zones on AWS.
27+
The module currently supports AWS and Azure.
28+
For Azure, see the [README].
2029

2130
## Configuration
2231

23-
First, we create a new Terraform configuration file named `main.tf`.
24-
That file references the [crate/crate-terraform](https://github.com/crate/crate-terraform) repository with the underlying logic of the deployment. Several variables need to be specified regarding the target environment.
25-
32+
First, create a Terraform configuration file named `main.tf`.
33+
Reference the [cratedb-terraform] module, which contains the deployment logic.
34+
Then, specify variables for the target environment.
2635

2736
```hcl
28-
module "cratedb-cluster" {
29-
source = "[email protected]:crate/crate-terraform.git//aws"
37+
terraform {
38+
required_version = ">= 1.5.0"
39+
}
40+
variable "region" {
41+
type = string
42+
default = "eu-central-1"
43+
}
44+
provider "aws" {
45+
region = var.region
46+
}
47+
module "cratedb_cluster" {
48+
source = "https://github.com/crate/cratedb-terraform.git//aws"
3049
3150
# Global configuration items for naming/tagging resources
3251
config = {
@@ -38,53 +57,54 @@ module "cratedb-cluster" {
3857
3958
# CrateDB-specific configuration
4059
crate = {
41-
# Java Heap size in GB available to CrateDB
60+
# Java heap size in GB available to CrateDB
4261
heap_size_gb = 2
4362
4463
cluster_name = "crate-cluster"
4564
46-
# The number of nodes the cluster will consist of
65+
# Number of nodes in the cluster
4766
cluster_size = 3
4867
49-
# Enables a self-signed SSL certificate
68+
# Enable a self-signed TLS certificate
5069
ssl_enable = true
5170
}
5271
53-
# The AWS region
54-
region = "eu-central-1"
72+
# AWS region
73+
region = var.region
5574
56-
# The VPC to deploy to
75+
# Target VPC
5776
vpc_id = "vpc-1234567"
5877
59-
# Applicable subnets of the VPC
78+
# Subnets in the VPC
6079
subnet_ids = ["subnet-123456", "subnet-123457"]
6180
62-
# The corresponding availability zones of above subnets
81+
# Availability Zones for the subnets above
82+
# Note: AZ suffixes are account-specific; ensure these match your subnets' AZs.
6383
availability_zones = ["eu-central-1b", "eu-central-1a"]
6484
65-
# The SSH key pair for EC2 instances
66-
ssh_keypair = "cratedb-cluster"
85+
# SSH key pair name for EC2 instances
86+
ssh_keypair = "cratedb_cluster"
6787
6888
# Enable SSH access to EC2 instances
89+
# Tip: restrict SSH to your IP/CIDRs if supported by the module.
6990
ssh_access = true
7091
}
7192
72-
# Connection information on the newly created cluster
93+
# Connection information for the newly created cluster
7394
output "cratedb" {
74-
value = module.cratedb-cluster
95+
value = module.cratedb_cluster
7596
sensitive = true
7697
}
7798
```
7899

79100
## Execution
80101

81-
Once all variables are adjusted, we initialize Terraform by installing needed plugins:
82-
102+
Once you adjust all variables, initialize Terraform by installing the needed plugins.
83103
```bash
84104
$ terraform init
85105
Initializing modules...
86-
Downloading git@github.com:crate/crate-terraform.git for cratedb-cluster...
87-
- cratedb-cluster in .terraform/modules/cratedb-cluster/aws
106+
Downloading git::https://github.com/crate/cratedb-terraform.git for cratedb_cluster...
107+
- cratedb_cluster in .terraform/modules/cratedb_cluster/aws
88108

89109
Initializing the backend...
90110

@@ -112,7 +132,8 @@ Terraform has been successfully initialized!
112132
[...]
113133
```
114134

115-
Before deploying anything to the cloud, we can optionally print the planned resource creation that Terraform derived from the configuration (the output below is shortened):
135+
Before deploying, optionally print the planned resource creation derived from the
136+
configuration (shortened output below):
116137
```bash
117138
$ terraform plan
118139

@@ -122,7 +143,7 @@ Terraform used the selected providers to generate the following execution plan.
122143

123144
Terraform will perform the following actions:
124145

125-
# module.cratedb-cluster.aws_instance.cratedb_node[0] will be created
146+
# module.cratedb_cluster.aws_instance.cratedb_node[0] will be created
126147
+ resource "aws_instance" "cratedb_node" {
127148
+ ami = "ami-0afc0414aefc9eaa7"
128149
+ arn = (known after apply)
@@ -140,7 +161,7 @@ Terraform will perform the following actions:
140161
+ instance_type = "t3.xlarge"
141162
+ ipv6_address_count = (known after apply)
142163
+ ipv6_addresses = (known after apply)
143-
+ key_name = "cratedb-cluster"
164+
+ key_name = "cratedb_cluster"
144165
+ monitoring = (known after apply)
145166
+ outpost_arn = (known after apply)
146167
+ password_data = (known after apply)
@@ -226,7 +247,7 @@ Terraform will perform the following actions:
226247
}
227248
}
228249

229-
# module.cratedb-cluster.aws_instance.cratedb_node[1] will be created
250+
# module.cratedb_cluster.aws_instance.cratedb_node[1] will be created
230251
+ resource "aws_instance" "cratedb_node" {
231252
[...]
232253
}
@@ -254,20 +275,33 @@ Outputs:
254275
cratedb = <sensitive>
255276
```
256277

257-
## Connecting to CrateDB
258-
The `cratedb` output element contains information on the newly created cluster, such as URL and credentials. Since it contains the admin password, the output is marked as sensitive and not immediately shown by Terraform. It can be made visible via the `terraform output` command:
278+
## Connect to CrateDB
279+
280+
The `cratedb` output element contains the connection details (URL and
281+
credentials). Because it’s marked sensitive, human‑readable output is
282+
redacted. Use the `terraform output` command to display it, adding
283+
`-json` to print the full value and pipe it to `jq` (ensure `jq` is
284+
installed).
259285
```bash
260-
$ terraform output cratedb
286+
$ terraform output -json cratedb | jq
287+
```
288+
```json
261289
{
262-
"cratedb_application_url" = "https://example-project-test-lb-572e07fbd6b72b88.elb.eu-central-1.amazonaws.com:4200"
263-
"cratedb_password" = "IgqVcBV28wNX8Js1"
264-
"cratedb_username" = "admin"
290+
"cratedb_application_url": "https://example-project-test-lb-572e07fbd6b72b88.elb.eu-central-1.amazonaws.com:4200",
291+
"cratedb_password": "IgqVcBV28wNX8Js1",
292+
"cratedb_username": "admin"
265293
}
266294
```
267295

268296
## Teardown
269297

270-
If you no longer need the deployed cluster, a simple `terraform destroy` from the same directory will suffice.
298+
:::{caution}
299+
This tutorial creates billable AWS resources. Destroy the stack when finished
300+
to avoid ongoing costs.
301+
:::
302+
303+
If you no longer need the deployed cluster, a simple `terraform destroy` from
304+
the same directory will suffice.
271305

272306
```bash
273307
$ terraform destroy
@@ -276,7 +310,7 @@ Terraform used the selected providers to generate the following execution plan.
276310

277311
Terraform will perform the following actions:
278312

279-
# module.cratedb-cluster.aws_instance.cratedb_node[0] will be destroyed
313+
# module.cratedb_cluster.aws_instance.cratedb_node[0] will be destroyed
280314
- resource "aws_instance" "cratedb_node" {
281315
[...]
282316
} -> null
@@ -291,16 +325,28 @@ Changes to Outputs:
291325
Destroy complete! Resources: 22 destroyed.
292326
```
293327

294-
For optimal security, a self-signed SSL certificate was automatically generated and deployed. After adding an exception in your browser to accept the certificate, the HTTP Basic Auth dialog of CrateDB’s Admin UI will show. Authenticate with the credentials retrieved from above.
328+
Terraform generates and deploys a self‑signed TLS certificate. After adding a
329+
browser exception, CrateDB’s Admin UI prompts for HTTP Basic Auth. Authenticate
330+
with the credentials shown above.
295331

296-
If the credentials are rejected, or no authentication prompt appears, please wait for a couple of minutes and try again. Provisioning the virtual machines might not be complete yet and can take several minutes.
332+
If authentication fails or no prompt appears:
333+
- Wait a few minutes; provisioning can take time.
334+
- Verify the load balancer is healthy and targets are in service.
335+
- Confirm the security group allows inbound HTTPS from your IP.
336+
- Ensure DNS/URL matches the ALB listener port (4200).
297337

298338
## Final Remarks
299339

300-
Please note that (as of now) the provided Terraform configuration is meant for development or testing purposes and doesn’t fulfill all requirements of a production-ready setup (i.e. regarding high availability, encryption, or backups). For production-ready setups, please consider [CrateDB Cloud](https://crate.io/products/cratedb-cloud/).
340+
This Terraform configuration targets development or testing and is not
341+
production‑ready (e.g., it does not configure high availability, disk encryption,
342+
automated backups, or managed TLS certificates).
343+
For production, consider using [CrateDB Cloud].
301344

302-
If you have any feedback or requests to extend the configuration, please feel free to [open an issue on GitHub](https://github.com/crate/crate-terraform/issues) or let us know in this thread!
345+
To request enhancements or share feedback, [open an issue on GitHub].
303346

304347

348+
[CrateDB Cloud]: https://crate.io/products/cratedb-cloud/
349+
[cratedb-terraform]: https://github.com/crate/cratedb-terraform
350+
[open an issue on GitHub]: https://github.com/crate/cratedb-terraform/issues
305351
[README]: https://github.com/crate/cratedb-terraform/blob/main/azure/README.md
306-
[Terraform]: https://www.terraform.io/
352+
[Terraform]: https://developer.hashicorp.com/terraform

0 commit comments

Comments
 (0)