terraformterraform-provider-awsterraform-aws-modules

terraform plan giving Error: Unsupported argument in child module of AWS RDS


I am creating a two-tier architecture using Terraform and AWS modules. I have a Root folder and a child folder where I am getting this below error. I have declared a variable in Child folder and Root folder named as'db_name' and calling it out in rds.tf and main.tf files. But running terraform plan command is giving the below error:

Waiting for the plan to start...

Terraform v1.4.2 

on linux_amd64

Initializing plugins and modules...

│ Error: Unsupported argument
│ 
│   on child-module/rds.tf line 54, in resource "aws_db_instance" "db_instance":
│   54:   db_name                = var.db_name
│ 
│ An argument named "db_name" is not expected here.
╵
Operation failed: failed running terraform plan (exit 1) 

child_module Folder

rds.tf

.
.
resource "aws_db_instance" "db_instance" {
  allocated_storage      = var.db_allocated_storage
  engine                 = var.db_engine
  engine_version         = var.db_engine_version
  instance_class         = var.db_instance_class
  db_name                = var.db_name
  username               = var.db_username
  password               = var.db_password
  skip_final_snapshot    = true
  multi_az               = true
  db_subnet_group_name   = aws_db_subnet_group.db_subnet_group.name
  vpc_security_group_ids = [aws_security_group.db_security_group.id]

  tags = {
    Name        = "${var.env}-db-instance"
    Environment = var.env
  }
.
.

variables.tf

.
.

variable "db_name" {
  description = "The database name"
  type        = string
}
.
.

Root Folder:

main.tf

.
.
module "create_two_tier_aws" {
  source = "./child-module"

  env                       = var.env
  aws_region                = var.aws_region
  vpc_cidr_block            = "172.16.0.0/16"
  public_subnet_cidr_block  = ["172.16.0.0/24", "172.16.1.0/24"]
  private_subnet_cidr_block = ["172.16.10.0/24", "172.16.11.0/24"]

  ec2_name = var.ec2_name
  ssh_key  = var.ssh_key

  db_name     = var.db_name
  db_username = var.db_username
  db_password = var.db_password
}
.
.

variables.tf

.
.
variable "db_name" {
  description = "The database name"
  type        = string
  default     = "terraformdatabase1"
}
.
.

Can someone please provide your insight or suggestion on what am I missing here? Thank you for reading through this.

I checked and rechecked everywhere the variable db_name was defined and called out.


Solution

  • Attribute db_name depends on the Database Engine you are creating.

    As per the terraform resource aws_db_instance docs.

    Note that this does not apply to Oracle or SQL Server engines. See the AWS documentation for more details on what applies for those engines. If you are providing an Oracle db name, it needs to be in all upper case. Cannot be specified for a replica.

    For Amazon RDS Custom for SQL Server and SQL Server

    --db-name parameter is Not applicable and Must be null.

    Hence you should pass a default null value to your variable db_name in the child module to make it an optional attribute in the module and then can get rid of the attribute in your root module whenever you are creating either of Amazon RDS Custom for SQL Server or SQL Server database engines.

    variable "db_name" {
      description = "The database name"
      type        = string
      default     = null
    }
    
    module "create_two_tier_aws" {
      source = "./child-module"
    
      env                       = var.env
      aws_region                = var.aws_region
      vpc_cidr_block            = "172.16.0.0/16"
      public_subnet_cidr_block  = ["172.16.0.0/24", "172.16.1.0/24"]
      private_subnet_cidr_block = ["172.16.10.0/24", "172.16.11.0/24"]
    
      ec2_name = var.ec2_name
      ssh_key  = var.ssh_key
    
      db_username = var.db_username
      db_password = var.db_password
    }