Skip to content

Commit

Permalink
Use terraform-aws-dynamodb-autoscaler module (#5)
Browse files Browse the repository at this point in the history
* Use `terraform-aws-dynamodb-autoscaler` module

* Add `enable_autoscaler` variable
  • Loading branch information
aknysh authored Mar 24, 2018
1 parent 5ec97dc commit 45cb3dc
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 160 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ install:
- make init

script:
- make terraform:install
- make terraform:get-plugins
- make terraform:get-modules
- make terraform:lint
- make terraform:validate
- make terraform/install
- make terraform/get-plugins
- make terraform/get-modules
- make terraform/lint
- make terraform/validate
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ SHELL := /bin/bash
-include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness)

lint:
$(SELF) terraform:install terraform:get-modules terraform:get-plugins terraform:lint terraform:validate
$(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
# terraform-aws-dynamodb [![Build Status](https://travis-ci.org/cloudposse/terraform-aws-dynamodb.svg?branch=master)](https://travis-ci.org/cloudposse/terraform-aws-dynamodb)

Terraform Module to provision a DynamoDB table with auto-scaling.
Terraform module to provision a DynamoDB table with autoscaling.

Autoscaler scales up/down the provisioned OPS for the DynamoDB table based on the load.


## Usage

```hcl
module "dynamodb_table" {
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb.git?ref=master"
namespace = "cp"
stage = "dev"
name = "cluster"
hash_key = "HashKey"
range_key = "RangeKey"
autoscale_write_target = 10
autoscale_read_target = 10
autoscale_min_read_capacity = 5
autoscale_max_read_capacity = 20
autoscale_min_write_capacity = 5
autoscale_max_write_capacity = 20
enable_autoscaler = "true"
}
```


## Variables

| Name | Default | Description | Required |
|:--------------------------------|:-------------|:-------------------------------------------------------------------------------|:--------:|
| `namespace` | `` | Namespace (_e.g._ `cp` or `cloudposse`) | Yes |
| `stage` | `` | Stage (_e.g._ `prod`, `dev`, `staging`) | Yes |
| `name` | `` | Name (_e.g._ `app` or `cluster`) | Yes |
| `hash_key` | `` | DynamoDB table Hash Key | Yes |
| `range_key` | `` | DynamoDB table Range Key | Yes |
| `ttl_attribute` | `` | DynamoDB table TTL attribute | No |
| `enable_encryption` | `true` | Enable DynamoDB server-side encryption | No |
| `attributes` | `[]` | Additional attributes (_e.g._ `policy` or `role`) | No |
| `tags` | `{}` | Additional tags (_e.g._ `map("BusinessUnit","XYZ")` | No |
| `delimiter` | `-` | Delimiter to be used between `namespace`, `stage`, `name`, and `attributes` | No |
| `autoscale_write_target` | `10` | The target value for DynamoDB write autoscaling | No |
| `autoscale_read_target` | `10` | The target value for DynamoDB read autoscaling | No |
| `autoscale_min_read_capacity` | `5` | DynamoDB autoscaling min read capacity | No |
| `autoscale_max_read_capacity` | `20` | DynamoDB autoscaling max read capacity | No |
| `autoscale_min_write_capacity` | `5` | DynamoDB autoscaling min write capacity | No |
| `autoscale_max_write_capacity` | `20` | DynamoDB autoscaling max write capacity | No |
| `enable_autoscaler` | `true` | Flag to enable/disable DynamoDB autoscaling | No |


## Outputs

| Name | Description |
|:----------------|:-----------------------------|
| `table_id` | DynamoDB table ID |
| `table_arn` | DynamoDB table ARN |
| `table_name` | DynamoDB table name |


## Help
Expand Down
140 changes: 21 additions & 119 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
data "aws_caller_identity" "current" {}

module "default" {
module "dynamodb_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3"
namespace = "${var.namespace}"
stage = "${var.stage}"
Expand All @@ -11,7 +9,7 @@ module "default" {
}

resource "aws_dynamodb_table" "default" {
name = "${module.default.id}"
name = "${module.dynamodb_label.id}"
read_capacity = "${var.autoscale_min_read_capacity}"
write_capacity = "${var.autoscale_min_write_capacity}"
hash_key = "${var.hash_key}"
Expand Down Expand Up @@ -40,119 +38,23 @@ resource "aws_dynamodb_table" "default" {
enabled = true
}

tags = "${module.default.tags}"
}

// Autoscaler scales up/down the provisioned ops for DynamoDB table based on the load
data "aws_iam_policy_document" "assume_role" {
statement {
sid = ""

actions = [
"sts:AssumeRole",
]

principals {
type = "Service"
identifiers = ["application-autoscaling.amazonaws.com"]
}

effect = "Allow"
}
}

resource "aws_iam_role" "autoscaler" {
name = "${module.default.id}-autoscaler"
assume_role_policy = "${data.aws_iam_policy_document.assume_role.json}"
}

data "aws_iam_policy_document" "autoscaler" {
statement {
sid = ""

actions = [
"dynamodb:DescribeTable",
"dynamodb:UpdateTable",
]

resources = ["${aws_dynamodb_table.default.arn}"]

effect = "Allow"
}
}

resource "aws_iam_role_policy" "autoscaler" {
name = "${module.default.id}-autoscaler-dynamodb"
role = "${aws_iam_role.autoscaler.id}"
policy = "${data.aws_iam_policy_document.autoscaler.json}"
}

data "aws_iam_policy_document" "autoscaler_cloudwatch" {
statement {
sid = ""

actions = [
"cloudwatch:PutMetricAlarm",
"cloudwatch:DescribeAlarms",
"cloudwatch:DeleteAlarms",
]

resources = ["*"]

effect = "Allow"
}
}

resource "aws_iam_role_policy" "autoscaler_cloudwatch" {
name = "${module.default.id}-autoscaler-cloudwatch"
role = "${aws_iam_role.autoscaler.id}"
policy = "${data.aws_iam_policy_document.autoscaler_cloudwatch.json}"
}

resource "aws_appautoscaling_target" "read_target" {
max_capacity = "${var.autoscale_max_read_capacity}"
min_capacity = "${var.autoscale_min_read_capacity}"
resource_id = "table/${aws_dynamodb_table.default.name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}

resource "aws_appautoscaling_policy" "read_policy" {
name = "DynamoDBReadCapacityUtilization:${aws_appautoscaling_target.read_target.resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = "${aws_appautoscaling_target.read_target.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.read_target.scalable_dimension}"
service_namespace = "${aws_appautoscaling_target.read_target.service_namespace}"

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}

target_value = "${var.autoscale_read_target}"
}
}

resource "aws_appautoscaling_target" "write_target" {
max_capacity = "${var.autoscale_max_write_capacity}"
min_capacity = "${var.autoscale_min_write_capacity}"
resource_id = "table/${aws_dynamodb_table.default.name}"
scalable_dimension = "dynamodb:table:WriteCapacityUnits"
service_namespace = "dynamodb"
}

resource "aws_appautoscaling_policy" "write_policy" {
name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.write_target.resource_id}"
policy_type = "TargetTrackingScaling"
resource_id = "${aws_appautoscaling_target.write_target.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.write_target.scalable_dimension}"
service_namespace = "${aws_appautoscaling_target.write_target.service_namespace}"

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}

target_value = "${var.autoscale_write_target}"
}
tags = "${module.dynamodb_label.tags}"
}

module "dynamodb_autoscaler" {
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb-autoscaler.git?ref=tags/0.1.1"
enabled = "${var.enable_autoscaler}"
namespace = "${var.namespace}"
stage = "${var.stage}"
name = "${var.name}"
delimiter = "${var.delimiter}"
attributes = "${var.attributes}"
dynamodb_table_name = "${aws_dynamodb_table.default.id}"
dynamodb_table_arn = "${aws_dynamodb_table.default.arn}"
autoscale_write_target = "${var.autoscale_write_target}"
autoscale_read_target = "${var.autoscale_read_target}"
autoscale_min_read_capacity = "${var.autoscale_min_read_capacity}"
autoscale_max_read_capacity = "${var.autoscale_max_read_capacity}"
autoscale_min_write_capacity = "${var.autoscale_min_write_capacity}"
autoscale_max_write_capacity = "${var.autoscale_max_write_capacity}"
}
91 changes: 57 additions & 34 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,67 +1,90 @@
variable "name" {
type = "string"
}

variable "namespace" {
type = "string"
type = "string"
description = "Namespace (e.g. `cp` or `cloudposse`)"
}

variable "stage" {
type = "string"
type = "string"
description = "Stage (e.g. `prod`, `dev`, `staging`, `infra`)"
}

variable "enable_encryption" {
default = "true"
variable "name" {
type = "string"
description = "Name (e.g. `app` or `cluster`)"
}

variable "delimiter" {
type = "string"
default = "-"
description = "Delimiter to be used between `namespace`, `stage`, `name`, and `attributes`"
}

variable "attributes" {
type = "list"
default = []
type = "list"
default = []
description = "Additional attributes (e.g. `policy` or `role`)"
}

variable "tags" {
type = "map"
default = {}
type = "map"
default = {}
description = "Additional tags (e.g. map('BusinessUnit`,`XYZ`)"
}

variable "delimiter" {
type = "string"
default = "-"
variable "autoscale_write_target" {
default = 10
description = "The target value for DynamoDB write autoscaling"
}

variable "hash_key" {
type = "string"
variable "autoscale_read_target" {
default = 10
description = "The target value for DynamoDB read autoscaling"
}

variable "range_key" {
type = "string"
variable "autoscale_min_read_capacity" {
default = 5
description = "DynamoDB autoscaling min read capacity"
}

variable "ttl_attribute" {
type = "string"
default = "Expires"
variable "autoscale_max_read_capacity" {
default = 20
description = "DynamoDB autoscaling max read capacity"
}

variable "autoscale_write_target" {
default = 50
variable "autoscale_min_write_capacity" {
default = 5
description = "DynamoDB autoscaling min write capacity"
}

variable "autoscale_read_target" {
default = 50
variable "autoscale_max_write_capacity" {
default = 20
description = "DynamoDB autoscaling max write capacity"
}

variable "autoscale_min_read_capacity" {
default = 10
variable "enable_encryption" {
type = "string"
default = "true"
description = "Enable DynamoDB server-side encryption"
}

variable "autoscale_max_read_capacity" {
default = 100
variable "hash_key" {
type = "string"
description = "DynamoDB table Hash Key"
}

variable "autoscale_min_write_capacity" {
default = 10
variable "range_key" {
type = "string"
description = "DynamoDB table Range Key"
}

variable "autoscale_max_write_capacity" {
default = 100
variable "ttl_attribute" {
type = "string"
default = "Expires"
description = "DynamoDB table TTL attribute"
}

variable "enable_autoscaler" {
type = "string"
default = "true"
description = "Flag to enable/disable DynamoDB autoscaling"
}

0 comments on commit 45cb3dc

Please sign in to comment.