|
| 1 | +name: terraform-aws-rds-db-proxy |
| 2 | + |
| 3 | +tags: |
| 4 | + - aws |
| 5 | + - terraform |
| 6 | + - terraform-modules |
| 7 | + - databases |
| 8 | + - rds |
| 9 | + - rds-database |
| 10 | + - proxy |
| 11 | + - proxy-pool |
| 12 | + - database-proxy |
| 13 | + - connection |
| 14 | + - connections |
| 15 | + - pool |
| 16 | + - connection-pool |
| 17 | + - aurora |
| 18 | + - mysql |
| 19 | + - postgres |
| 20 | + - cluster |
| 21 | + |
| 22 | +categories: |
| 23 | + - terraform-modules/databases |
| 24 | + |
| 25 | +license: APACHE2 |
| 26 | + |
| 27 | +github_repo: cloudposse/terraform-aws-rds-db-proxy |
| 28 | + |
| 29 | +badges: |
| 30 | + - name: Latest Release |
| 31 | + image: https://img.shields.io/github/release/cloudposse/terraform-aws-rds-db-proxy.svg |
| 32 | + url: https://github.com/cloudposse/terraform-aws-rds-db-proxy/releases/latest |
| 33 | + - name: Slack Community |
| 34 | + image: https://slack.cloudposse.com/badge.svg |
| 35 | + url: https://slack.cloudposse.com |
| 36 | + |
| 37 | +related: |
| 38 | + - name: terraform-aws-rds-cluster |
| 39 | + description: Terraform module to provision an RDS Aurora cluster for MySQL or Postgres. |
| 40 | + url: https://github.com/cloudposse/terraform-aws-rds-cluster |
| 41 | + - name: terraform-aws-rds |
| 42 | + description: Terraform module to provision AWS RDS instances. |
| 43 | + url: https://github.com/cloudposse/terraform-aws-rds |
| 44 | + - name: terraform-aws-rds-cloudwatch-sns-alarms |
| 45 | + description: Terraform module that configures important RDS alerts using CloudWatch and sends them to an SNS topic. |
| 46 | + url: https://github.com/cloudposse/terraform-aws-rds-cloudwatch-sns-alarms |
| 47 | + - name: terraform-aws-rds-replica |
| 48 | + description: Terraform module to provision AWS RDS replica instances. These are best suited for reporting purposes. |
| 49 | + url: https://github.com/cloudposse/terraform-aws-rds-replica |
| 50 | + - name: terraform-aws-backup |
| 51 | + description: Terraform module to provision AWS Backup, a fully managed backup service that makes it easy to centralize and automate |
| 52 | + the back up of data across AWS services such as Amazon EBS volumes, Amazon EC2 instances, Amazon RDS databases, |
| 53 | + Amazon DynamoDB tables, Amazon EFS file systems, and AWS Storage Gateway volumes. |
| 54 | + url: https://github.com/cloudposse/terraform-aws-backup |
| 55 | + |
| 56 | +description: |- |
| 57 | + Terraform module to provision an Amazon [RDS Proxy](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-proxy.html) for MySQL or Postgres. |
| 58 | +
|
| 59 | +usage: |2- |
| 60 | +
|
| 61 | + For a complete example, see [examples/complete](examples/complete). |
| 62 | +
|
| 63 | + For automated tests of the complete example using [bats](https://github.com/bats-core/bats-core) and [Terratest](https://github.com/gruntwork-io/terratest) |
| 64 | + (which tests and deploys the example on AWS), see [test](test). |
| 65 | +
|
| 66 | + ```hcl |
| 67 | + module "vpc" { |
| 68 | + source = "cloudposse/vpc/aws" |
| 69 | + version = "0.21.1" |
| 70 | +
|
| 71 | + cidr_block = "172.16.0.0/16" |
| 72 | +
|
| 73 | + context = module.this.context |
| 74 | + } |
| 75 | +
|
| 76 | + module "subnets" { |
| 77 | + source = "cloudposse/dynamic-subnets/aws" |
| 78 | + version = "0.38.0" |
| 79 | +
|
| 80 | + availability_zones = var.availability_zones |
| 81 | + vpc_id = module.vpc.vpc_id |
| 82 | + igw_id = module.vpc.igw_id |
| 83 | + cidr_block = module.vpc.vpc_cidr_block |
| 84 | + nat_gateway_enabled = false |
| 85 | + nat_instance_enabled = false |
| 86 | +
|
| 87 | + context = module.this.context |
| 88 | + } |
| 89 | +
|
| 90 | + resource "random_password" "admin_password" { |
| 91 | + count = var.database_password == "" || var.database_password == null ? 1 : 0 |
| 92 | + length = 33 |
| 93 | + special = false |
| 94 | + override_special = "!#$%^&*()<>-_" |
| 95 | + } |
| 96 | +
|
| 97 | + locals { |
| 98 | + database_password = var.database_password != "" && var.database_password != null ? var.database_password : join("", random_password.admin_password.*.result) |
| 99 | +
|
| 100 | + username_password = { |
| 101 | + username = var.database_user |
| 102 | + password = local.database_password |
| 103 | + } |
| 104 | +
|
| 105 | + auth = [ |
| 106 | + { |
| 107 | + auth_scheme = "SECRETS" |
| 108 | + description = "Access the database instance using username and password from AWS Secrets Manager" |
| 109 | + iam_auth = "DISABLED" |
| 110 | + secret_arn = aws_secretsmanager_secret.rds_username_and_password.arn |
| 111 | + } |
| 112 | + ] |
| 113 | + } |
| 114 | +
|
| 115 | + module "rds_instance" { |
| 116 | + source = "cloudposse/rds/aws" |
| 117 | + version = "0.34.0" |
| 118 | +
|
| 119 | + database_name = var.database_name |
| 120 | + database_user = var.database_user |
| 121 | + database_password = local.database_password |
| 122 | + database_port = var.database_port |
| 123 | + multi_az = var.multi_az |
| 124 | + storage_type = var.storage_type |
| 125 | + allocated_storage = var.allocated_storage |
| 126 | + storage_encrypted = var.storage_encrypted |
| 127 | + engine = var.engine |
| 128 | + engine_version = var.engine_version |
| 129 | + instance_class = var.instance_class |
| 130 | + db_parameter_group = var.db_parameter_group |
| 131 | + publicly_accessible = var.publicly_accessible |
| 132 | + vpc_id = module.vpc.vpc_id |
| 133 | + subnet_ids = module.subnets.private_subnet_ids |
| 134 | + security_group_ids = [module.vpc.vpc_default_security_group_id] |
| 135 | + apply_immediately = var.apply_immediately |
| 136 | +
|
| 137 | + context = module.this.context |
| 138 | + } |
| 139 | +
|
| 140 | + resource "aws_secretsmanager_secret" "rds_username_and_password" { |
| 141 | + name = module.this.id |
| 142 | + description = "RDS username and password" |
| 143 | + recovery_window_in_days = 0 |
| 144 | + tags = module.this.tags |
| 145 | + } |
| 146 | +
|
| 147 | + resource "aws_secretsmanager_secret_version" "rds_username_and_password" { |
| 148 | + secret_id = aws_secretsmanager_secret.rds_username_and_password.id |
| 149 | + secret_string = jsonencode(local.username_password) |
| 150 | + } |
| 151 | +
|
| 152 | + module "rds_proxy" { |
| 153 | + source = "cloudposse/rds-db-proxy/aws" |
| 154 | + version = "0.1.0" |
| 155 | +
|
| 156 | + db_instance_identifier = module.rds_instance.instance_id |
| 157 | + auth = local.auth |
| 158 | + vpc_security_group_ids = [module.vpc.vpc_default_security_group_id] |
| 159 | + vpc_subnet_ids = module.subnets.public_subnet_ids |
| 160 | +
|
| 161 | + debug_logging = var.debug_logging |
| 162 | + engine_family = var.engine_family |
| 163 | + idle_client_timeout = var.idle_client_timeout |
| 164 | + require_tls = var.require_tls |
| 165 | + connection_borrow_timeout = var.connection_borrow_timeout |
| 166 | + init_query = var.init_query |
| 167 | + max_connections_percent = var.max_connections_percent |
| 168 | + max_idle_connections_percent = var.max_idle_connections_percent |
| 169 | + session_pinning_filters = var.session_pinning_filters |
| 170 | + existing_iam_role_arn = var.existing_iam_role_arn |
| 171 | +
|
| 172 | + context = module.this.context |
| 173 | + } |
| 174 | +
|
| 175 | + ``` |
| 176 | +
|
| 177 | +examples: |- |
| 178 | + Review the [complete example](examples/complete) to see how to use this module. |
| 179 | +
|
| 180 | +include: |
| 181 | + - docs/targets.md |
| 182 | + - docs/terraform.md |
| 183 | + |
| 184 | +contributors: |
| 185 | + - name: Erik Osterman |
| 186 | + github: osterman |
| 187 | + - name: Andriy Knysh |
| 188 | + github: aknysh |
0 commit comments