Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD dax module #13

Merged
merged 7 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This repo list some open to use Terraform modules we use at `Peak AI` because we
- [:camera: ECR](/ecr)
- [☸ K8S_IRSA](/k8s_irsa)
- [:open_file_folder: S3](/s3)
- [:oil_drum: Dax](/dax)
- [:bookmark: Tags (aka Labels)](/tags)

## Example usage
Expand Down
51 changes: 51 additions & 0 deletions dax/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| aws | >= 2.62 |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| item\_cache\_ttl\_milli\_second | Time after which item cache will invalidate. Default 5 minutes | `number` | `300000` | no |
| name | n/a | `string` | n/a | yes |
| node\_type | (Required) The compute and memory capacity of the nodes | `string` | `"dax.t2.small"` | no |
| query\_cache\_ttl\_\_milli\_second | Time after which item query will invalidate. Default 5 minutes | `number` | `300000` | no |
| region | n/a | `string` | n/a | yes |
| replication\_factor | (Required) The number of nodes in the DAX cluster | `number` | `3` | no |
| tables | List of tables for using dax | `list(string)` | n/a | yes |
| tags | A map of tags to assign to the resource | `any` | n/a | yes |
| vpc\_id | n/a | `string` | n/a | yes |
azhar22k marked this conversation as resolved.
Show resolved Hide resolved

## Outputs

| Name | Description |
|------|-------------|
| endpoint\_created | Endpoint of the created cluster. |
azhar22k marked this conversation as resolved.
Show resolved Hide resolved

## Example usage

```hcl
provider "aws" {
version = "~> 2.62"
}

module "tags" {
source = "git::https://github.com/peak-ai/terraform-modules.git//tags?ref=v0.7.0"
tenant = "new-client"
stage = "latest"
feature = "example"
service = "example"
}

module "my_dax" {
source = "git::https://github.com/peak-ai/terraform-modules.git//dax?ref=v0.7.0"
name = "dax"
azhar22k marked this conversation as resolved.
Show resolved Hide resolved
tags = module.tags.default
}
```
102 changes: 102 additions & 0 deletions dax/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
provider "aws" {
region = var.region
default_tags {
tags = var.tags
}
}

data "aws_caller_identity" "current" {}
resource "aws_iam_role" "role" {
name = "${var.name}-dax"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "dax.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

data "aws_subnet_ids" "vpc_subnets" {
vpc_id = var.vpc_id
tags = {
Network = "Private"
}
}

locals {
table_arns = [for table in var.tables : [
"arn:aws:dynamodb:${var.region}:${data.aws_caller_identity.current.account_id}:table/${table}",
"arn:aws:dynamodb:${var.region}:${data.aws_caller_identity.current.account_id}:table/${table}/*"
]]
}


data "aws_iam_policy_document" "document" {
statement {
effect = "Allow"
actions = [
"dynamodb:DescribeTable",
"dynamodb:PutItem",
"dynamodb:GetItem",
"dynamodb:UpdateItem",
"dynamodb:DeleteItem",
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:BatchGetItem",
"dynamodb:BatchWriteItem",
"dynamodb:ConditionCheckItem"
]
resources = flatten(local.table_arns)
}
}

resource "aws_iam_policy" "policy" {
name = "${var.name}-dax"
description = "Dax access policy for cluster ${var.name}"

policy = data.aws_iam_policy_document.document.json
}

resource "aws_iam_role_policy_attachment" "policy_attachment" {
role = aws_iam_role.role.name
policy_arn = aws_iam_policy.policy.arn
}

resource "aws_dax_parameter_group" "group" {
name = var.name

parameters {
name = "query-ttl-millis"
value = var.query_cache_ttl__milli_second
}

parameters {
name = "record-ttl-millis"
value = var.item_cache_ttl_milli_second
}
}
resource "aws_dax_subnet_group" "subnet_group" {
name = var.name
subnet_ids = data.aws_subnet_ids.vpc_subnets.ids
}

resource "aws_dax_cluster" "cluster" {
cluster_name = var.name
iam_role_arn = aws_iam_role.role.arn
node_type = var.node_type
replication_factor = var.replication_factor
parameter_group_name = aws_dax_parameter_group.group.name
subnet_group_name = aws_dax_subnet_group.subnet_group.id
server_side_encryption {
enabled = true
}
}
4 changes: 4 additions & 0 deletions dax/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "endpoint_created" {
value = aws_dax_cluster.cluster.cluster_address
description = "Endpoint of the DaX cluster."
}
42 changes: 42 additions & 0 deletions dax/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
variable "tags" {}

variable "region" {
type = string
}

variable "name" {
type = string
}

variable "node_type" {
type = string
default = "dax.t2.small"
description = "(Required) The compute and memory capacity of the nodes"
}

variable "replication_factor" {
type = number
default = 3
description = "(Required) The number of nodes in the DAX cluster"
}

variable "vpc_id" {
type = string
}

variable "tables" {
type = list(string)
description = "List of tables for using dax"
}

variable "item_cache_ttl_milli_second" {
type = number
default = 300000
description = "Time after which item cache will invalidate. Default 5 minutes"
}

variable "query_cache_ttl__milli_second" {
type = number
default = 300000
description = "Time after which query cache will invalidate. Default 5 minutes"
}