# Terraform configuration to create an Amazon DynamoDB table.
# CRUD operations (Put, Get, Update, Delete Item) are typically performed by
# applications interacting with the table, not directly by Terraform.
# This example focuses on defining the table and optionally adding an initial item.

provider "aws" {
  region = "us-east-1"
}

# --- DynamoDB Table ---
resource "aws_dynamodb_table" "crud_table" {
  name             = "MyTerraformCRUDTable"
  billing_mode     = "PAY_PER_REQUEST"
  hash_key         = "Id"

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

  tags = {
    Name        = "MyTerraformCRUDTable"
    Environment = "Dev"
  }
}

# --- Optional: Add an initial item to the table ---
# This demonstrates how Terraform can manage initial data.
# For dynamic CRUD operations, an application would interact with the table.
resource "aws_dynamodb_table_item" "initial_item" {
  table_name = aws_dynamodb_table.crud_table.name
  hash_key   = aws_dynamodb_table.crud_table.hash_key

  item = jsonencode({
    Id          = { S = "item001" },
    Name        = { S = "Initial Item" },
    Description = { S = "This is an item created by Terraform." }
  })
}

# --- Outputs ---
output "dynamodb_table_name" {
  value       = aws_dynamodb_table.crud_table.name
  description = "The name of the DynamoDB table."
}

output "dynamodb_table_arn" {
  value       = aws_dynamodb_table.crud_table.arn
  description = "The ARN of the DynamoDB table."
}
