AWS RDS Terraform module

March 19, 2026 ยท View on GitHub

Terraform module which creates RDS resources on AWS.

SWUbanner

Root module calls these modules which can also be used separately to create independent resources:

Usage

module "db" {
  source = "terraform-aws-modules/rds/aws"

  identifier = "demodb"

  engine            = "mysql"
  engine_version    = "8.0"
  instance_class    = "db.t3a.large"
  allocated_storage = 5

  db_name  = "demodb"
  username = "user"
  port     = "3306"

  iam_database_authentication_enabled = true

  vpc_security_group_ids = ["sg-12345678"]

  maintenance_window = "Mon:00:00-Mon:03:00"
  backup_window      = "03:00-06:00"

  # Enhanced Monitoring - see example for details on how to create the role
  # by yourself, in case you don't want to create it automatically
  monitoring_interval    = "30"
  monitoring_role_name   = "MyRDSMonitoringRole"
  create_monitoring_role = true

  tags = {
    Owner       = "user"
    Environment = "dev"
  }

  # DB subnet group
  create_db_subnet_group = true
  subnet_ids             = ["subnet-12345678", "subnet-87654321"]

  # DB parameter group
  family = "mysql8.0"

  # DB option group
  major_engine_version = "8.0"

  # Database Deletion Protection
  deletion_protection = true

  parameters = [
    {
      name  = "character_set_client"
      value = "utf8mb4"
    },
    {
      name  = "character_set_server"
      value = "utf8mb4"
    }
  ]

  options = [
    {
      option_name = "MARIADB_AUDIT_PLUGIN"

      option_settings = [
        {
          name  = "SERVER_AUDIT_EVENTS"
          value = "CONNECT"
        },
        {
          name  = "SERVER_AUDIT_FILE_ROTATIONS"
          value = "37"
        },
      ]
    },
  ]
}

Conditional creation

The following values are provided to toggle on/off creation of the associated resources as desired:

module "db" {
  source = "terraform-aws-modules/rds/aws"

  # Disable creation of RDS instance(s)
  create_db_instance = false

  # Disable creation of option group - provide an option group or default AWS default
  create_db_option_group = false

  # Disable creation of parameter group - provide a parameter group or default to AWS default
  create_db_parameter_group = false

  # Enable creation of subnet group (disabled by default)
  create_db_subnet_group = true

  # Enable creation of monitoring IAM role
  create_monitoring_role = true

  # ... omitted
}

Option Groups

Reference

Users have the ability to:

  • Create an option group with the name provided:
  option_group_name            = "prod-instance-mysql-8.0"
  option_group_use_name_prefix = false
  • Create an option group using a unique prefix beginning with the name provided:
  option_group_name = "prod-instance-mysql-8.0"
  • Pass the name of an option group to use that has been created outside of the module:
  create_db_option_group = false
  option_group_name      = "prod-instance-mysql-8.0" # must already exist in AWS
  • Skip creating an option group for PostgreSQL entirely as that is not supported
  engine            = "postgres"
  option_group_name = "prod-instance-postgresql-11.0" # this will be ignored, no option group created
  • Use a default option group provided by AWS
  create_db_option_group = false

Parameter Groups

Reference

Users have the ability to:

  • Create a parameter group with the name provided:
  parameter_group_name            = "prod-instance-mysql-8.0"
  parameter_group_use_name_prefix = false
  • Create a parameter group using a unique prefix beginning with the name provided:
  parameter_group_name = "prod-instance-mysql-8.0"
  • Pass the name of a parameter group to use that has been created outside of the module:
  create_db_parameter_group = false
  parameter_group_name   = "prod-instance-mysql-8.0" # must already exist in AWS
  • Use a default parameter group provided by AWS
  create_db_parameter_group = false

Examples

Notes

  1. This module does not create RDS security group. Use terraform-aws-security-group module for this.
  2. For an RDS instance with storage_type using gp3, be aware that iops and storage_throughput cannot be specified if the allocated_storage value is below a per-engine threshold. See the RDS User Guide for details.

Requirements

NameVersion
terraform>= 1.11.1
aws>= 6.28

Providers

No providers.

Modules

NameSourceVersion
db_instance./modules/db_instancen/a
db_instance_role_association./modules/db_instance_role_associationn/a
db_option_group./modules/db_option_groupn/a
db_parameter_group./modules/db_parameter_groupn/a
db_subnet_group./modules/db_subnet_groupn/a

Resources

No resources.

Inputs

NameDescriptionTypeDefaultRequired
allocated_storageThe allocated storage in gigabytesnumbernullno
allow_major_version_upgradeIndicates that major version upgrades are allowed. Changing this parameter does not result in an outage and the change is asynchronously applied as soon as possibleboolfalseno
apply_immediatelySpecifies whether any database modifications are applied immediately, or during the next maintenance windowboolfalseno
auto_minor_version_upgradeIndicates that minor engine upgrades will be applied automatically to the DB instance during the maintenance windowbooltrueno
availability_zoneThe Availability Zone of the RDS instancestringnullno
backup_retention_periodThe days to retain backups fornumbernullno
backup_windowThe daily time range (in UTC) during which automated backups are created if they are enabled. Example: '09:46-10:16'. Must not overlap with maintenance_windowstringnullno
blue_green_updateEnables low-downtime updates using RDS Blue/Green deployments.
object({
enabled = optional(bool)
})
nullno
ca_cert_identifierSpecifies the identifier of the CA certificate for the DB instancestringnullno
character_set_nameThe character set name to use for DB encoding in Oracle instances. This can't be changed. See Oracle Character Sets Supported in Amazon RDS and Collations and Character Sets for Microsoft SQL Server for more information. This can only be set on creationstringnullno
cloudwatch_log_group_classSpecified the log class of the log group. Possible values are: STANDARD or INFREQUENT_ACCESSstringnullno
cloudwatch_log_group_kms_key_idThe ARN of the KMS Key to use when encrypting log datastringnullno
cloudwatch_log_group_retention_in_daysThe number of days to retain CloudWatch logs for the DB instancenumber7no
cloudwatch_log_group_skip_destroySet to true if you do not wish the log group (and any logs it may contain) to be deleted at destroy time, and instead just remove the log group from the Terraform stateboolnullno
cloudwatch_log_group_tagsAdditional tags for the CloudWatch log group(s)map(string){}no
copy_tags_to_snapshotOn delete, copy all Instance tags to the final snapshotbooltrueno
create_cloudwatch_log_groupDetermines whether a CloudWatch log group is created for each enabled_cloudwatch_logs_exportsboolfalseno
create_db_instanceWhether to create a database instancebooltrueno
create_db_option_groupCreate a database option groupbooltrueno
create_db_parameter_groupWhether to create a database parameter groupbooltrueno
create_db_subnet_groupWhether to create a database subnet groupboolfalseno
create_monitoring_roleCreate IAM role with a defined name that permits RDS to send enhanced monitoring metrics to CloudWatch Logsboolfalseno
custom_iam_instance_profileRDS custom iam instance profilestringnullno
customer_owned_ip_enabledIndicates whether to enable a customer-owned IP address (CoIP) for an RDS on Outposts DB instanceboolnullno
database_insights_modeThe mode of Database Insights that is enabled for the instance. Valid values: standard, advancedstringnullno
db_instance_role_associationsA map of DB instance supported feature name to role association ARNs.map(string){}no
db_instance_tagsAdditional tags for the DB instancemap(string){}no
db_nameThe DB name to create. If omitted, no database is created initiallystringnullno
db_option_group_tagsAdditional tags for the DB option groupmap(string){}no
db_parameter_group_tagsAdditional tags for the DB parameter groupmap(string){}no
db_subnet_group_descriptionDescription of the DB subnet group to createstringnullno
db_subnet_group_nameName of DB subnet group. DB instance will be created in the VPC associated with the DB subnet group. If unspecified, will be created in the default VPCstringnullno
db_subnet_group_tagsAdditional tags for the DB subnet groupmap(string){}no
db_subnet_group_use_name_prefixDetermines whether to use subnet_group_name as is or create a unique name beginning with the subnet_group_name as the prefixbooltrueno
dedicated_log_volumeUse a dedicated log volume (DLV) for the DB instance. Requires Provisioned IOPS.boolfalseno
delete_automated_backupsSpecifies whether to remove automated backups immediately after the DB instance is deletedbooltrueno
deletion_protectionThe database can't be deleted when this value is set to trueboolfalseno
domainThe ID of the Directory Service Active Directory domain to create the instance instringnullno
domain_auth_secret_arn(Optional, but required if domain_fqdn is provided) The ARN for the Secrets Manager secret with the self managed Active Directory credentials for the user joining the domain. Conflicts with domain and domain_iam_role_name.stringnullno
domain_dns_ips(Optional, but required if domain_fqdn is provided) The IPv4 DNS IP addresses of your primary and secondary self managed Active Directory domain controllers. Two IP addresses must be provided. If there isn't a secondary domain controller, use the IP address of the primary domain controller for both entries in the list. Conflicts with domain and domain_iam_role_name.list(string)nullno
domain_fqdnThe fully qualified domain name (FQDN) of the self managed Active Directory domain. Conflicts with domain and domain_iam_role_name.stringnullno
domain_iam_role_name(Required if domain is provided) The name of the IAM role to be used when making API calls to the Directory Servicestringnullno
domain_ou(Optional, but required if domain_fqdn is provided) The self managed Active Directory organizational unit for your DB instance to join. Conflicts with domain and domain_iam_role_name.stringnullno
enabled_cloudwatch_logs_exportsList 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)list(string)[]no
engineThe database engine to usestringnullno
engine_lifecycle_supportThe life cycle type for this DB instance. This setting applies only to RDS for MySQL and RDS for PostgreSQL. Valid values are open-source-rds-extended-support, open-source-rds-extended-support-disabled. Default value is open-source-rds-extended-support.stringnullno
engine_versionThe engine version to usestringnullno
familyThe family of the DB parameter groupstringnullno
final_snapshot_identifier_prefixThe name which is prefixed to the final snapshot on cluster destroystring"final"no
iam_database_authentication_enabledSpecifies whether or not the mappings of AWS Identity and Access Management (IAM) accounts to database accounts are enabledboolfalseno
identifierThe name of the RDS instancestringn/ayes
instance_classThe instance type of the RDS instancestringnullno
instance_use_identifier_prefixDetermines whether to use identifier as is or create a unique identifier beginning with identifier as the specified prefixboolfalseno
iopsThe amount of provisioned IOPS. Setting this implies a storage_type of 'io1' or gp3. See notes for limitations regarding this variable for gp3numbernullno
kms_key_idThe ARN for the KMS encryption key. If creating an encrypted replica, set this to the destination KMS ARN. If storage_encrypted is set to true and kms_key_id is not specified the default KMS key created in your account will be used. Be sure to use the full ARN, not a key alias.stringnullno
license_modelLicense model information for this DB instance. Optional, but required for some DB engines, i.e. Oracle SE1stringnullno
maintenance_windowThe window to perform maintenance in. Syntax: 'ddd:hh24:mi-ddd:hh24:mi'. Eg: 'Mon:00:00-Mon:03:00'stringnullno
major_engine_versionSpecifies the major version of the engine that this option group should be associated withstringnullno
manage_master_user_passwordSet to true to allow RDS to manage the master user password in Secrets Managerbooltrueno
manage_master_user_password_rotationWhether to manage the master user password rotation. By default, false on creation, rotation is managed by RDS. There is not currently a way to disable this on initial creation even when set to false. Setting this value to false after previously having been set to true will disable automatic rotation.boolfalseno
master_user_password_rotate_immediatelySpecifies whether to rotate the secret immediately or wait until the next scheduled rotation window.boolnullno
master_user_password_rotation_automatically_after_daysSpecifies the number of days between automatic scheduled rotations of the secret. Either automatically_after_days or schedule_expression must be specified.numbernullno
master_user_password_rotation_durationThe length of the rotation window in hours. For example, 3h for a three hour window.stringnullno
master_user_password_rotation_schedule_expressionA cron() or rate() expression that defines the schedule for rotating your secret. Either automatically_after_days or schedule_expression must be specified.stringnullno
master_user_secret_kms_key_idThe key ARN, key ID, alias ARN or alias name for the KMS key to encrypt the master user password secret in Secrets Manager.
If not specified, the default KMS key for your Amazon Web Services account is used.
stringnullno
max_allocated_storageSpecifies the value for Storage Autoscalingnumber0no
monitoring_intervalThe 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, 60number0no
monitoring_role_arnThe ARN for the IAM role that permits RDS to send enhanced monitoring metrics to CloudWatch Logs. Must be specified if monitoring_interval is non-zerostringnullno
monitoring_role_descriptionDescription of the monitoring IAM rolestringnullno
monitoring_role_nameName of the IAM role which will be created when create_monitoring_role is enabledstring"rds-monitoring-role"no
monitoring_role_permissions_boundaryARN of the policy that is used to set the permissions boundary for the monitoring IAM rolestringnullno
monitoring_role_use_name_prefixDetermines whether to use monitoring_role_name as is or create a unique identifier beginning with monitoring_role_name as the specified prefixboolfalseno
multi_azSpecifies if the RDS instance is multi-AZboolfalseno
nchar_character_set_nameThe national character set is used in the NCHAR, NVARCHAR2, and NCLOB data types for Oracle instances. This can't be changed.stringnullno
network_typeThe type of network stack to usestringnullno
option_group_descriptionThe description of the option groupstringnullno
option_group_nameName of the option groupstringnullno
option_group_skip_destroySet to true if you do not wish the option group to be deleted at destroy time, and instead just remove the option group from the Terraform stateboolnullno
option_group_timeoutsDefine maximum timeout for deletion of aws_db_option_group resource
object({
delete = optional(string)
})
nullno
option_group_use_name_prefixDetermines whether to use option_group_name as is or create a unique name beginning with the option_group_name as the prefixbooltrueno
optionsA list of Options to apply
list(object({
option_name = string
port = optional(number)
version = optional(string)
db_security_group_memberships = optional(list(string))
vpc_security_group_memberships = optional(list(string))
option_settings = optional(list(object({
name = string
value = string
})))
}))
nullno
parameter_group_descriptionDescription of the DB parameter group to createstringnullno
parameter_group_nameName of the DB parameter group to associate or createstringnullno
parameter_group_skip_destroySet to true if you do not wish the parameter group to be deleted at destroy time, and instead just remove the parameter group from the Terraform stateboolnullno
parameter_group_use_name_prefixDetermines whether to use parameter_group_name as is or create a unique name beginning with the parameter_group_name as the prefixbooltrueno
parametersA list of DB parameters (map) to apply
list(object({
name = string
value = string
apply_method = optional(string)
}))
nullno
password_woWrite-Only required unless manage_master_user_password is set to true, snapshot_identifier, or replicate_source_db is provided). Password for the master DB user. Note that this may show up in logs, and it will be stored in the state filestringnullno
password_wo_versionUsed together with password_wo to trigger an update. Increment this value when an update to password_wo is required.numbernullno
performance_insights_enabledSpecifies whether Performance Insights are enabledboolfalseno
performance_insights_kms_key_idThe ARN for the KMS key to encrypt Performance Insights datastringnullno
performance_insights_retention_periodThe amount of time in days to retain Performance Insights data. Valid values are 7, 731 (2 years) or a multiple of 31number7no
portThe port on which the DB accepts connectionsstringnullno
publicly_accessibleBool to control if instance is publicly accessibleboolfalseno
putin_khuyloDo you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo!booltrueno
regionRegion where this resource will be managed. Defaults to the Region set in the provider configurationstringnullno
replica_modeSpecifies whether the replica is in either mounted or open-read-only mode. This attribute is only supported by Oracle instances. Oracle replicas operate in open-read-only mode unless otherwise specifiedstringnullno
replicate_source_dbSpecifies that this resource is a Replicate database, and to use this value as the source database. This correlates to the identifier of another Amazon RDS Database to replicatestringnullno
restore_to_point_in_timeRestore to a point in time (MySQL is NOT supported)
object({
restore_time = optional(string)
source_db_instance_automated_backups_arn = optional(string)
source_db_instance_identifier = optional(string)
source_dbi_resource_id = optional(string)
use_latest_restorable_time = optional(bool)
})
nullno
s3_importRestore from a Percona Xtrabackup in S3 (only MySQL is supported)
object({
source_engine_version = string
bucket_name = string
bucket_prefix = optional(string)
ingestion_role = string
})
nullno
skip_final_snapshotDetermines whether a final DB snapshot is created before the DB instance is deleted. If true is specified, no DBSnapshot is created. If false is specified, a DB snapshot is created before the DB instance is deletedboolfalseno
snapshot_identifierSpecifies whether or not to create this database from a snapshot. This correlates to the snapshot ID you'd find in the RDS console, e.g: rds:production-2015-06-26-06-05stringnullno
storage_encryptedSpecifies whether the DB instance is encryptedbooltrueno
storage_throughputStorage throughput value for the DB instance. See notes for limitations regarding this variable for gp3numbernullno
storage_typeOne of 'standard' (magnetic), 'gp2' (general purpose SSD), 'gp3' (new generation of general purpose SSD), or 'io1' (provisioned IOPS SSD). The default is 'io1' if iops is specified, 'gp2' if not. If you specify 'io1' or 'gp3' , you must also include a value for the 'iops' parameterstringnullno
subnet_idsA list of VPC subnet IDslist(string)[]no
tagsA mapping of tags to assign to all resourcesmap(string){}no
timeoutsUpdated Terraform resource management timeouts. Applies to aws_db_instance in particular to permit resource management times
object({
create = optional(string)
update = optional(string)
delete = optional(string)
})
nullno
timezoneTime 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 informationstringnullno
upgrade_storage_configWhether to upgrade the storage file system configuration on the read replica. Can only be set with replicate_source_db.boolnullno
usernameUsername for the master DB userstringnullno
vpc_security_group_idsList of VPC security groups to associatelist(string)[]no

Outputs

NameDescription
db_instance_addressThe address of the RDS instance
db_instance_arnThe ARN of the RDS instance
db_instance_availability_zoneThe availability zone of the RDS instance
db_instance_ca_cert_identifierSpecifies the identifier of the CA certificate for the DB instance
db_instance_cloudwatch_log_groupsMap of CloudWatch log groups created and their attributes
db_instance_domainThe ID of the Directory Service Active Directory domain the instance is joined to
db_instance_domain_auth_secret_arnThe ARN for the Secrets Manager secret with the self managed Active Directory credentials for the user joining the domain
db_instance_domain_dns_ipsThe IPv4 DNS IP addresses of your primary and secondary self managed Active Directory domain controllers
db_instance_domain_fqdnThe fully qualified domain name (FQDN) of an self managed Active Directory domain
db_instance_domain_iam_role_nameThe name of the IAM role to be used when making API calls to the Directory Service
db_instance_domain_ouThe self managed Active Directory organizational unit for your DB instance to join
db_instance_endpointThe connection endpoint
db_instance_engineThe database engine
db_instance_engine_version_actualThe running version of the database
db_instance_hosted_zone_idThe canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record)
db_instance_identifierThe RDS instance identifier
db_instance_master_user_secret_arnThe ARN of the master user secret (Only available when manage_master_user_password is set to true)
db_instance_nameThe database name
db_instance_portThe database port
db_instance_resource_idThe RDS Resource ID of this instance
db_instance_role_associationsA map of DB Instance Identifiers and IAM Role ARNs separated by a comma
db_instance_secretsmanager_secret_rotation_enabledSpecifies whether automatic rotation is enabled for the secret
db_instance_statusThe RDS instance status
db_instance_upgrade_rollout_orderOrder in which the instances are upgraded (first, second, last)
db_instance_usernameThe master username for the database
db_listener_endpointSpecifies the listener connection endpoint for SQL Server Always On
db_option_group_arnThe ARN of the db option group
db_option_group_idThe db option group id
db_parameter_group_arnThe ARN of the db parameter group
db_parameter_group_idThe db parameter group id
db_subnet_group_arnThe ARN of the db subnet group
db_subnet_group_idThe db subnet group name
enhanced_monitoring_iam_role_arnThe Amazon Resource Name (ARN) specifying the monitoring role
enhanced_monitoring_iam_role_nameThe name of the monitoring role

Authors

Module is maintained by Anton Babenko with help from these awesome contributors.

License

Apache 2 Licensed. See LICENSE for full details.

Additional information for users from Russia and Belarus