Manage new AWS resources with the Cloud Control provider
Terraform manages your resources through providers that connect to your cloud platforms' APIs. The Terraform Cloud Control provider supports new AWS services sooner than the traditional provider by using Cloud Control, a new AWS feature that creates standard API endpoints for new AWS services soon after their launch. These endpoints provide a standard set of actions, parameters, and error types that the Cloud Control provider uses to generate resources for AWS services automatically. You can use the Cloud Control provider alongside other providers, including the traditional AWS provider.
The Amazon Keyspaces service offers managed Apache Cassandra keyspaces and tables. The traditional AWS provider does not yet support Amazon Keyspaces, but the Cloud Control provider does. In this tutorial, you will provision a KMS key with the traditional AWS provider. Then, you will use the Cloud Control provider to provision a Cassandra keyspace and table, using the KMS key to encrypt your data at rest.
Prerequisites
- The Terraform CLI (1.0.7+).
- An AWS account.
- The AWS CLI (2.0+) installed, and configured for your AWS account.
- Docker Desktop installed and running.
- The Git CLI.
Note
Some of the infrastructure in this tutorial may not qualify for the AWS free tier. Destroy the infrastructure at the end of the tutorial to avoid unnecessary charges. We are not responsible for any charges that you incur.
Clone example configuration
Clone the example repository for this tutorial.
Change to the repository directory.
This configuration defines a KMS key managed by the traditional AWS provider. You will use this key to encrypt your Cassandra table.
Create KMS key
Initialize this configuration.
Apply the configuration to create your KMS key. Respond to the confirmation
prompt with a yes
.
Add AWS Cloud Control provider
The traditional AWS provider does not currently support Amazon Keyspaces, but the Cloud Control provider does. Add the Cloud Control provider to your configuration so you can use Terraform to manage a Cassandra keyspace and table.
First, update the terraform
block in main.tf
to add the Cloud Control and
random
providers. You will use the random provider to generate a random
keyspace name.
Next, add provider blocks for both the Cloud Control and random providers. Configure the Cloud Control provider to use the same region as the traditional AWS provider.
Reinitialize your configuration to install the new providers.
Now that you have installed the Cloud Control provider, you can create your Cassandra resources.
Add Cassandra keyspace and table
Add the following configuration to main.tf
to configure a Cassandra keyspace
with a random name, and a table to store your sample user data.
Resource types begin with the name of the provider, so the Cloud Control
provider manages awscc_cassandra_keyspace
and awscc_cassandra_table
resources.
Notice that your Cassandra table configuration uses the KMS key managed by the
traditional provider, by referencing aws_kms_key.terraform.key_id
for the
kms_key_identifier
argument. You can use resources from both the traditional
and Cloud Control providers in the same configuration.
Next, add an output for your Cassandra keyspace name to outputs.tf
.
Now, apply this configuration to create your keyspace and table. Respond to the
confirmation prompt with a yes
.
Load data into Cassandra table
Now that your table is ready, load the sample data from the data/
directory in
the example repository into your newly provisioned Cassandra table.
You will use the cqlsh
command line utility to load data into your table.
Amazon provides a Docker image pre-configured with cqlsh
and an authentication
plugin that allows you to access your Amazon Keyspaces Cassandra table with your
AWS credentials.
Build the Docker image now.
Note
It may take several minutes for Docker to build your container image.
By default, both the traditional AWS provider and the Cloud Control provider use
the same authentication credentials as the aws
command line utility. You must
pass your AWS credentials to the Docker container so that it has permission to
access your Cassandra table. In this tutorial, you will do so via environment
variables.
If you are not already using environment variables to authenticate with AWS, configure them now.
For example, if you use an access key to authenticate with AWS, first set the access key ID environment variable. Your access key ID will be different from the one shown here.
Next, set the secret access key environment variable. Your secret access key will be different from the one shown here.
You do not need to set the AWS_DEFAULT_REGION
environment variable.
Note
Depending on how you authenticate with AWS, you may need to set
other environment
variables
such as AWS_SESSION_TOKEN
and AWS_SESSION_EXPIRATION
. If so, set those
variables in your terminal session before you export them with the command
below.
Once you have set your AWS credentials as environment variables, export them to
a file named aws_auth_env
.
Now, launch the amazon/keyspaces-toolkit
Docker container to connect to your
Cassandra database.
In addition to configuring AWS authentication, the above command mounts the
data
directory from the example repository inside your Docker container. Then,
it launches the cqlsh-expansion
command to launch cqlsh and connect to your
keyspace using an authentication provider from AWS. After it connects to your
keyspace, cqlsh will print output summarizing its configuration followed by a
prompt including your keyspace name.
At the cqlsh
prompt, copy the data from the example CSV file into your
Cassandra table with the following command:
Cassandra will load your data into the users
table, and print output similar to the following.
Read data from table
Now, read the data from your Cassandra table with the following command:
Cassandra will print output similar to the following.
Exit the cqlsh
prompt with exit
.
After you exit cqlsh
, Docker will remove your container, so you do not need to
delete it manually.
Clean up your infrastructure
Remove the infrastructure you created during this tutorial. Respond to the
confirmation prompt with a yes
.
Next, remove the file containing your AWS credentials.
Finally, remove the Amazon Docker image you built during this tutorial.
Next steps
In this tutorial, you used the Cloud Control provider to manage Amazon Keyspaces resources that the traditional AWS provider does not yet support.
Review the following resources to learn more about the Cloud Control provider and Terraform providers in general.
- Read the Cloud Control Provider announcement blog post.
- Visit the Cloud Control provider documentation to learn more about authentication and supported resources.
- Learn how to create custom Terraform providers.