Terraform Level 3 Task 1: Managing Scalable NoSQL Databases with Amazon DynamoDB Using Terraform

I received this error: Hardcoded table name found in main.tf — should use variable

I believe this error is wrong. Please tell me if there is anything wrong with my solution below:

main.tf:
resource “aws_dynamodb_table” “xfusion-tasks” {
name = var.KKE_TABLE_NAME
billing_mode = “PAY_PER_REQUEST”
hash_key = “taskId”

attribute {
name = “taskId”
type = “S”
}

tags = {
Name = var.KKE_TABLE_NAME
}
}

resource “aws_dynamodb_table_item” “task_1” {
table_name = aws_dynamodb_table.xfusion-tasks.name
hash_key = aws_dynamodb_table.xfusion-tasks.hash_key

item = jsonencode({
taskId = { S = “1” }
description = { S = “Learn DynamoDB” }
status = { S = “completed” }
})

depends_on = [aws_dynamodb_table.xfusion-tasks]
}

resource “aws_dynamodb_table_item” “task_2” {
table_name = aws_dynamodb_table.xfusion-tasks.name
hash_key = aws_dynamodb_table.xfusion-tasks.hash_key

item = jsonencode({
taskId = { S = “2” }
description = { S = “Build To-Do App” }
status = { S = “in-progress” }
})

depends_on = [aws_dynamodb_table.xfusion-tasks]
}

variables.tf:
variable “KKE_TABLE_NAME” {
type = string
}

terraform.tfvars:
KKE_TABLE_NAME = “xfusion-tasks”

outputs.tf:
output “kke_dynamodb_table_name” {
value = aws_dynamodb_table.xfusion-tasks.name
}

Can you please paste this again using a code block? This will make it easier to analyze your code and try it out – pasting it as you’ve done corrupts the code by doing character substitution.

Sure, here’s the code block version:

main.tf:

resource "aws_dynamodb_table" "xfusion-tasks" {
  name         = var.KKE_TABLE_NAME
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "taskId"

  attribute {
    name = "taskId"
    type = "S"
  }

  tags = {
    Name = var.KKE_TABLE_NAME
  }
}

resource "aws_dynamodb_table_item" "task_1" {
  table_name = aws_dynamodb_table.xfusion-tasks.name
  hash_key   = aws_dynamodb_table.xfusion-tasks.hash_key

  item = jsonencode({
    taskId      = { S = "1" }
    description = { S = "Learn DynamoDB" }
    status      = { S = "completed" }
  })

  depends_on = [aws_dynamodb_table.xfusion-tasks]
}

resource "aws_dynamodb_table_item" "task_2" {
  table_name = aws_dynamodb_table.xfusion-tasks.name
  hash_key   = aws_dynamodb_table.xfusion-tasks.hash_key

  item = jsonencode({
    taskId      = { S = "2" }
    description = { S = "Build To-Do App" }
    status      = { S = "in-progress" }
  })

  depends_on = [aws_dynamodb_table.xfusion-tasks]
}

variables.tf:

variable "KKE_TABLE_NAME" {
  type = string
}

terraform.tfvars:

KKE_TABLE_NAME = "xfusion-tasks"

outputs.tf:

output "kke_dynamodb_table_name" {
  value = aws_dynamodb_table.xfusion-tasks.name
}

Thanks for that. What was the error you got from the grader?

Hardcoded table name found in main.tf — should use variable

I’m not getting too much traction here either. To test a theory, I did the following for main.tf, although everything else except main.tf, and outputs.tf (which will depend on my resource names):

main.tf:

resource "aws_dynamodb_table" "basic_dynamodb_table" {
  name           = var.KKE_TABLE_NAME
  read_capacity  = 20
  write_capacity = 20

  hash_key       = "taskId"

  attribute {
    name = "taskId"
    type = "S"
  }


}

resource "aws_dynamodb_table_item" "example1" {
  table_name = aws_dynamodb_table.basic_dynamodb_table.name
  hash_key   = aws_dynamodb_table.basic_dynamodb_table.hash_key

  item = <<ITEM
{
  "taskId": {"S": "1"},
  "description": {"S": "Learn DynamoDB"},
  "status": {"S": "in-progress"}
}
ITEM
}

resource "aws_dynamodb_table_item" "example2" {
  table_name = aws_dynamodb_table.basic_dynamodb_table.name
  hash_key   = aws_dynamodb_table.basic_dynamodb_table.hash_key

  item = <<ITEM2
{
  "taskId": {"S": "2"},
  "description": {"S": "Build To-Do App"},
  "status": {"S": "completed"}
}
ITEM2
}

This parses, but note that I do not use the table name as a resource name. My theory is that the grader script (mistakenly) scans main.tf, and gets upset, hence your error.

which leaves outputs.tf as

output "kke_dynamodb_table_name" {
  value = aws_dynamodb_table.basic_dynamodb_table.name
}

This did not get your error. It still failed – it claims I got the status of Task 1 wrong.

I’m going to write this up for engineering, with your version of the resource as the sample, to find out what the issue is with the grader.

1 Like

I’m told this has now been fixed. If you have a chance, please give it a try. Thanks for reporting this.