Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 93 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,120 @@
# DynamoDB table

[![Opstree Solutions][opstree_avatar]][opstree_homepage]<br/>[Opstree Solutions][opstree_homepage]

[opstree_homepage]: https://opstree.github.io/
[opstree_avatar]: https://img.cloudposse.com/200x100/https://www.opstree.com/images/og_image8.jpg

* A table is a collection of items, and each item is a collection of attributes.
* DynamoDB uses primary keys to uniquely identify each item in a table and secondary indexes to provide more querying flexibility.

## Providers

| Name | Version |
|---------------------------------------------------|----------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.82.2 |
| <a name="terraform_module"></a> [Terraform](Terraform\module) | >= 1.12.1|


## Architecture
<img width="1219" height="598" alt="Screenshot from 2025-07-16 14-20-19" src="https://github.com/user-attachments/assets/73ac5937-da6f-4e4e-b6f2-acc8fc42e8f0" />


___

## Usage


```hcl
module "dynamodb_table" {
source = "OT-CLOUD-KIT/dynamodb/aws"
environment = var.environment
count = var.enable_dynamodb_table_resource == true ? 1 : 0
table_name = var.table_name
region = var.region
billing_mode = var.billing_mode
read_capacity_main-table = var.read_capacity_main-table
write_capacity_main-table = var.write_capacity_main-table
hash_key = var.hash_key
range_key = var.range_key
stream_enabled = var.stream_enabled
stream_view_type = var.stream_view_type
ttl_enabled = var.ttl_enabled
ttl_attribute_name = var.ttl_attribute_name
point_in_time_recovery_enabled = var.point_in_time_recovery_enabled
attributes = var.attributes
local_secondary_indexes = var.local_secondary_indexes
global_secondary_indexes = var.global_secondary_indexes
tags = var.tags

source = "OT-CLOUD-KIT/terraform-aws-dynamodb"
count = 1
region = "us-east-1"
billing_mode = "PROVISIONED"
read_capacity_main_table = 2
write_capacity_main_table = 2
hash_key = "orderId"
range_key = "customerId"
stream_enabled = false
stream_view_type = "NEW_AND_OLD_IMAGES"
ttl_enabled = false
ttl_attribute_name = "ttl"
point_in_time_recovery_enabled = false

attributes = [
{
name = "orderId"
type = "S"
},
{
name = "customerId"
type = "S"
},
{
name = "shipped"
type = "S"
}
]

local_secondary_indexes = [
{
name = "lsi-orderId-customerId"
range_key = "customerId"
projection_type = "ALL"
non_key_attributes = []
}
]

global_secondary_indexes = [
{
name = "TitleIndex"
hash_key = "orderId"
range_key = "shipped"
projection_type = "ALL"
read_capacity = 5
write_capacity = 5
non_key_attributes = []
}
]
}

```
> **Note:**
> The above example demonstrates how to use the module. All variables, resources, and outputs used here are already defined within this module.

___

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| attributes | Additional attributes (e.g. `policy` or `role`) | `list(string)` | `"orderId" "customerId" "Shipped"` | yes |
| write_capacity_main-table | write capacity of mian table in the form of a number | `number` | `2` | yes |
| read_capacity_main-table| read capacity of main table in the form of a number | `number` | `2` | yes ||
| hash_key | DynamoDB table Hash Key in the form of a string | `string` | `orderId` | yes |
| hash_key_type | Hash Key type, which must be a scalar type: `S`, `N`, or `B` for (S)tring, (N)umber or (B)inary data | `string` | `S` | yes |
| billing_mode| billing mode required in the form of a string | `string` |`"PROVISIONED"`| no |
| table_name | table Name | `string` | `shipping` | yes |
| range_key | DynamoDB table Range Key | `string` | `customerId`| yes |
| range_key_type | Range Key type, which must be a scalar type: `S`, `N`, or `B` for (S)tring, (N)umber or (B)inary data | `string` | `S` | yes |
| stream_enabled | stream enabled, yes or no in the form of a bool | `bool` | `false` | yes |
| stream_view_type | stream view type in the form of a string | `string` | `"NEW_AND_OLD_IMAGES"` | yes |
| ttl_enabled | ttl enabled, yes or no in the form of a bool | `bool` | `false` | yes |
| ttl_attribute_name | ttl attribute name, in the form of a string | `string`| `"ttl"` | yes |
| point_in_time_recovery_enabled | point in time recovery enabled , yes or no in the form of a bool | `bool` | `false` | yes |
| local_secondary_indexes | local secondary indexes in the form of any | `any` | `name = "lsi-orderId-customerId"`, <br> `range_key = "customerId"`, <br> `projection_type = "ALL"`, <br> `non_key_attributes = []`| yes |
| global_secondary_indexes | global secondary indexes in the form of any | `any` | `name = "TitleIndex"`,<br> `hash_key = "orderId"`,<br> `range_key = "shipped"`,<br> `projection_type = "ALL"`,<br> `read_capacity = 5`,<br> `write_capacity = 5`,<br> `non_key_attributes = []`| yes |
| environment | environment required in the form of a string| `string` | `"dev"` | yes |
| tags | Additional tags (e.g. map(`BusinessUnit`,`XYZ`) | `map(string)` | `<map>` | yes |
|------|-------------|------|---------|:--------:|
| <a name="input_attributes"></a> [attributes](#input_attributes) | List of attributes for the table | `list(object)` | `[]` | yes |
| <a name="input_write_capacity_main_table"></a> [write_capacity_main_table](#input_write_capacity_main_table) | Write capacity of main table | `number` | `2` | yes |
| <a name="input_read_capacity_main_table"></a> [read_capacity_main_table](#input_read_capacity_main_table) | Read capacity of main table | `number` | `2` | yes |
| <a name="input_hash_key"></a> [hash_key](#input_hash_key) | DynamoDB hash key name | `string` | `"orderId"` | yes |
| <a name="input_hash_key_type"></a> [hash_key_type](#input_hash_key_type) | Hash key type (`S`, `N`, or `B`) | `string` | `"S"` | yes |
| <a name="input_billing_mode"></a> [billing_mode](#input_billing_mode) | Billing mode (`PROVISIONED` or `PAY_PER_REQUEST`) | `string` | `"PROVISIONED"` | no |
| <a name="input_table_name"></a> [table_name](#input_table_name) | Name of the DynamoDB table | `string` | `"shipping"` | yes |
| <a name="input_range_key"></a> [range_key](#input_range_key) | DynamoDB range key name | `string` | `"customerId"` | yes |
| <a name="input_range_key_type"></a> [range_key_type](#input_range_key_type) | Range key type (`S`, `N`, or `B`) | `string` | `"S"` | yes |
| <a name="input_stream_enabled"></a> [stream_enabled](#input_stream_enabled) | Whether to enable streams | `bool` | `false` | yes |
| <a name="input_stream_view_type"></a> [stream_view_type](#input_stream_view_type) | Stream view type | `string` | `"NEW_AND_OLD_IMAGES"` | yes |
| <a name="input_ttl_enabled"></a> [ttl_enabled](#input_ttl_enabled) | Enable TTL (Time to Live) | `bool` | `false` | yes |
| <a name="input_ttl_attribute_name"></a> [ttl_attribute_name](#input_ttl_attribute_name) | TTL attribute name | `string` | `"ttl"` | yes |
| <a name="input_point_in_time_recovery_enabled"></a> [point_in_time_recovery_enabled](#input_point_in_time_recovery_enabled) | PITR enabled | `bool` | `false` | yes |
| <a name="input_local_secondary_indexes"></a> [local_secondary_indexes](#input_local_secondary_indexes) | Local Secondary Indexes | `any` | `[...]` | yes |
| <a name="input_global_secondary_indexes"></a> [global_secondary_indexes](#input_global_secondary_indexes) | Global Secondary Indexes | `any` | `[...]` | yes |
| <a name="input_environment"></a> [environment](#input_environment) | Environment label (e.g., `dev`) | `string` | `"dev"` | yes |
| <a name="input_tags"></a> [tags](#input_tags) | Tags for resources | `map(string)` | `{}` | yes |

## Outputs

These defined outputs that can be used within the same service and terraform release.
## Outputs

| Name | Description |
|------|-------------|
| table_name | name of the model Dynamodb Table created |
| main-table-arn | arn of the model Dynamodb Table created |
| main-table-id | id of the model Dynamodb Table created |
| <a name="output_table_name"></a> [table_name](#output_table_name) | Name of the DynamoDB table created |
| <a name="output_main_table_arn"></a> [main_table_arn](#output_main_table_arn) | ARN of the DynamoDB table |
| <a name="output_main_table_id"></a> [main_table_id](#output_main_table_id) | ID of the DynamoDB table |

## Contributors

![Screenshot from 2022-08-31 11-15-32](https://user-images.githubusercontent.com/98826875/187603118-a9bf0285-01f9-409e-9752-2fec11bc551b.png)


[Shristi Gupta][shristi_homepage]
## Contributors

[shristi_homepage]: https://gitlab.com/shristi.gupta
- [Piyush Upadhyay](https://github.com/piiiyuushh)
- [Nikita Joshi](https://github.com/jnikita19)


8 changes: 8 additions & 0 deletions example/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
backend "s3" {
bucket = "ot-cloud-kit-bucket"
key = "ot/module/DynomoDB/terraform.tfstate"
region = "us-east-1"

}
}
8 changes: 5 additions & 3 deletions example/main.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@

module "dynamodb_table" {
source = "git::https://github.com/OT-CLOUD-KIT/terraform-aws-dynamodb.git"
source = "git@github.com:OT-CLOUD-KIT/terraform-aws-dynamodb.git?ref=Feature"
environment = var.environment
count = var.enable_dynamodb_table_resource == true ? 1 : 0
table_name = var.table_name
region = var.region
billing_mode = var.billing_mode
read_capacity_main-table = var.read_capacity_main-table
write_capacity_main-table = var.write_capacity_main-table
hash_key = var.hash_key
range_key = var.range_key
env = var.env
app = var.app
owner = var.owner
stream_enabled = var.stream_enabled
stream_view_type = var.stream_view_type
ttl_enabled = var.ttl_enabled
Expand All @@ -17,5 +20,4 @@ module "dynamodb_table" {
attributes = var.attributes
local_secondary_indexes = var.local_secondary_indexes
global_secondary_indexes = var.global_secondary_indexes
tags = var.tags
}
14 changes: 14 additions & 0 deletions example/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "main_table_id" {
description = "The ID of the DynamoDB table"
value = lookup(module.dynamodb_table[0], "main-table-id", null)
}

output "main_table_arn" {
description = "The ARN of the DynamoDB table"
value = lookup(module.dynamodb_table[0], "main-table-arn", null)
}

output "table_name" {
description = "The name of the DynamoDB table"
value = lookup(module.dynamodb_table[0], "table-name", null)
}
4 changes: 4 additions & 0 deletions example/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provider "aws" {
region = var.region

}
13 changes: 8 additions & 5 deletions example/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
environment = "dev"
enable_dynamodb_table_resource = true
table_name = "shri"
region = "us-east-1"
billing_mode = "PROVISIONED"
read_capacity_main-table = 2
Expand Down Expand Up @@ -47,7 +46,11 @@ global_secondary_indexes = [
non_key_attributes = []
}
]
tags = {
"team" : "devops"
"service" : "dynamodb"
}


################# Naming Convension #####################

env = "dev"
owner = "opstree"
app = "otcloud-kit"

28 changes: 20 additions & 8 deletions example/variable.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ variable "environment" {
description = "The environment for the infrastrcutrue (dev)"
}

variable "table_name" {
type = string
description = "The name of the table"
}

variable "region" {
type = string
Expand Down Expand Up @@ -63,10 +59,7 @@ variable "point_in_time_recovery_enabled" {
description = "point in time recovery enabled"
}

variable "tags" {
type = map(string)
description = "(Required) tags"
}


variable "attributes" {
type = list(map(string))
Expand All @@ -86,4 +79,23 @@ variable "local_secondary_indexes" {
variable "enable_dynamodb_table_resource" {
type = bool
description = "need to create dynamodb resource yes or no"
}

################################# Naming Convention Variables #########################################

variable "env" {
type = string
default = "dev"

}

variable "owner" {
type = string
default = "opstree"
}

variable "app" {
type = string
default = "otcloud-kit"

}
9 changes: 9 additions & 0 deletions local.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
locals {
base_name = "${var.env}-${var.app}"

common_tags = {
env = var.env
owner = var.owner
app = var.app
}
}
10 changes: 5 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

resource "aws_dynamodb_table" "main-table" {
name = var.table_name
resource "aws_dynamodb_table" "main_table" {
name = "${local.base_name}-dynamodb-table"
billing_mode = var.billing_mode
read_capacity = var.read_capacity_main-table
write_capacity = var.write_capacity_main-table
Expand Down Expand Up @@ -50,11 +50,11 @@ resource "aws_dynamodb_table" "main-table" {
}
}


tags = merge(
{
Name = var.table_name
environment = var.environment
Name = "${local.base_name}-dynamodb-table"
},
var.tags,
local.common_tags,
)
}
6 changes: 3 additions & 3 deletions output.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
output "main-table-id" {
value = aws_dynamodb_table.main-table.id
value = aws_dynamodb_table.main_table.id
}

output "main-table-arn" {
value = aws_dynamodb_table.main-table.arn
value = aws_dynamodb_table.main_table.arn
}

output "table-name" {
value = aws_dynamodb_table.main-table.name
value = aws_dynamodb_table.main_table.name
}

Loading