Integrate HashiCorp Terraform with Astra DB Serverless
Terraform is an infrastructure-as-code tool that lets you safely and predictably provision and manage infrastructure in any cloud. Developed in partnership with HashiCorp, the DataStax AI Stack is a Terraform module that simplifies how you provision, install, and configure Astra DB, Langflow, the Astra Assistants API, and more.
Prerequisites
-
An active Astra account
-
An application token with the Organization Administrator role
-
If you’re running macOS, this guide assumes you have Homebrew installed.
Install Terraform, Astra CLI, and AWS CLI
-
Install Terraform.
-
In a terminal, run the following command to confirm that Terraform is installed:
terraform -v
The command returns your Terraform version and local OS.
Result
Terraform v1.9.2 on darwin_arm64
-
Install the Astra CLI. Astra CLI is a command-line tool that lets you interact with your Astra DB database.
-
macOS
-
Linux
To install the Astra CLI on macOS:
brew update && brew install datastax/astra-cli/astra-cli
To install the Astra CLI on Linux:
curl -Ls "https://dtsx.io/get-astra-cli" | bash
-
-
Run the following command to confirm that the Astra CLI is installed:
astra --version
The command returns your Astra CLI version.
Result
0.5
On macOS, the operating system might display the following system prompt:
"astra" can’t be opened because Apple cannot check it for malicious software
. If the option is presented, click Open to allow the Astra CLI to run. If the Open button is not present, go to System Settings > Privacy & Security, scroll down, and click Open Anyway. For more information, see the Apple support documentation. -
Use your application token to create an Astra CLI configuration that lets you interact with your Astra DB Serverless database:
astra setup --token TOKEN
Replace
TOKEN
with your application token (AstraCS:WSnyFUhRxsrg…
). -
Confirm that your Astra CLI configuration is correctly set up. This command returns the email address associated with your Astra account.
astra config list
-
Install the AWS CLI.
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg" sudo installer -pkg AWSCLIV2.pkg -target /
-
Confirm installation of the AWS CLI.
aws help
You have successfully installed Terraform, the Astra CLI, and the AWS CLI.
Add AWS credentials to AWS CLI
-
Access the AWS Identity and Access Management console (IAM).
-
In the left navigation pane, click Users, and then click Create user.
-
Name your User and click Next.
-
Click Attach policies directly.
-
Search for and attach the following policies your User:
-
AmazonEC2FullAccess
-
AmazonECS_FullAccess
-
AmazonRoute53FullAccess
-
AutoScalingFullAccess
-
CloudWatchLogsFullAccess
-
IAMFullAccess
-
AmazonRDSFullAccess
These permissions are excessive for production use. For production deployments, you should create a custom policy with the minimum required permissions.
-
-
Click Next, and then click Create user.
-
From your IAM Dashboard, select your new User.
-
Select Create access key to create a pair of keys wih
access_key
andsecret_key
. -
Under Access key best practices and alternatives, select Application running outside AWS.
-
Click Download .csv file to save your keys locally.
Configure your AWS CLI profile
-
Add the keys you downloaded to the AWS CLI configuration file. If you already have a default profile, you can create a new profile for this project by adding it to the
~/.aws/credentials
file.touch ~/.aws/credentials vi ~/.aws/credentials
-
Add the following lines to the
~/.aws/credentials
file.[terraform] aws_access_key_id = YOUR_ACCESS_KEY aws_secret_access_key = YOUR_SECRET_KEY region = us-east-2
Save and close the file.
-
If you need to change profiles, use
export AWS_PROFILE=<profile_name>
to switch between profiles. -
Confirm that your AWS CLI secret is configured correctly.
aws configure list --profile terraform Name Value Type Location ---- ----- ---- -------- profile terraform manual --profile access_key ****************FZ6H shared-credentials-file secret_key ****************u1hZ shared-credentials-file
You have successfully configured your AWS CLI profile to interact with AWS services with the appropriate permissions.
Install Terraform module for Astra DB
-
Create a new directory for your Terraform project:
mkdir terraform-demo && cd terraform-demo
-
Clone the Terraform AWS Stack repository. This is a submodule of the DataStax AI Stack repository that contains example Terraform modules for deploying the DataStax AI Stack on AWS.
git clone https://github.com/datastax/terraform-aws-ai-stack
-
Change to the Terraform DataStax AWS Stack directory containing an AWS example that doesn’t require provisioning a custom domain.
cd terraform-aws-ai-stack/examples/aws-no-custom-domain
This directory contains
.tf
files that define the Terraform modules for deploying the DataStax AI Stack on AWS. If you want to try other cloud providers or evaluate different module configurations, you can find additional submodules for GCP and Microsoft Azure in the repo. For more information, see the Terraform module README. -
Initialize Terraform.
terraform init
Terraform initializes the modules and downloads the required providers from the Terraform registry. You will see
Terraform has been successfully initialized!
when the process is complete. -
Confirm that Terraform is set up correctly.
terraform plan
-
When prompted, enter values for the variables in the
.tf
files, including your application token.var.astra_token Enter a value: AstraCS...
When the process is complete, you will see a message like
Plan: 65 to add, 0 to change, 0 to destroy.
This is describing the resources that Terraform will modify if you apply the plan. -
Apply the Terraform plan. Passing the
aws_profile
value is optional if you already usedexport
earlier.terraform apply -var="astra_token=<your_astra_token>" -var="aws_profile=<your_aws_profile>"
-
Terraform will prompt you to confirm the plan. Enter
yes
to apply the plan. Your Terraform plan is applied, and you will see a message likeApply complete! Resources: 36 added, 0 changed, 0 destroyed.
when the process is complete. -
Confirm the creation of new resources with the Astra CLI or check the Astra Portal:
astra db list
The results will show the new databases you created with Terraform.
+---------------------+--------------------------------------+-----------+-------+---+-----------+ | Name | id | Regions | Cloud | V | Status | +---------------------+--------------------------------------+-----------+-------+---+-----------+ | us-east-2 | d16f4b43-b3f5-4825-9868-20204e7007d1 | us-east-2 | aws | ■ | ACTIVE | | my_db | 6005fdc3-80c3-43c0-8adc-688539750001 | us-east-2 | aws | ■ | ACTIVE | | assistant_api_db | d403e9f7-46d2-46a0-912d-0313892cbcd8 | us-east-2 | aws | ■ | ACTIVE | | langflow_db | 3b3b3b3b-3b3b-3b3b-3b3b-3b3b3b3b3b3b | us-east-2 | aws | ■ | ACTIVE | +---------------------+--------------------------------------+-----------+-------+---+-----------+
You have successfully deployed the DataStax AI Stack on AWS using Terraform.
Cleanup provisioned resources
-
To remove the resources you created with Terraform, run the following command:
terraform destroy -var="astra_token=<your_astra_token>"
When the process is complete, you will see
Destroy complete! Resources: 42 destroyed.
-
If any Terraform files remain locally, delete them manually.
-
If you want to re-deploy and are running into existing load balancers or CloudWatch logs, you can remove them with the AWS CLI.
-
List your load balancers and record their ARN resource IDs.
aws elbv2 describe-load-balancers --names datastax-assistants-alb datastax-langflow-alb
-
Delete the load balancer by ARN resource ID.
aws elbv2 delete-load-balancer --load-balancer-arn <arn-of-load-balancer>
-
Delete CloudWatch log groups by name.
aws logs delete-log-group --log-group-name /aws/ecs/datastax-ecs-cluster aws logs delete-log-group --log-group-name /ecs/langflow aws logs delete-log-group --log-group-name /ecs/assistants
You have successfully removed the resources you created with Terraform.
Terraform configuration options
For more configuration options for your Terraform deployment, see the Terraform module README.