aurora-postgres
This component is responsible for provisioning Aurora Postgres RDS clusters. It seeds relevant database information (hostnames, username, password, etc.) into AWS SSM Parameter Store.
Usage
Stack Level: Regional
Here's an example for how to use this component.
stacks/catalog/aurora-postgres/defaults.yaml
file (base component for all Aurora Postgres clusters with default
settings):
components:
terraform:
aurora-postgres/defaults:
metadata:
type: abstract
vars:
enabled: true
name: aurora-postgres
tags:
Team: sre
Service: aurora-postgres
cluster_name: shared
deletion_protection: false
storage_encrypted: true
engine: aurora-postgresql
# Provisioned configuration
engine_mode: provisioned
engine_version: "15.3"
cluster_family: aurora-postgresql15
# 1 writer, 1 reader
cluster_size: 2
# https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Concepts.DBInstanceClass.html
instance_type: db.t3.medium
admin_user: postgres
admin_password: "" # generate random password
database_name: postgres
database_port: 5432
skip_final_snapshot: false
# Enhanced Monitoring
# A boolean flag to enable/disable the creation of the enhanced monitoring IAM role.
# If set to false, the module will not create a new role and will use rds_monitoring_role_arn for enhanced monitoring
enhanced_monitoring_role_enabled: true
# The interval, in seconds, between points when enhanced monitoring metrics are collected for the DB instance.
# To disable collecting Enhanced Monitoring metrics, specify 0. The default is 0. Valid Values: 0, 1, 5, 10, 15, 30, 60
rds_monitoring_interval: 15
# Allow ingress from the following accounts
# If any of tenant, stage, or environment aren't given, this will be taken
allow_ingress_from_vpc_accounts:
- tenant: core
stage: auto
Example (not actual):
stacks/uw2-dev.yaml
file (override the default settings for the cluster in the dev
account, create an additional
database and user):
import:
- catalog/aurora-postgres/defaults
components:
terraform:
aurora-postgres:
metadata:
component: aurora-postgres
inherits:
- aurora-postgres/defaults
vars:
enabled: true
Finding Aurora Engine Version
Use the following to query the AWS API by engine-mode
. Both provisioned and Serverless v2 use the privisoned
engine
mode, whereas only Serverless v1 uses the serverless
engine mode.
aws rds describe-db-engine-versions \
--engine aurora-postgresql \
--query 'DBEngineVersions[].EngineVersion' \
--filters 'Name=engine-mode,Values=serverless'
Use the following to query AWS API by db-instance-class
. Use this query to find supported versions for a specific
instance class, such as db.serverless
with Serverless v2.
aws rds describe-orderable-db-instance-options \
--engine aurora-postgresql \
--db-instance-class db.serverless \
--query 'OrderableDBInstanceOptions[].[EngineVersion]'
Once a version has been selected, use the following to find the cluster family.
aws rds describe-db-engine-versions --engine aurora-postgresql --query "DBEngineVersions[]" | \
jq '.[] | select(.EngineVersion == "15.3") |
{ Engine: .Engine, EngineVersion: .EngineVersion, DBParameterGroupFamily: .DBParameterGroupFamily }'
Examples
Generally there are three different engine configurations for Aurora: provisioned, Serverless v1, and Serverless v2.
Provisioned Aurora Postgres
See the default usage example above
Serverless v1 Aurora Postgres
Serverless v1 requires engine-mode
set to serverless
uses scaling_configuration
to configure scaling options.
For valid values, see ModifyCurrentDBClusterCapacity.
components:
terraform:
aurora-postgres:
vars:
enabled: true
name: aurora-postgres
eks_component_names:
- eks/cluster
allow_ingress_from_vpc_accounts:
# Allows Spacelift
- tenant: core
stage: auto
environment: use2
# Allows VPN
- tenant: core
stage: network
environment: use2
cluster_name: shared
engine: aurora-postgresql
# Serverless v1 configuration
engine_mode: serverless
instance_type: "" # serverless engine_mode ignores `var.instance_type`
engine_version: "13.9" # Latest supported version as of 08/28/2023
cluster_family: aurora-postgresql13
cluster_size: 0 # serverless
scaling_configuration:
- auto_pause: true
max_capacity: 5
min_capacity: 2
seconds_until_auto_pause: 300
timeout_action: null
admin_user: postgres
admin_password: "" # generate random password
database_name: postgres
database_port: 5432
storage_encrypted: true
deletion_protection: true
skip_final_snapshot: false
# Creating read-only users or additional databases requires Spacelift
read_only_users_enabled: false
# Enhanced Monitoring
# A boolean flag to enable/disable the creation of the enhanced monitoring IAM role.
# If set to false, the module will not create a new role and will use rds_monitoring_role_arn for enhanced monitoring
enhanced_monitoring_role_enabled: true
enhanced_monitoring_attributes: ["monitoring"]
# The interval, in seconds, between points when enhanced monitoring metrics are collected for the DB instance.
# To disable collecting Enhanced Monitoring metrics, specify 0. The default is 0. Valid Values: 0, 1, 5, 10, 15, 30, 60
rds_monitoring_interval: 15
iam_database_authentication_enabled: false
additional_users: {}
Serverless v2 Aurora Postgres
Aurora Postgres Serverless v2 uses the provisioned
engine mode with db.serverless
instances. In order to configure
scaling with Serverless v2, use var.serverlessv2_scaling_configuration
.
For more on valid scaling configurations, see Performance and scaling for Aurora Serverless v2.
components:
terraform:
aurora-postgres:
vars:
enabled: true
name: aurora-postgres
eks_component_names:
- eks/cluster
allow_ingress_from_vpc_accounts:
# Allows Spacelift
- tenant: core
stage: auto
environment: use2
# Allows VPN
- tenant: core
stage: network
environment: use2
cluster_name: shared
engine: aurora-postgresql
# Serverless v2 configuration
engine_mode: provisioned
instance_type: "db.serverless"
engine_version: "15.3"
cluster_family: aurora-postgresql15
cluster_size: 2
serverlessv2_scaling_configuration:
min_capacity: 2
max_capacity: 64
admin_user: postgres
admin_password: "" # generate random password
database_name: postgres
database_port: 5432
storage_encrypted: true
deletion_protection: true
skip_final_snapshot: false
# Creating read-only users or additional databases requires Spacelift
read_only_users_enabled: false
# Enhanced Monitoring
# A boolean flag to enable/disable the creation of the enhanced monitoring IAM role.
# If set to false, the module will not create a new role and will use rds_monitoring_role_arn for enhanced monitoring
enhanced_monitoring_role_enabled: true
enhanced_monitoring_attributes: ["monitoring"]
# The interval, in seconds, between points when enhanced monitoring metrics are collected for the DB instance.
# To disable collecting Enhanced Monitoring metrics, specify 0. The default is 0. Valid Values: 0, 1, 5, 10, 15, 30, 60
rds_monitoring_interval: 15
iam_database_authentication_enabled: false
additional_users: {}
Variables
Required Variables
cluster_name
(string
) requiredShort name for this cluster
cluster_size
(number
) requiredPostgres cluster size
engine_mode
(string
) requiredThe database engine mode. Valid values:
global
,multimaster
,parallelquery
,provisioned
,serverless
instance_type
(string
) requiredEC2 instance type for Postgres cluster
region
(string
) requiredAWS Region
Optional Variables
admin_password
(string
) optionalPostgres password for the admin user
Default value:
""
admin_user
(string
) optionalPostgres admin user name
Default value:
""
allow_ingress_from_vpc_accounts
optionalList of account contexts to pull VPC ingress CIDR and add to cluster security group.
e.g.
{
environment = "ue2",
stage = "auto",
tenant = "core"
}Defaults to the "vpc" component in the given account
Type:
list(object({
vpc = optional(string, "vpc")
environment = optional(string)
stage = optional(string)
tenant = optional(string)
}))Default value:
[ ]
allow_major_version_upgrade
(bool
) optionalEnable to allow major engine version upgrades when changing engine versions. Defaults to false.
Default value:
false
allowed_cidr_blocks
(list(string)
) optionalList of CIDRs allowed to access the database (in addition to security groups and subnets)
Default value:
[ ]
allowed_security_group_names
(list(string)
) optionalList of security group names (tags) that should be allowed access to the database
Default value:
[ ]
autoscaling_enabled
(bool
) optionalWhether to enable cluster autoscaling
Default value:
false
autoscaling_max_capacity
(number
) optionalMaximum number of instances to be maintained by the autoscaler
Default value:
5
autoscaling_min_capacity
(number
) optionalMinimum number of instances to be maintained by the autoscaler
Default value:
1
autoscaling_policy_type
(string
) optionalAutoscaling policy type.
TargetTrackingScaling
andStepScaling
are supportedDefault value:
"TargetTrackingScaling"
autoscaling_scale_in_cooldown
(number
) optionalThe amount of time, in seconds, after a scaling activity completes and before the next scaling down activity can start. Default is 300s
Default value:
300
autoscaling_scale_out_cooldown
(number
) optionalThe amount of time, in seconds, after a scaling activity completes and before the next scaling up activity can start. Default is 300s
Default value:
300
autoscaling_target_metrics
(string
) optionalThe metrics type to use. If this value isn't provided the default is CPU utilization
Default value:
"RDSReaderAverageCPUUtilization"
autoscaling_target_value
(number
) optionalThe target value to scale with respect to target metrics
Default value:
75
backup_window
(string
) optionalDaily time range during which the backups happen, UTC
Default value:
"07:00-09:00"
ca_cert_identifier
(string
) optionalThe identifier of the CA certificate for the DB instance
Default value:
null
cluster_dns_name_part
(string
) optionalPart of DNS name added to module and cluster name for DNS for cluster endpoint
Default value:
"writer"
cluster_family
(string
) optionalFamily of the DB parameter group. Valid values for Aurora PostgreSQL:
aurora-postgresql9.6
,aurora-postgresql10
,aurora-postgresql11
,aurora-postgresql12
Default value:
"aurora-postgresql13"
cluster_parameters
optionalList of DB cluster parameters to apply
Type:
list(object({
apply_method = string
name = string
value = string
}))Default value:
[ ]
database_name
(string
) optionalName for an automatically created database on cluster creation. An empty name will generate a db name.
Default value:
""
database_port
(number
) optionalDatabase port
Default value:
5432
deletion_protection
(bool
) optionalSpecifies whether the Cluster should have deletion protection enabled. The database can't be deleted when this value is set to
true
Default value:
false
dns_gbl_delegated_environment_name
(string
) optionalThe name of the environment where global
dns_delegated
is provisionedDefault value:
"gbl"
eks_component_names
(set(string)
) optionalThe names of the eks components
Default value:
[
"eks/cluster"
]eks_security_group_enabled
(bool
) optionalUse the eks default security group
Default value:
false
enabled_cloudwatch_logs_exports
(list(string)
) optionalList of log types to export to cloudwatch. The following log types are supported: audit, error, general, slowquery
Default value:
[ ]
engine
(string
) optionalName of the database engine to be used for the DB cluster
Default value:
"postgresql"
engine_version
(string
) optionalEngine version of the Aurora global database
Default value:
"13.4"
enhanced_monitoring_attributes
(list(string)
) optionalAttributes used to format the Enhanced Monitoring IAM role. If this role hits IAM role length restrictions (max 64 characters), consider shortening these strings.
Default value:
[
"enhanced-monitoring"
]enhanced_monitoring_role_enabled
(bool
) optionalA boolean flag to enable/disable the creation of the enhanced monitoring IAM role. If set to
false
, the module will not create a new role and will userds_monitoring_role_arn
for enhanced monitoringDefault value:
true
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
intra_security_group_traffic_enabled
(bool
) optionalWhether to allow traffic between resources inside the database's security group.
Default value:
false
maintenance_window
(string
) optionalWeekly time range during which system maintenance can occur, in UTC
Default value:
"wed:03:00-wed:04:00"
performance_insights_enabled
(bool
) optionalWhether to enable Performance Insights
Default value:
false
publicly_accessible
(bool
) optionalSet true to make this database accessible from the public internet
Default value:
false
rds_monitoring_interval
(number
) optionalThe interval, in seconds, between points when enhanced monitoring metrics are collected for the DB instance. To disable collecting Enhanced Monitoring metrics, specify 0. The default is 0. Valid Values: 0, 1, 5, 10, 15, 30, 60
Default value:
60
reader_dns_name_part
(string
) optionalPart of DNS name added to module and cluster name for DNS for cluster reader
Default value:
"reader"
retention_period
(number
) optionalNumber of days to retain backups for
Default value:
5
scaling_configuration
optionalList of nested attributes with scaling properties. Only valid when
engine_mode
is set toserverless
. This is required for Serverless v1Type:
list(object({
auto_pause = bool
max_capacity = number
min_capacity = number
seconds_until_auto_pause = number
timeout_action = string
}))Default value:
[ ]
serverlessv2_scaling_configuration
optionalNested attribute with scaling properties for ServerlessV2. Only valid when
engine_mode
is set toprovisioned.
This is required for Serverless v2Type:
object({
min_capacity = number
max_capacity = number
})Default value:
null
skip_final_snapshot
(bool
) optionalNormally AWS makes a snapshot of the database before deleting it. Set this to
true
in order to skip this.
NOTE: The final snapshot has a name derived from the cluster name. If you delete a cluster, get a final snapshot,
then create a cluster of the same name, its final snapshot will fail with a name collision unless you delete
the previous final snapshot first.Default value:
false
snapshot_identifier
(string
) optionalSpecifies whether or not to create this cluster from a snapshot
Default value:
null
ssm_cluster_name_override
(string
) optionalSet a cluster name into the ssm path prefix
Default value:
""
ssm_path_prefix
(string
) optionalTop level SSM path prefix (without leading or trailing slash)
Default value:
"aurora-postgres"
storage_encrypted
(bool
) optionalSpecifies whether the DB cluster is encrypted
Default value:
true
vpc_component_name
(string
) optionalThe name of the VPC component
Default value:
"vpc"
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
admin_username
Postgres admin username
allowed_security_groups
The resulting list of security group IDs that are allowed to connect to the Aurora Postgres cluster.
cluster_identifier
Postgres cluster identifier
config_map
Map containing information pertinent to a PostgreSQL client configuration.
database_name
Postgres database name
kms_key_arn
KMS key ARN for Aurora Postgres
master_hostname
Postgres master hostname
replicas_hostname
Postgres replicas hostname
ssm_key_paths
Names (key paths) of all SSM parameters stored for this cluster
Dependencies
Requirements
terraform
, version:>= 1.3.0
aws
, version:>= 4.9.0
postgresql
, version:>= 1.17.1
random
, version:>= 2.3
Providers
aws
, version:>= 4.9.0
random
, version:>= 2.3
Modules
Name | Version | Source | Description |
---|---|---|---|
aurora_postgres_cluster | 1.3.2 | cloudposse/rds-cluster/aws | https://www.terraform.io/docs/providers/aws/r/rds_cluster.html |
cluster | 0.25.0 | cloudposse/label/null | n/a |
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 |
parameter_store_write | 0.11.0 | cloudposse/ssm-parameter-store/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 |
vpc_ingress | 1.5.0 | cloudposse/stack-config/yaml//modules/remote-state | n/a |
Resources
The following resources are used by this module:
random_password.admin_password
(resource)random_pet.admin_user
(resource)random_pet.database_name
(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)aws_partition.current
(data source)aws_security_groups.allowed
(data source)
References
- cloudposse/terraform-aws-components - Cloud Posse's upstream component