Xanagandr
Xanagandr

Reputation: 763

Create a SQL Server database while deploying an RDS instance using Terraform

I'm using Terraform with the AWS provider (v3.37.0) to create an RDS instance:

resource "aws_db_instance" "rds" {
  
  identifier     = "my_db"
  engine         = "sqlserver-se"
  engine_version = "14.00.3401.7.v1"

  name           = null
  username       = "${somewhere/in/ssm}"
  password       = "${somewhere/in/ssm}"

}

I wish to create a database alongside this RDS instance, so the documentation (https://registry.terraform.io/providers/hashicorp/aws/3.37.0/docs/resources/db_instance) states that I need to assign a value to the variable name, which I cannot, since the engine that I'm using (SQL Server) doesn't support that.

SQL Server not supported for out-of-the-box database creation

Is there another way to create that database inside this instance within Terraform?

I was thinking of using the local-exec provisioner or a community provider, but:

resource "aws_db_instance" "rds" {

  identifier     = "my_db"
  engine         = "sqlserver-se"
  engine_version = "14.00.3401.7.v1"

  name           = null
  username       = "${somewhere/in/ssm}"
  password       = "${somewhere/in/ssm}"

  provisioner "local-exec" {
    command = "USE master; CREATE DATABASE db-name"
  }

}

Upvotes: 0

Views: 1208

Answers (1)

Marcin
Marcin

Reputation: 238687

I don't know what I would put in the command

You need to use full mssql-cli command (or any other programming interface to mssql) which will execute on your computer and create your database. This means you have to have it installed first. For example, you can develop a python script which will connect and create your database. Then you invoke the python script from your local-exec.

Upvotes: 0

Related Questions