Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
dynamodb with multi-region support
null-label for standardized labels and tags.
  • Loading branch information
Dave Barnum committed Nov 18, 2019
0 parents commit b457fa6
Show file tree
Hide file tree
Showing 19 changed files with 1,804 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Wycliffe USA

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Terraform Modules
This repository contains Terraform modules that implement conventions we use
on AWS and other providers. You're welcome to use them, however be aware
that they may not be the best available or suit your needs exactly right. We
have opinions and conventions we like to follow and these modules follow them.

## Documentation
Each module provides its own documentation, so browse the source to read more
about each.

## MIT License
Copyright (c) 2019 Wycliffe USA

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions aws/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Amazon Web Services Provider
The modules in this directory are all specifically for use with Amazon Web
Services.

## Provider Configuration
In your application / Terraform configuration you need to define/configure
the `aws` provider like so:

```
provider "aws" {
access_key = "${var.YOUR_ACCESS_KEY_VARNAME}"
secret_key = "${var.YOUR_SECRET_KEY_VARNAME}"
region = "${var.REGION_NAME}"
}
```

Original code snippets from https://www.terraform.io/docs/providers/aws/r/dynamodb_global_table.html and https://github.com/cloudposse/terraform-aws-dynamodb
74 changes: 74 additions & 0 deletions aws/dynamodb-autoscaler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# dynamodb-autoscaler

Terraform module to provision DynamoDB autoscaler.

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


---

## Usage


**IMPORTANT:** The `master` branch is used in `source` just as an example. In your code, do not pin to `master` because there may be breaking changes between releases.
Instead pin to the release tag (e.g. `?ref=tags/x.y.z`) of one of our [latest releases]


```hcl
module "dynamodb_autoscaler" {
source = "git::https://github.com/cloudposse/terraform-aws-dynamodb-autoscaler.git?ref=master"
namespace = "eg"
stage = "dev"
name = "cluster"
dynamodb_table_name = "eg-dev-cluster-terraform-state-lock"
dynamodb_indexes = ["first-index", "second-index"]
dynamodb_table_arn = "arn:aws:dynamodb:us-east-1:123456789012:table/eg-dev-cluster-terraform-state-lock"
autoscale_write_target = 50
autoscale_read_target = 50
autoscale_min_read_capacity = 5
autoscale_max_read_capacity = 20
autoscale_min_write_capacity = 5
autoscale_max_write_capacity = 20
}
```




## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| attributes | Additional attributes (e.g. `1`) | list(string) | `<list>` | no |
| autoscale_max_read_capacity | DynamoDB autoscaling max read capacity | number | `20` | no |
| autoscale_max_write_capacity | DynamoDB autoscaling max write capacity | number | `20` | no |
| autoscale_min_read_capacity | DynamoDB autoscaling min read capacity | number | `5` | no |
| autoscale_min_write_capacity | DynamoDB autoscaling min write capacity | number | `5` | no |
| autoscale_read_target | The target value for DynamoDB read autoscaling | number | `50` | no |
| autoscale_write_target | The target value for DynamoDB write autoscaling | number | `50` | no |
| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | string | `-` | no |
| dynamodb_indexes | List of DynamoDB indexes | list(string) | `<list>` | no |
| dynamodb_table_arn | DynamoDB table ARN | string | - | yes |
| dynamodb_table_name | DynamoDB table name | string | - | yes |
| enabled | Set to false to prevent the module from creating any resources | bool | `true` | no |
| name | Name (e.g. `app` or `cluster`) | string | - | yes |
| namespace | Namespace (e.g. `eg` or `cp`) | string | `` | no |
| stage | Stage (e.g. `prod`, `dev`, `staging`, `infra`) | string | `` | no |
| tags | Additional tags (e.g. map(`BusinessUnit`,`XYZ`) | map(string) | `<map>` | no |

## Outputs

| Name | Description |
|------|-------------|
| appautoscaling_read_target_id | Appautoscaling read target ID |
| appautoscaling_read_target_index_id | Appautoscaling read target index ID |
| appautoscaling_write_target_id | Appautoscaling write target ID |
| appautoscaling_write_target_index_id | Appautoscaling write target index ID |
| autoscaler_iam_role_arn | Autoscaler IAM Role ARN |
| autoscaler_iam_role_id | Autoscaler IAM Role ID |



### Code from:
- https://github.com/cloudposse/terraform-aws-dynamodb-autoscaler
- https://www.terraform.io/docs/modules/index.html
202 changes: 202 additions & 0 deletions aws/dynamodb-autoscaler/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
module "default_label" {
source = "github.com/Wycliffe-USA/terraform-modules//generic/null-label?ref=0.1.0"
namespace = var.namespace
stage = var.stage
name = var.name
component = var.component
delimiter = var.delimiter
attributes = var.attributes
tags = var.tags
enabled = var.enabled

label_order = var.label_order
}

resource "random_id" "code" {
byte_length = 4
}

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" {
count = var.enabled ? 1 : 0
name = "${module.default_label.id}${var.delimiter}autoscaler${var.delimiter}${random_id.code.hex}"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
tags = module.default_label.tags
}

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

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

resources = [var.dynamodb_table_arn]

effect = "Allow"
}
}

resource "aws_iam_role_policy" "autoscaler" {
count = var.enabled ? 1 : 0
name = "${module.default_label.id}${var.delimiter}autoscaler${var.delimiter}dynamodb"
role = join("", 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" {
count = var.enabled ? 1 : 0
name = "${module.default_label.id}${var.delimiter}autoscaler${var.delimiter}cloudwatch"
role = join("", aws_iam_role.autoscaler.*.id)
policy = data.aws_iam_policy_document.autoscaler_cloudwatch.json
}

resource "aws_appautoscaling_target" "read_target" {
count = var.enabled ? 1 : 0
max_capacity = var.autoscale_max_read_capacity
min_capacity = var.autoscale_min_read_capacity
resource_id = "table/${var.dynamodb_table_name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
service_namespace = "dynamodb"
}

resource "aws_appautoscaling_target" "read_target_index" {
count = var.enabled ? length(var.dynamodb_indexes) : 0
max_capacity = var.autoscale_max_read_capacity
min_capacity = var.autoscale_min_read_capacity
resource_id = "table/${var.dynamodb_table_name}/index/${element(var.dynamodb_indexes, count.index)}"
scalable_dimension = "dynamodb:index:ReadCapacityUnits"
service_namespace = "dynamodb"
}

resource "aws_appautoscaling_policy" "read_policy" {
count = var.enabled ? 1 : 0
name = "DynamoDBReadCapacityUtilization:${join("", aws_appautoscaling_target.read_target.*.resource_id)}"
policy_type = "TargetTrackingScaling"
resource_id = join("", aws_appautoscaling_target.read_target.*.resource_id)

scalable_dimension = join("", aws_appautoscaling_target.read_target.*.scalable_dimension)
service_namespace = join("", 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_policy" "read_policy_index" {
count = var.enabled ? length(var.dynamodb_indexes) : 0

name = "DynamoDBReadCapacityUtilization:${element(
aws_appautoscaling_target.read_target_index.*.resource_id,
count.index
)}"

policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.read_target_index.*.resource_id[count.index]
scalable_dimension = aws_appautoscaling_target.read_target_index.*.scalable_dimension[count.index]
service_namespace = aws_appautoscaling_target.read_target_index.*.service_namespace[count.index]

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}

target_value = var.autoscale_read_target
}
}

resource "aws_appautoscaling_target" "write_target" {
count = var.enabled ? 1 : 0
max_capacity = var.autoscale_max_write_capacity
min_capacity = var.autoscale_min_write_capacity
resource_id = "table/${var.dynamodb_table_name}"
scalable_dimension = "dynamodb:table:WriteCapacityUnits"
service_namespace = "dynamodb"
}

resource "aws_appautoscaling_target" "write_target_index" {
count = var.enabled ? length(var.dynamodb_indexes) : 0
max_capacity = var.autoscale_max_write_capacity
min_capacity = var.autoscale_min_write_capacity
resource_id = "table/${var.dynamodb_table_name}/index/${element(var.dynamodb_indexes, count.index)}"
scalable_dimension = "dynamodb:index:WriteCapacityUnits"
service_namespace = "dynamodb"
}

resource "aws_appautoscaling_policy" "write_policy" {
count = var.enabled ? 1 : 0
name = "DynamoDBWriteCapacityUtilization:${join("", aws_appautoscaling_target.write_target.*.resource_id)}"
policy_type = "TargetTrackingScaling"
resource_id = join("", aws_appautoscaling_target.write_target.*.resource_id)

scalable_dimension = join("", aws_appautoscaling_target.write_target.*.scalable_dimension)
service_namespace = join("", 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
}
}

resource "aws_appautoscaling_policy" "write_policy_index" {
count = var.enabled ? length(var.dynamodb_indexes) : 0

name = "DynamoDBWriteCapacityUtilization:${element(
aws_appautoscaling_target.write_target_index.*.resource_id,
count.index
)}"

policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.write_target_index.*.resource_id[count.index]
scalable_dimension = aws_appautoscaling_target.write_target_index.*.scalable_dimension[count.index]
service_namespace = aws_appautoscaling_target.write_target_index.*.service_namespace[count.index]

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBWriteCapacityUtilization"
}

target_value = var.autoscale_write_target
}
}
29 changes: 29 additions & 0 deletions aws/dynamodb-autoscaler/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
output "autoscaler_iam_role_id" {
value = join("", aws_iam_role.autoscaler.*.id)
description = "Autoscaler IAM Role ID"
}

output "autoscaler_iam_role_arn" {
value = join("", aws_iam_role.autoscaler.*.arn)
description = "Autoscaler IAM Role ARN"
}

output "appautoscaling_read_target_id" {
value = join("", aws_appautoscaling_target.read_target.*.id)
description = "Appautoscaling read target ID"
}

output "appautoscaling_read_target_index_id" {
value = join("", aws_appautoscaling_target.read_target_index.*.id)
description = "Appautoscaling read target index ID"
}

output "appautoscaling_write_target_id" {
value = join("", aws_appautoscaling_target.write_target.*.id)
description = "Appautoscaling write target ID"
}

output "appautoscaling_write_target_index_id" {
value = join("", aws_appautoscaling_target.write_target_index.*.id)
description = "Appautoscaling write target index ID"
}
Loading

0 comments on commit b457fa6

Please sign in to comment.