Terraform module which creates standalone AWS RDS resources
The module will create:
- DB instance (MySQL, Postgres, SQL Server, Oracle)
- DB Subnet Group
- DB Security Group
The project is organized into the following directories and files:
-
/DevOps-Automations/terraform/aws/modules/rds: This directory contains the Terraform modules for setting up the RDS instance
rds.tf
: Defines the rds instance and its dependent resources like subnet group and security group.variables.tf
: Declares input variables used throughout the module.outputs.tf
: Defines the output values of the module.
-
/DevOps-Automations/terraform/aws/examples/rds: This directory contains example configurations that use the rds module defined in the
modules/rds
directory.provider.tf
: Configures the Terraform provider for AWS and specifies the default AWS region.main.tf
: Calls both the RDS and VPC module and passes input variables to create an RDS instance and its associated infrastructure within the VPC.
-
/DevOps-Automations/terraform/aws/modules/vpc: This directory contains the Terraform module for configuring the VPC.
Before using this Terraform configuration, ensure you have the following prerequisites:
- Terraform (v1.5.0 or later) installed.
- AWS CLI configured with appropriate access credentials.
Name | Type |
---|---|
aws_db_instance.default | resource |
aws_db_subnet_group.default | resource |
aws_security_group.default | resource |
aws_security_group_rule.egress | resource |
aws_security_group_rule.ingress_cidr_blocks | resource |
aws_security_group_rule.ingress_security_groups | resource |
random_password.db-password | resource |
aws_kms_key.db_ssm_encrypt | resource |
aws_ssm_parameter.db-password | resource |
Name | Description | Type |
---|---|---|
database_port | Database port Used in the DB Security Group to allow access to the DB instance from the provided security_group_ids |
number |
database_user | Username for the primary DB user, Required unless a snapshot_identifier is provided. | string |
engine | Database engine type. | string |
engine_version | Database engine version, depends on engine type | string |
instance_class | Class of RDS instance | string |
vpc_id | VPC ID the DB instance will be created in | string |
allocated_storage | The allocated storage in GBs, Required unless a snapshot_identifier is provided. | number |
subnet_ids | List of subnet IDs for the DB. DB instance will be created in the VPC associated with the DB subnet group provisioned using the subnet IDs. | list(string) |
db-identifier | Identifier for the RDS DB instance. | |
database_name | Name of the database to be created on the RDS instance. | string |
snapshot_identifier | whether or not to create this database from a snapshot. This correlates to the snapshot ID you'd find in the RDS console. | string |
To use this Terraform project, follow these steps:
- Clone this repository:
git clone <repository_url>
- Change to the project directory:
cd terraform/aws/examples/rds
- Initialize Terraform:
terraform init
- Review the plan to ensure everything looks correct:
terraform plan
- Apply the Terraform configuration to create the Lambda function and associated resources:
terraform apply
To establish a connection with the database, follow these steps:
- Create an EC2 instance in the same VPC where the RDS is deployed.
- Install MySQL or any appropriate database client on the EC2 instance.
- Retrieve the database password securely from AWS Systems Manager Parameter Store
- Use the following connection command:
mysql -h <RDS_endpoint> -u <username> -p
**Cleanup**
To destroy the created resources and clean up, run:
```bash
terraform destroy