It's been a long while since my last blog but happy to say I'm back creating some hopefully good blog content that you will all enjoy. One thing I'm looking at more in depth at the moment is terraform- you may recall a long while ago I made a blog post that details getting started with terraform and deploying a simple s3 bucket. Well, now I'm looking into using terraform for all components of MS SQL Server RDS.
My hope is to create a little series which shows you individual components for RDS and then at the end we can combine these all together within terraform to make the best terraform RDS instance you've ever seen :-).
One thing to note, I am not an expert in terraform by any means, in fact I'm pretty much a novice. Hopefully though you'll see as these blogs progress we'll learn some cool tips and tricks. I'll be pretty much teaching myself as we go along!
Database Subnet Groups
A database subnet is basically just a collection of subnets that we can create within a VPC which we can then designate to our RDS database instance. Subnet Groups are optional within terraform- if you don't specify one it will just create your RDS instance in your default VPC.
How it works in terraform
Well if we wanted to spin up a simple RDS subnet example- you can take an example from the terraform page which looks something like the below adding your relevant subnet ids:
resource "aws_db_subnet_group" "mytestgroup" {
name = "main"
subnet_ids = ["subnetid"]
tags = {
Name = "My DB subnet group"
}
The code above will work perfectly fine, but what I wanted to offer a repeatable style of code where we can spin up subnet groups and not have the hassle of trying to find out the subnet id. For this example I want to be able to spin up a resource in an account based on the subnetids of a VPC named 'Main'
To do this i created two files
Variables file
Subnet file
Variable file:
We can specify variables with default values (as seen below) which can be over ridden when running terraform apply by specifying the variable name.
We can also use data sources (ref: data) to obtain information you require for specific data sources eg from the below: we want to find vpc information and then subnet id information. We can then use the VPC information to filter the subnets using the VPC ID.
variable "environmentname" {
description = "An environment name that is prefixed to resource names"
type = string
default = "dev"
}
variable "servicename" {
description = "An service name that is used in resource names and tags"
type = string
default = "amazingdb"
}
data "aws_vpc" "selected" {
filter {
name = "tag:Name"
values = ["Main"] #finds vpc based on name eg VPC named main
}
}
data "aws_subnet_ids" "list_subnet_ids" {
vpc_id = "${data.aws_vpc.selected.id}"
}
data "aws_subnet" "list_subnet" {
count = "${length(data.aws_subnet_ids.list_subnet_ids.ids)}"
id = "${tolist(data.aws_subnet_ids.list_subnet_ids.ids)[count.index]}"
}
Subnet File
the below code will make use of environment name, service name and also automatically obtain a list of subnet ids from the variables file.
resource "aws_db_subnet_group" "default_rds_mssql" {
name = "${var.environmentname}-${var.servicename}-rds-mssql-subnet-group"
description = "The ${var.environmentname} ${var.servicename} rds-mssql private subnet group."
subnet_ids = "${data.aws_subnet.list_subnet.*.id}"
}
Running in terraform
When running terraform plan and specifying the variable service name as myawesomedb you can begin to see how it has applied variables and automatically obtained the relevant subnet ids required.
![](https://static.wixstatic.com/media/7a082c_c4032c74f33e4fad9b3e9bf6c6996d0d~mv2.png/v1/fill/w_980,h_359,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/7a082c_c4032c74f33e4fad9b3e9bf6c6996d0d~mv2.png)
Once your happy you just need to run the same command but using terraform apply instead of terraform plan to deploy your infrastructure!
Comments