skip to Main Content

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

2

Answers


  1. 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
    }
    
    
    Login or Signup to reply.
  2. Terraform probably tries to create the item in the table first (due to the parallelism), because there are no implicit dependencies between the table and the item. Implicit dependencies are created by referencing an attribute of a resource or an output of a module. The module does not provide an output which would be helpful. In this case, you might try using depends_on meta-argument so you tell terraform to create the table prior to trying to create the item. So for example, you would do the following:

    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" {
      depends_on = [module.dynamodb_table]
      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
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search