Deploy RDS Instance with Terraform¶
In this guide, we're going to use a Terraform definition and instance to deploy an instance of the AWS Relational Database Service (RDS) using Threeport.
You'll need an active AWS account to follow this guide.
Note: At this time, you can only use Terraform to deploy resources on AWS with Threeport.
Prerequisites¶
You'll need a Threeport control plane for this guide. Follow the Install Threeport Locally guide to set that up a local control plane or the Install Threeport on AWS guide for a remote control plane. Either will work.
Work Space¶
First, create a temporary work space on your machine.
1 2 |
|
AWS Account¶
If you installed Threeport on AWS, you'll already have an AWS account available to use (the same one used to deploy EKS for the Threeport control plane). If you'd like to use a different AWS account for this guide - or if you installed Threeport locally, you'll need to register an AWS account with Threeport.
Download a config to register AWS with Threeport.
1 |
|
Open this file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Edit the file to make the following changes:
- On line 2, update the
AccountID
with the value for your account. - If using option 1, enter the
DefaultRegion
you'd like to use on line 7 and add the keys on lines 8 and 9. If you're unsure how to create access keys for AWS, see the AWS guide for managing access keys. - If using option 2, you'll need to have your local config and credentials set up. If you have set up the AWS CLI in the past, you'll likely have this ready. If not, see the AWS guide to set up the AWS CLI. With this set up, enter the file paths for the config and credentials along with the profile you'd prefer to use.
Now, register the AWS account with Threeport.
1 |
|
Create Terraform Definition¶
The Terraform definition includes the Terraform configs to deploy some AWS resources.
Download a sample Threeport config.
1 |
|
The sample config for a Terraform definition looks as follows:
1 2 3 |
|
It indicates that it will look for Terraform configs in a directory config
.
Let's create that directory and download sample Terraform configs.
1 2 3 4 |
|
Now we can create the Terraform definition:
1 |
|
The Terraform configs for an RDS instance are now stored in Threeport. Next, we can create an instance from that definition.
Create Terraform Instance¶
This step will actually deploy the RDS instance on AWS.
Download a sample config.
1 |
|
The config looks as follows:
1 2 3 4 5 6 7 |
|
This config specifies:
- An arbitrary name for the RDS instance
- Terraform variables to use for this deployment
- The name of the definition we just created
- The AWS account we registered earlier
Download Terraform a sample variables file:
1 |
|
Open the config/terraform.tfvars
file.
1 2 3 4 5 6 7 |
|
There are several values we'll have to set for your environment:
- Set the
region
to your desired AWS region. - Log into the AWS console and go to the VPC dashboard. If you have a Threeport control plane installed in AWS, select the VPC where that is installed. Otherwise, use your default VPC. Enter the VPC ID on line 2 in the tfvars file.
- Go the subnets dashboard in the AWS console. Select the subnets used for Threeport, or the default subnets. Enter the subnet IDs on line 3.
- For the security group, use your default security group ID on line 6. We won't be connecting an app in this guide so it is not important.
- Change the password to something unique on line 7.
Now we can create the Terraform instance.
1 |
|
If you go to the AWS RDS console, you'll shortly see the RDS instance being created. Terraform takes a few minutes to initialize and deploy resources, so it won't be immediate. Give it a little time.
You can view your Terraforms with this command:
1 |
|
Once the RDS instance is up, you should see output similar to this:
1 2 |
|
Get Terraform Outputs¶
In order to connect to the database, there are several outputs from Terraform that you'll need to retrieve. These are stored securely in the Threeport database.
Once the RDS instance is up, get those outputs as follows:
1 |
|
You'll notice the sensitive encrypted values are redacted. You can view those values by requesting specific fields.
1 |
|
You'll see the Terraform outputs in JSON format similar to this:
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 |
|
You can now use these values as inputs to a workload to use this database.
Delete Terraform Instance¶
The following command will delete the database instance:
1 |
|
The command will not return a response until the AWS resources have been removed. For an RDS instance this will take a few minutes.
Once the resources are removed and the prompt returns, you can delete the Terraform definition as well.
1 |
|
Clean Up¶
Before we finish, let's clean up the files we downloaded to your file system.
1 2 |
|
Summary¶
In this guide you used a Terraform definition and instance to deploy and delete an AWS RDS instance. You also learned how to get the outputs from Terraform to provide DB connection info to a client application.
Therein lies one of the challenges with Terraform: programmatically providing outputs from Terraform as inputs to another operation to deploy a workload that connects to that DB. In the near future, we will provide guides on how to use the Threeport SDK to wire these concerns together to remove human copy-paste operations from the process.