Skip to main content

rds

This component is responsible for provisioning an RDS instance. It seeds relevant database information (hostnames, username, password, etc.) into AWS SSM Parameter Store.

Security Groups Guidance:

By default this component creates a client security group and adds that security group id to the default attached security group. Ideally other AWS resources that require RDS access can be granted this client security group. Additionally you can grant access via specific CIDR blocks or security group ids.

Usage

Stack Level: Regional

Here's an example snippet for how to use this component.

PostgreSQL

components:
terraform:
rds/defaults:
metadata:
type: abstract
vars:
enabled: true
use_fullname: false
name: my-postgres-db
instance_class: db.t3.micro
database_name: my-postgres-db
# database_user: admin # enable to specify something specific
engine: postgres
engine_version: "15.2"
database_port: 5432
db_parameter_group: "postgres15"
allocated_storage: 10 #GBs
ssm_enabled: true
client_security_group_enabled: true
## The following settings allow the database to be accessed from anywhere
# publicly_accessible: true
# use_private_subnets: false
# allowed_cidr_blocks:
# - 0.0.0.0/0

Microsoft SQL

components:
terraform:
rds:
vars:
enabled: true
name: mssql
# SQL Server 2017 Enterprise
engine: sqlserver-ee
engine_version: "14.00.3356.20"
db_parameter_group: "sqlserver-ee-14.0"
license_model: license-included
# Required for MSSQL
database_name: null
database_port: 1433
database_user: mssql
instance_class: db.t3.xlarge
# There are issues with enabling this
multi_az: false
allocated_storage: 20
publicly_accessible: false
ssm_enabled: true
# This does not seem to work correctly
deletion_protection: false

Provisioning from a snapshot

The snapshot identifier variable can be added to provision an instance from a snapshot HOWEVER- Keep in mind these instances are provisioned from a unique kms key per rds. For clean terraform runs, you must first provision the key for the destination instance, then copy the snapshot using that kms key.

Example - I want a new instance rds-example-new to be provisioned from a snapshot of rds-example-old:

  1. Use the console to manually make a snapshot of rds instance rds-example-old

  2. provision the kms key for rds-example-new

    atmos terraform plan  rds-example-new -s ue1-staging '-target=module.kms_key_rds.aws_kms_key.default[0]'
    atmos terraform apply rds-example-new -s ue1-staging '-target=module.kms_key_rds.aws_kms_key.default[0]'
  3. Use the console to copy the snapshot to a new name using the above provisioned kms key

  4. Add snapshot_identifier variable to rds-example-new catalog and specify the newly copied snapshot that used the above key

  5. Post provisioning, remove the snapshot_idenfier variable and verify terraform runs clean for the copied instance

Variables

Required Variables

allocated_storage (number) required

The allocated storage in GBs

database_name (string) required

The name of the database to create when the DB instance is created

database_port (number) required

Database port (e.g. 3306 for MySQL). Used in the DB Security Group to allow access to the DB instance from the provided security_group_ids

db_parameter_group (string) required

The DB parameter group family name. The value depends on DB engine used. See DBParameterGroupFamily for instructions on how to retrieve applicable value.

engine (string) required

Database engine type

engine_version (string) required

Database engine version, depends on engine type

instance_class (string) required

Class of RDS instance

region (string) required

AWS Region

Optional Variables

allow_major_version_upgrade (bool) optional

Allow major version upgrade


Default value: false

allowed_cidr_blocks (list(string)) optional

The whitelisted CIDRs which to allow ingress traffic to the DB instance


Default value: [ ]

apply_immediately (bool) optional

Specifies whether any database modifications are applied immediately, or during the next maintenance window


Default value: false

associate_security_group_ids (list(string)) optional

The IDs of the existing security groups to associate with the DB instance


Default value: [ ]

auto_minor_version_upgrade (bool) optional

Allow automated minor version upgrade (e.g. from Postgres 9.5.3 to Postgres 9.5.4)


Default value: true

availability_zone (string) optional

The AZ for the RDS instance. Specify one of subnet_ids, db_subnet_group_name or availability_zone. If availability_zone is provided, the instance will be placed into the default VPC or EC2 Classic


Default value: null

backup_retention_period (number) optional

Backup retention period in days. Must be > 0 to enable backups


Default value: 0

backup_window (string) optional

When AWS can perform DB snapshots, can't overlap with maintenance window


Default value: "22:00-03:00"

ca_cert_identifier (string) optional

The identifier of the CA certificate for the DB instance


Default value: null

charset_name (string) optional

The character set name to use for DB encoding. Oracle & Microsoft SQL only. For other engines use db_parameter


Default value: null

client_security_group_enabled (bool) optional

create a client security group and include in attached default security group


Default value: true

copy_tags_to_snapshot (bool) optional

Copy tags from DB to a snapshot


Default value: true

database_password (string) optional

Database password for the admin user


Default value: ""

database_user (string) optional

Database admin user name


Default value: ""

db_options optional

A list of DB options to apply with an option group. Depends on DB engine


Type:

list(object({
db_security_group_memberships = list(string)
option_name = string
port = number
version = string
vpc_security_group_memberships = list(string)

option_settings = list(object({
name = string
value = string
}))
}))

Default value: [ ]

db_parameter optional

A list of DB parameters to apply. Note that parameters may differ from a DB family to another


Type:

list(object({
apply_method = string
name = string
value = string
}))

Default value: [ ]

db_subnet_group_name (string) optional

Name of DB subnet group. DB instance will be created in the VPC associated with the DB subnet group. Specify one of subnet_ids, db_subnet_group_name or availability_zone


Default value: null

deletion_protection (bool) optional

Set to true to enable deletion protection on the RDS instance


Default value: false

dns_gbl_delegated_environment_name (string) optional

The name of the environment where global dns_delegated is provisioned


Default value: "gbl"

dns_zone_id (string) optional

The ID of the DNS Zone in Route53 where a new DNS record will be created for the DB host name


Default value: ""

enabled_cloudwatch_logs_exports (list(string)) optional

List of log types to enable for exporting to CloudWatch logs. If omitted, no logs will be exported. Valid values (depending on engine): alert, audit, error, general, listener, slowquery, trace, postgresql (PostgreSQL), upgrade (PostgreSQL).


Default value: [ ]

final_snapshot_identifier (string) optional

Final snapshot identifier e.g.: some-db-final-snapshot-2019-06-26-06-05


Default value: ""

host_name (string) optional

The DB host name created in Route53


Default value: "db"

iam_database_authentication_enabled (bool) optional

Specifies whether or mappings of AWS Identity and Access Management (IAM) accounts to database accounts is enabled


Default value: false

iops (number) optional

The amount of provisioned IOPS. Setting this implies a storage_type of 'io1'. Default is 0 if rds storage type is not 'io1'


Default value: 0

kms_alias_name_ssm (string) optional

KMS alias name for SSM


Default value: "alias/aws/ssm"

kms_key_arn (string) optional

The ARN of the existing KMS key to encrypt storage


Default value: ""

license_model (string) optional

License model for this DB. Optional, but required for some DB Engines. Valid values: license-included | bring-your-own-license | general-public-license


Default value: ""

maintenance_window (string) optional

The window to perform maintenance in. Syntax: 'ddd:hh24:mi-ddd:hh24:mi' UTC


Default value: "Mon:03:00-Mon:04:00"

major_engine_version (string) optional

Database MAJOR engine version, depends on engine type


Default value: ""

max_allocated_storage (number) optional

The upper limit to which RDS can automatically scale the storage in GBs


Default value: 0

monitoring_interval (string) optional

The interval, in seconds, between points when Enhanced Monitoring metrics are collected for the DB instance. To disable collecting Enhanced Monitoring metrics, specify 0. Valid Values are 0, 1, 5, 10, 15, 30, 60.


Default value: "0"

monitoring_role_arn (string) optional

The ARN for the IAM role that permits RDS to send enhanced monitoring metrics to CloudWatch Logs


Default value: null

multi_az (bool) optional

Set to true if multi AZ deployment must be supported


Default value: false

option_group_name (string) optional

Name of the DB option group to associate


Default value: ""

parameter_group_name (string) optional

Name of the DB parameter group to associate


Default value: ""

performance_insights_enabled (bool) optional

Specifies whether Performance Insights are enabled.


Default value: false

performance_insights_kms_key_id (string) optional

The ARN for the KMS key to encrypt Performance Insights data. Once KMS key is set, it can never be changed.


Default value: null

performance_insights_retention_period (number) optional

The amount of time in days to retain Performance Insights data. Either 7 (7 days) or 731 (2 years).


Default value: 7

publicly_accessible (bool) optional

Determines if database can be publicly available (NOT recommended)


Default value: false

replicate_source_db (any) optional

If the rds db instance is a replica, supply the source database identifier here


Default value: null

security_group_ids (list(string)) optional

The IDs of the security groups from which to allow ingress traffic to the DB instance


Default value: [ ]

skip_final_snapshot (bool) optional

If true (default), no snapshot will be made before deleting DB


Default value: true

snapshot_identifier (string) optional

Snapshot identifier e.g: rds:production-2019-06-26-06-05. If specified, the module create cluster from the snapshot


Default value: null

ssm_enabled (bool) optional

If true create SSM keys for the database user and password.


Default value: false

ssm_key_format (string) optional

SSM path format. The values will will be used in the following order: var.ssm_key_prefix, var.name, var.ssm_key_*


Default value: "/%v/%v/%v"

ssm_key_hostname (string) optional

The SSM key to save the hostname. See var.ssm_path_format.


Default value: "admin/db_hostname"

ssm_key_password (string) optional

The SSM key to save the password. See var.ssm_path_format.


Default value: "admin/db_password"

ssm_key_port (string) optional

The SSM key to save the port. See var.ssm_path_format.


Default value: "admin/db_port"

ssm_key_prefix (string) optional

SSM path prefix. Omit the leading forward slash /.


Default value: "rds"

ssm_key_user (string) optional

The SSM key to save the user. See var.ssm_path_format.


Default value: "admin/db_user"

storage_encrypted (bool) optional

(Optional) Specifies whether the DB instance is encrypted. The default is false if not specified


Default value: true

storage_throughput (number) optional

The storage throughput value for the DB instance. Can only be set when storage_type is gp3. Cannot be specified if the allocated_storage value is below a per-engine threshold.


Default value: null

storage_type (string) optional

One of 'standard' (magnetic), 'gp2' (general purpose SSD), or 'io1' (provisioned IOPS SSD)


Default value: "standard"

timezone (string) optional

Time zone of the DB instance. timezone is currently only supported by Microsoft SQL Server. The timezone can only be set on creation. See MSSQL User Guide for more information.


Default value: null

use_dns_delegated (bool) optional

Use the dns-delegated dns_zone_id


Default value: false

use_eks_security_group (bool) optional

Use the eks default security group


Default value: false

use_private_subnets (bool) optional

Use private subnets


Default value: true

Context Variables

The following variables are defined in the context.tf file of this module and part of the terraform-null-label pattern.

additional_tag_map (map(string)) optional

Additional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration.


Required: No

Default value: { }

attributes (list(string)) optional

ID element. Additional attributes (e.g. workers or cluster) to add to id,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the delimiter
and treated as a single ID element.


Required: No

Default value: [ ]

context (any) optional

Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as null to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional_tag_map, which are merged.


Required: No

Default value:

{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
delimiter (string) optional

Delimiter to be used between ID elements.
Defaults to - (hyphen). Set to "" to use no delimiter at all.


Required: No

Default value: null

descriptor_formats (any) optional

Describe additional descriptors to be output in the descriptors output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
\{<br/> format = string<br/> labels = list(string)<br/> \}
(Type is any so the map values can later be enhanced to provide additional options.)
format is a Terraform format string to be passed to the format() function.
labels is a list of labels, in order, to pass to format() function.
Label values will be normalized before being passed to format() so they will be
identical to how they appear in id.
Default is {} (descriptors output will be empty).


Required: No

Default value: { }

enabled (bool) optional

Set to false to prevent the module from creating any resources
Required: No

Default value: null

environment (string) optional

ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'
Required: No

Default value: null

id_length_limit (number) optional

Limit id to this many characters (minimum 6).
Set to 0 for unlimited length.
Set to null for keep the existing setting, which defaults to 0.
Does not affect id_full.


Required: No

Default value: null

label_key_case (string) optional

Controls the letter case of the tags keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the tags input.
Possible values: lower, title, upper.
Default value: title.


Required: No

Default value: null

label_order (list(string)) optional

The order in which the labels (ID elements) appear in the id.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present.


Required: No

Default value: null

label_value_case (string) optional

Controls the letter case of ID elements (labels) as included in id,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the tags input.
Possible values: lower, title, upper and none (no transformation).
Set this to title and set delimiter to "" to yield Pascal Case IDs.
Default value: lower.


Required: No

Default value: null

labels_as_tags (set(string)) optional

Set of labels (ID elements) to include as tags in the tags output.
Default is to include all labels.
Tags with empty values will not be included in the tags output.
Set to [] to suppress all generated tags.
Notes:
The value of the name tag, if included, will be the id, not the name.
Unlike other null-label inputs, the initial setting of labels_as_tags cannot be
changed in later chained modules. Attempts to change it will be silently ignored.


Required: No

Default value:

[
"default"
]
name (string) optional

ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a tag.
The "name" tag is set to the full id string. There is no tag with the value of the name input.


Required: No

Default value: null

namespace (string) optional

ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique
Required: No

Default value: null

regex_replace_chars (string) optional

Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits.


Required: No

Default value: null

stage (string) optional

ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'
Required: No

Default value: null

tags (map(string)) optional

Additional tags (e.g. {'BusinessUnit': 'XYZ'}).
Neither the tag keys nor the tag values will be modified by this module.


Required: No

Default value: { }

tenant (string) optional

ID element (Rarely used, not included by default). A customer identifier, indicating who this instance of a resource is for
Required: No

Default value: null

Outputs

exports

Map of exports for use in deployment configuration templates

kms_key_alias

The KMS key alias

psql_helper

A helper output to use with psql for connecting to this RDS instance.

rds_address

Address of the instance

rds_arn

ARN of the instance

rds_database_ssm_key_prefix

SSM prefix

rds_endpoint

DNS Endpoint of the instance

rds_hostname

DNS host name of the instance

rds_id

ID of the instance

rds_name

RDS DB name

rds_option_group_id

ID of the Option Group

rds_parameter_group_id

ID of the Parameter Group

rds_port

RDS DB port

rds_resource_id

The RDS Resource ID of this instance.

rds_security_group_id

ID of the Security Group

rds_subnet_group_id

ID of the created Subnet Group

Dependencies

Requirements

  • terraform, version: >= 1.0.0
  • aws, version: >= 4.0
  • random, version: >= 2.3

Providers

  • aws, version: >= 4.0
  • random, version: >= 2.3

Modules

NameVersionSourceDescription
dns_gbl_delegated1.5.0cloudposse/stack-config/yaml//modules/remote-staten/a
eks1.5.0cloudposse/stack-config/yaml//modules/remote-staten/a
iam_roleslatest../account-map/modules/iam-rolesn/a
kms_key_rds0.12.1cloudposse/kms-key/awsn/a
rds_client_sg2.2.0cloudposse/security-group/awsn/a
rds_instance1.1.0cloudposse/rds/awsn/a
rds_monitoring_role0.17.0cloudposse/iam-role/awsn/a
this0.25.0cloudposse/label/nulln/a
vpc1.5.0cloudposse/stack-config/yaml//modules/remote-staten/a

Resources

The following resources are used by this module:

Data Sources

The following data sources are used by this module:

References