https://github.com/materializeinc/terraform-aws-rds-privatelink
A Terraform module for configuring RDS with AWS PrivateLink.
https://github.com/materializeinc/terraform-aws-rds-privatelink
privatelink rds terraform terraform-module
Last synced: 10 months ago
JSON representation
A Terraform module for configuring RDS with AWS PrivateLink.
- Host: GitHub
- URL: https://github.com/materializeinc/terraform-aws-rds-privatelink
- Owner: MaterializeInc
- License: apache-2.0
- Created: 2023-05-12T14:53:42.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-16T18:20:33.000Z (over 1 year ago)
- Last Synced: 2024-11-08T01:49:45.792Z (over 1 year ago)
- Topics: privatelink, rds, terraform, terraform-module
- Language: HCL
- Homepage: https://registry.terraform.io/modules/MaterializeInc/rds-privatelink
- Size: 29.3 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Materialize + PrivateLink + RDS
> [!WARNING]
> This is provided on a best-effort basis and Materialize cannot offer support for this module
This repository contains a Terraform module that configures a PrivateLink endpoint for existing Amazon RDS PostgreSQL or MySQL databases to connect to Materialize.
The module creates the following resources:
- Target group for each RDS instance
- Network Load Balancer for the RDS instances
- TCP listener for the NLB to forward traffic to the target groups
- A VPC endpoint service for your RDS instances
- Lambda Function to check and update the IP addresses of the RDS instances in the NLB target groups
- IAM Role and Policy to give the Lambda function necessary permissions
- Event Source Mapping, Event Rule, and Target: Triggers the Lambda function every _n_ minutes
- Lambda Permission: Allows the event to invoke the Lambda function
## Important Remarks
> [!NOTE]
> The RDS instances need to be private. If your RDS instances are public, there is no need to use PrivateLink.
> [!NOTE]
> When using Aurora, the RDS instance needs to be a **writer** instance as the reader instances will not work.
- The RDS instances must be in the same VPC as the PrivateLink endpoint.
- Review this module with your Cloud Security team to ensure that it meets your security requirements.
- Finally, after the Terraform module has been applied, you will need to make sure that the Target Groups health checks are passing. As the NLB does not have security groups, you will need to make sure that the NLB is able to reach the RDS instances by allowing the subnet CIDR blocks in the security groups of the RDS instances.
- Cross-region connections:
To connect to an AWS PrivateLink endpoint service in a different region to the one where your Materialize environment is deployed, you need to set the `mz_supported_regions` variable to include the region where the Materialize instance is deployed.
For same-region connections, you can leave the `mz_supported_regions` variable empty.
To override the default AWS provider variables, you can export the following environment variables:
```bash
export AWS_PROFILE= # eg. default
export AWS_CONFIG_FILE= # eg. ["~/.aws/config"]
export AWS_REGION= # eg. us-east-1
```
## Usage
### Variables
Start by copying the `terraform.tfvars.example` file to `terraform.tfvars` and filling in the variables:
```
cp terraform.tfvars.example terraform.tfvars
```
| Name | Description | Type | Example | Required |
|-----------------------------|-------------|:----:|:-----:|:-----:|
| `mz_rds_instance_names` | The name and listener port of the RDS instances | list | `{ name = "instance1", listener_port = 5001 }` | yes |
| `mz_rds_vpc_id` | The VPC ID of the RDS instance | string | `'vpc-1234567890abcdef0'` | yes |
| `mz_acceptance_required` | Whether or not to require manual acceptance of new connections | bool | `true` | no |
| `schedule_expression` | [The scheduling expression](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule#schedule_expression). For example, `cron(0 20 * * ? *)` | string | `'rate(5 minutes)'` | no |
| `cross_zone_load_balancing` | Enables cross zone load balancing for the NLB | bool | `false` | no |
| `mz_supported_regions` | Only required for cross-region connections. The regions where the Materialize instance is deployed | list | `["us-east-1"]` | no |
### Apply the Terraform Module
```
terraform apply
```
### Output
After the Terraform module has been applied, you will see the following output.
You can follow the instructions in the output to configure the PrivateLink endpoint and the database connections in Materialize.
First, you will need to create the PrivateLink endpoint in Materialize:
```sql
mz_rds_private_link_endpoint_sql = < Change the `privatelink_svc` to the name of the connection you want to use.
- Get the allowed principals for the VPC endpoint service
```sql
SELECT principal
FROM mz_aws_privatelink_connections plc
JOIN mz_connections c ON plc.id = c.id
WHERE c.name = 'privatelink_svc';
```
- Add the allowed principals to the Endpoint Service configuration in the AWS console
- Finally, run the last SQL statement from the output of the `terraform apply` command to create the database connection which will use the PrivateLink endpoint. If you have multiple RDS instances, you will see multiple SQL statements:
For PostgreSQL instances:
```sql
-- Create the connection to the PostgreSQL RDS instance
CREATE CONNECTION pg_connection TO POSTGRES (
HOST 'instance.foo000.us-west-1.rds.amazonaws.com',
PORT 5432,
DATABASE postgres,
USER postgres,
PASSWORD SECRET pgpass,
AWS PRIVATELINK privatelink_svc
);
```
For MySQL instances:
```sql
-- Create the connection to the MySQL RDS instance
CREATE CONNECTION mysql_connection TO MYSQL (
HOST 'mysql-instance.foo000.us-west-1.rds.amazonaws.com',
PORT 3306,
USER your_mysql_user,
PASSWORD SECRET mysql_dbpass,
AWS PRIVATELINK privatelink_svc
);
```
After that go to your AWS console and check that the VPC endpoint service has a pending connection request from the Materialize instance which you can approve.
After the connection request has been approved, you can create a database source in Materialize using the `pg_connection` or `mysql_connection` connection.
## Materialize Documentation
You can also follow the [Materialize documentation](https://materialize.com/docs/ops/network-security/privatelink/) for more information.