I have some terraform code that will create the dynamodb table and inserts one item into that table. When I run terraform apply
I am getting below error. It seems it going to insert the item before the dynamodb table gets complete created (status - Active).
module "dynamodb_table" {
source = "terraform-aws-modules/dynamodb-table/aws"
name = "${var.aws.region}-${var.config.dynamodb.table_name}"
hash_key = var.config.dynamodb.hash_key
attributes = [
{
name = var.config.dynamodb.hash_key
type = "S"
}
]
billing_mode = "PAY_PER_REQUEST"
table_class = "STANDARD"
}
resource "random_string" "random" {
length = 16
special = false
}
resource "aws_dynamodb_table_item" "table_item" {
table_name = "${var.aws.region}-${var.config.dynamodb.table_name}"
hash_key = var.config.dynamodb.hash_key
item = <<ITEM
{
"${var.config.dynamodb.hash_key}": {"S": "${random_string.random.result}"},
"collections": {"M": {}}
}
ITEM
}
Error message:
Error: creating DynamoDB Table Item: ResourceNotFoundException: Requested resource not found
To wait for the table to exist, using Terraform depends_on
should be enough to suffice:
module "dynamodb_table" {
source = "terraform-aws-modules/dynamodb-table/aws"
name = "${var.aws.region}-${var.config.dynamodb.table_name}"
hash_key = var.config.dynamodb.hash_key
attributes = [
{
name = var.config.dynamodb.hash_key
type = "S"
}
]
billing_mode = "PAY_PER_REQUEST"
table_class = "STANDARD"
}
resource "random_string" "random" {
length = 16
special = false
}
resource "aws_dynamodb_table_item" "table_item" {
table_name = "${var.aws.region}-${var.config.dynamodb.table_name}"
depends_on = [module.dynamodb_table]
hash_key = var.config.dynamodb.hash_key
item = <<ITEM
{
"${var.config.dynamodb.hash_key}": {"S": "${random_string.random.result}"},
"collections": {"M": {}}
}
ITEM
}