Skip to content

nsbno/terraform-aws-relational-database

Repository files navigation

Relational Database Using Aurora

Set up a PostgreSQL or MySQL database using AWS Aurora with sane defaults.

Using this module, you don’t have to worry about backups or managing hosts.

About

Remember to check out the variables and outputs to see all options.

module "database" {
  source = "github.com/nsbno/terraform-aws-relational-database?ref=x.y.z"

  application_name = "tut-tut-tog"

  engine         = "postgresql"
  engine_version = "13"

  vpc_id             = "vpc-12345"
  subnet_ids         = ["subnet-1a2b3c", "subnet-4b5c6d", "subnet-7a8b9c"]
  availability_zones = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
  security_group_ids = ["sg-54321"]
}

This will give you everything you need to use your database. You don’t have to configure anything else unless you have specific requirements 🎉

Examples

These are some examples that can help you get going with your setup.

Simple Postgres

This example shows a bare-bones setup with only a database using postgress >=13,<14. In this example minor versions will be automatically updated by AWS, and has 1 writer node.

Considerations when creating a database cluster

You still have to make some decisions about your database, even though it’s managed. Here are some things to look out for.

Choosing Instance Types

The instance type you should choose is based on your workload. By default, a db.t4g.medium is used, which should be enough for your initial deployment (and maybe forever for smaller apps).

In short you must select the instance type that

  • allows for the amount of connections you actually need

  • has enough temporary storage to hold your queries and index builds in memory.

You can find out more about what the different instance types allow for in the management docs for Aurora.

When your application is in production, you can also monitor the impact of your instance type by checking the VolumeReadIOPS and BufferCacheHitRatio metrics. The Read IOPS should stay low and the buffer cache hit ratio should stay high. Values that don’t match this can indicate that you should upgrade to a larger instance type.

Picking versions for the database

The engine version constraint can take a few different forms. Either just a major version, or a major and minor version.

For most deployments, it is recommended to only specify the major version. This is because AWS will automatically upgrade minor versions for you by default.

You should disable automatic minor version upgrades if you need a specific minor version of your engine. This can be done by setting allow_minor_version_upgrade to false.