
I wanted to create an Amazon Aurora PostgreSQL database on AWS using Terraform. The goal was to automate the infrastructure deployment and connect to the database using pgAdmin or psql.
What initially seemed like a straightforward Terraform deployment turned into an interesting exercise because of the restrictions associated with the AWS Free Tier.
It’s actually very simple to create an Aurora instance on the free tier with Terraform. I have more info on Terraform here. My Terraform code is here.
The big gotcha is that I can create a new login role with the express configuration, but I can’t log in with it. For example, I don’t want to log in as postgres all the time. I would like to have my own login. That requires you to move your instance into a VPC, which requires you to connect with VPN or other options.
I wanted to keep this simple. So that means I have to get a token to use the instance, and it expires every 15 minutes or so. Not a huge restriction since my end goal is to test some scripts I’m creating with a PostgresCheck script library to run against your Postgres instances to check configurations and settings.
It’s easy enough to get pgAdmin to get a token refreshed under Advanced in the connection properties. Note: if you put in a password manually before this, you will need to Clear Saved Password, or pgAdmin will keep trying to use that and give an error.
pathtoawsonyourmachine/aws rds generate-db-auth-token --hostname yourhostname --port 5432 --region yourregionhere --username postgres 
Easy with psql, too.
RDS_HOST="your-cluster.cluster-abc123.us-east-1.rds.amazonaws.com"
DB_USER="yourdbuser"
DB_NAME="yourdb"
# Generate token
export PGPASSWORD="$(aws rds generate-db-auth-token
--hostname "$RDS_HOST"
--port 5432
--region us-east-1
--username "$DB_USER")"
# Now psql
psql "host=$RDS_HOST port=5432 dbname=$DB_NAME user=$DB_USER"
-f /path/to/your_script.sql
This isn’t a production-ready situation, but I wanted to test some things, learn some things, and it’s good for that.
I was destroying this instance regularly to save costs, but this is an express configuration, so it’s VERY inexpensive. As long as I disconnect any of my sessions, it auto-pauses after 5 minutes. It’s costing way less than $1/day, and I have $160 in free credits right now. I might not just let it sit there for ages on end, though.
References
- AWS Aurora PostgreSQL documentation: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/CHAP_AuroraPostgreSQL.html
https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/CHAP_GettingStartedAurora.AuroraPostgreSQL.ExpressConfig.html - Terraform AWS provider: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudcontrolapi_resource
The post Terraform Aurora PostgreSQL on Free Tier appeared first on sqlkitty.