-
Notifications
You must be signed in to change notification settings - Fork 11
/
scale.tf
108 lines (89 loc) · 3.44 KB
/
scale.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
data "aws_iam_policy_document" "assume_role" {
statement {
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = ["application-autoscaling.amazonaws.com"]
}
effect = "Allow"
}
}
resource "aws_iam_role" "auto_scale_role" {
name = "${var.app_name}-autoscaling-role"
assume_role_policy = "${data.aws_iam_policy_document.assume_role.json}"
}
data "aws_iam_policy_document" "autoscaler" {
statement {
actions = [
"dynamodb:DescribeTable",
"dynamodb:UpdateTable",
]
resources = ["${aws_dynamodb_table.default.arn}"]
effect = "Allow"
}
}
resource "aws_iam_role_policy" "autoscaler" {
name = "${var.app_name}-autoscaler-dynamodb"
role = "${aws_iam_role.auto_scale_role.id}"
policy = "${data.aws_iam_policy_document.autoscaler.json}"
}
data "aws_iam_policy_document" "autoscaler_cloudwatch" {
statement {
actions = [
"cloudwatch:PutMetricAlarm",
"cloudwatch:DescribeAlarms",
"cloudwatch:DeleteAlarms",
]
resources = ["${aws_dynamodb_table.default.arn}"]
effect = "Allow"
}
}
resource "aws_iam_role_policy" "autoscaler_cloudwatch" {
name = "${var.app_name}-autoscaler-cloudwatch"
role = "${aws_iam_role.auto_scale_role.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"
role_arn = "${aws_iam_role.auto_scale_role.arn}"
}
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"
role_arn = "${aws_iam_role.auto_scale_role.arn}"
}
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}"
}
}