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

Install Terraform, Astra CLI, and AWS CLI

  1. Install Terraform.

  2. 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
  3. 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
  4. 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.

  5. 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…​).

  6. Confirm that your Astra CLI configuration is correctly set up. This command returns the email address associated with your Astra account.

    astra config list
  7. Install the AWS CLI.

    curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
    sudo installer -pkg AWSCLIV2.pkg -target /
  8. 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

  1. Access the AWS Identity and Access Management console (IAM).

  2. In the left navigation pane, click Users, and then click Create user.

  3. Name your User and click Next.

  4. Click Attach policies directly.

  5. 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.

  6. Click Next, and then click Create user.

  7. From your IAM Dashboard, select your new User.

  8. Select Create access key to create a pair of keys wih access_key and secret_key.

  9. Under Access key best practices and alternatives, select Application running outside AWS.

  10. Click Download .csv file to save your keys locally.

Configure your AWS CLI profile

  1. 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
  2. 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.

  3. If you need to change profiles, use export AWS_PROFILE=<profile_name> to switch between profiles.

  4. 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

  1. Create a new directory for your Terraform project:

    mkdir terraform-demo && cd terraform-demo
  2. 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
  3. 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.

  4. 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.

  5. Confirm that Terraform is set up correctly.

    terraform plan
  6. 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.

  7. Apply the Terraform plan. Passing the aws_profile value is optional if you already used export earlier.

    terraform apply -var="astra_token=<your_astra_token>" -var="aws_profile=<your_aws_profile>"
  8. 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 like Apply complete! Resources: 36 added, 0 changed, 0 destroyed. when the process is complete.

  9. Confirm the creation of new resources with the Astra CLI, or check your Astra dashboard.

    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

  1. 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.

  2. If any Terraform files remain locally, delete them manually.

  3. If you want to re-deploy and are running into existing load balancers or CloudWatch logs, you can remove them with the AWS CLI.

  4. List your load balancers and record their ARN resource IDs.

    aws elbv2 describe-load-balancers --names datastax-assistants-alb datastax-langflow-alb
  5. Delete the load balancer by ARN resource ID.

    aws elbv2 delete-load-balancer --load-balancer-arn <arn-of-load-balancer>
  6. 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.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com