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
:
-
Use the console to manually make a snapshot of rds instance
rds-example-old
-
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]' -
Use the console to copy the snapshot to a new name using the above provisioned kms key
-
Add
snapshot_identifier
variable tords-example-new
catalog and specify the newly copied snapshot that used the above key -
Post provisioning, remove the
snapshot_idenfier
variable and verify terraform runs clean for the copied instance
Variables
Required Variables
allocated_storage
(number
) requiredThe allocated storage in GBs
database_name
(string
) requiredThe name of the database to create when the DB instance is created
database_port
(number
) requiredDatabase port (e.g.
3306
forMySQL
). Used in the DB Security Group to allow access to the DB instance from the providedsecurity_group_ids
db_parameter_group
(string
) requiredThe DB parameter group family name. The value depends on DB engine used. See DBParameterGroupFamily for instructions on how to retrieve applicable value.
engine
(string
) requiredDatabase engine type
engine_version
(string
) requiredDatabase engine version, depends on engine type
instance_class
(string
) requiredClass of RDS instance
region
(string
) requiredAWS Region
Optional Variables
allow_major_version_upgrade
(bool
) optionalAllow major version upgrade
Default value:
false
allowed_cidr_blocks
(list(string)
) optionalThe whitelisted CIDRs which to allow
ingress
traffic to the DB instanceDefault value:
[ ]
apply_immediately
(bool
) optionalSpecifies whether any database modifications are applied immediately, or during the next maintenance window
Default value:
false
associate_security_group_ids
(list(string)
) optionalThe IDs of the existing security groups to associate with the DB instance
Default value:
[ ]
auto_minor_version_upgrade
(bool
) optionalAllow automated minor version upgrade (e.g. from Postgres 9.5.3 to Postgres 9.5.4)
Default value:
true
availability_zone
(string
) optionalThe AZ for the RDS instance. Specify one of
subnet_ids
,db_subnet_group_name
oravailability_zone
. Ifavailability_zone
is provided, the instance will be placed into the default VPC or EC2 ClassicDefault value:
null
backup_retention_period
(number
) optionalBackup retention period in days. Must be > 0 to enable backups
Default value:
0
backup_window
(string
) optionalWhen AWS can perform DB snapshots, can't overlap with maintenance window
Default value:
"22:00-03:00"
ca_cert_identifier
(string
) optionalThe identifier of the CA certificate for the DB instance
Default value:
null
charset_name
(string
) optionalThe 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
) optionalcreate a client security group and include in attached default security group
Default value:
true
copy_tags_to_snapshot
(bool
) optionalCopy tags from DB to a snapshot
Default value:
true
database_password
(string
) optionalDatabase password for the admin user
Default value:
""
database_user
(string
) optionalDatabase admin user name
Default value:
""
db_options
optionalA 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
optionalA 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
) optionalName 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
oravailability_zone
Default value:
null
deletion_protection
(bool
) optionalSet to true to enable deletion protection on the RDS instance
Default value:
false
dns_gbl_delegated_environment_name
(string
) optionalThe name of the environment where global
dns_delegated
is provisionedDefault value:
"gbl"
dns_zone_id
(string
) optionalThe 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)
) optionalList 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
) optionalFinal snapshot identifier e.g.: some-db-final-snapshot-2019-06-26-06-05
Default value:
""
host_name
(string
) optionalThe DB host name created in Route53
Default value:
"db"
iam_database_authentication_enabled
(bool
) optionalSpecifies whether or mappings of AWS Identity and Access Management (IAM) accounts to database accounts is enabled
Default value:
false
iops
(number
) optionalThe 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
) optionalKMS alias name for SSM
Default value:
"alias/aws/ssm"
kms_key_arn
(string
) optionalThe ARN of the existing KMS key to encrypt storage
Default value:
""
license_model
(string
) optionalLicense 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
) optionalThe 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
) optionalDatabase MAJOR engine version, depends on engine type
Default value:
""
max_allocated_storage
(number
) optionalThe upper limit to which RDS can automatically scale the storage in GBs
Default value:
0
monitoring_interval
(string
) optionalThe 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
) optionalThe ARN for the IAM role that permits RDS to send enhanced monitoring metrics to CloudWatch Logs
Default value:
null
multi_az
(bool
) optionalSet to true if multi AZ deployment must be supported
Default value:
false
option_group_name
(string
) optionalName of the DB option group to associate
Default value:
""
parameter_group_name
(string
) optionalName of the DB parameter group to associate
Default value:
""
performance_insights_enabled
(bool
) optionalSpecifies whether Performance Insights are enabled.
Default value:
false
performance_insights_kms_key_id
(string
) optionalThe 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
) optionalThe amount of time in days to retain Performance Insights data. Either 7 (7 days) or 731 (2 years).
Default value:
7
publicly_accessible
(bool
) optionalDetermines if database can be publicly available (NOT recommended)
Default value:
false
replicate_source_db
(any
) optionalIf the rds db instance is a replica, supply the source database identifier here
Default value:
null
security_group_ids
(list(string)
) optionalThe IDs of the security groups from which to allow
ingress
traffic to the DB instanceDefault value:
[ ]
skip_final_snapshot
(bool
) optionalIf true (default), no snapshot will be made before deleting DB
Default value:
true
snapshot_identifier
(string
) optionalSnapshot 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
) optionalIf
true
create SSM keys for the database user and password.Default value:
false
ssm_key_format
(string
) optionalSSM 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
) optionalThe SSM key to save the hostname. See
var.ssm_path_format
.Default value:
"admin/db_hostname"
ssm_key_password
(string
) optionalThe SSM key to save the password. See
var.ssm_path_format
.Default value:
"admin/db_password"
ssm_key_port
(string
) optionalThe SSM key to save the port. See
var.ssm_path_format
.Default value:
"admin/db_port"
ssm_key_prefix
(string
) optionalSSM path prefix. Omit the leading forward slash
/
.Default value:
"rds"
ssm_key_user
(string
) optionalThe 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
) optionalThe storage throughput value for the DB instance. Can only be set when
storage_type
isgp3
. Cannot be specified if theallocated_storage
value is below a per-engine threshold.Default value:
null
storage_type
(string
) optionalOne of 'standard' (magnetic), 'gp2' (general purpose SSD), or 'io1' (provisioned IOPS SSD)
Default value:
"standard"
timezone
(string
) optionalTime 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
) optionalUse the dns-delegated dns_zone_id
Default value:
false
use_eks_security_group
(bool
) optionalUse the eks default security group
Default value:
false
use_private_subnets
(bool
) optionalUse 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.
context.tf
file of this module and part of the terraform-null-label pattern.additional_tag_map
(map(string)
) optionalAdditional key-value pairs to add to each map in
tags_as_list_of_maps
. Not added totags
orid
.
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)
) optionalID element. Additional attributes (e.g.
workers
orcluster
) to add toid
,
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 thedelimiter
and treated as a single ID element.Required: No
Default value:
[ ]
context
(any
) optionalSingle object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables asnull
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
) optionalDelimiter 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
) optionalDescribe 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 isany
so the map values can later be enhanced to provide additional options.)
format
is a Terraform format string to be passed to theformat()
function.
labels
is a list of labels, in order, to pass toformat()
function.
Label values will be normalized before being passed toformat()
so they will be
identical to how they appear inid
.
Default is{}
(descriptors
output will be empty).Required: No
Default value:
{ }
enabled
(bool
) optionalSet to false to prevent the module from creating any resources
Required: NoDefault value:
null
environment
(string
) optionalID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'
Required: NoDefault value:
null
id_length_limit
(number
) optionalLimit
id
to this many characters (minimum 6).
Set to0
for unlimited length.
Set tonull
for keep the existing setting, which defaults to0
.
Does not affectid_full
.Required: No
Default value:
null
label_key_case
(string
) optionalControls the letter case of the
tags
keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via thetags
input.
Possible values:lower
,title
,upper
.
Default value:title
.Required: No
Default value:
null
label_order
(list(string)
) optionalThe 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
) optionalControls 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 thetags
input.
Possible values:lower
,title
,upper
andnone
(no transformation).
Set this totitle
and setdelimiter
to""
to yield Pascal Case IDs.
Default value:lower
.Required: No
Default value:
null
labels_as_tags
(set(string)
) optionalSet 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 thetags
output.
Set to[]
to suppress all generated tags.
Notes:
The value of thename
tag, if included, will be theid
, not thename
.
Unlike othernull-label
inputs, the initial setting oflabels_as_tags
cannot be
changed in later chained modules. Attempts to change it will be silently ignored.Required: No
Default value:
[
"default"
]name
(string
) optionalID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as atag
.
The "name" tag is set to the fullid
string. There is no tag with the value of thename
input.Required: No
Default value:
null
namespace
(string
) optionalID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique
Required: NoDefault value:
null
regex_replace_chars
(string
) optionalTerraform 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
) optionalID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'
Required: NoDefault value:
null
tags
(map(string)
) optionalAdditional 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
) optionalID element (Rarely used, not included by default). A customer identifier, indicating who this instance of a resource is for
Required: NoDefault 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
Name | Version | Source | Description |
---|---|---|---|
dns_gbl_delegated | 1.5.0 | cloudposse/stack-config/yaml//modules/remote-state | n/a |
eks | 1.5.0 | cloudposse/stack-config/yaml//modules/remote-state | n/a |
iam_roles | latest | ../account-map/modules/iam-roles | n/a |
kms_key_rds | 0.12.1 | cloudposse/kms-key/aws | n/a |
rds_client_sg | 2.2.0 | cloudposse/security-group/aws | n/a |
rds_instance | 1.1.0 | cloudposse/rds/aws | n/a |
rds_monitoring_role | 0.17.0 | cloudposse/iam-role/aws | n/a |
this | 0.25.0 | cloudposse/label/null | n/a |
vpc | 1.5.0 | cloudposse/stack-config/yaml//modules/remote-state | n/a |
Resources
The following resources are used by this module:
aws_ssm_parameter.rds_database_hostname
(resource)aws_ssm_parameter.rds_database_password
(resource)aws_ssm_parameter.rds_database_port
(resource)aws_ssm_parameter.rds_database_user
(resource)random_password.database_password
(resource)random_pet.database_user
(resource)
Data Sources
The following data sources are used by this module:
aws_caller_identity.current
(data source)aws_iam_policy_document.kms_key_rds
(data source)
References
- cloudposse/terraform-aws-components - Cloud Posse's upstream component