-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.tf
103 lines (90 loc) · 2.35 KB
/
main.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
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:BatchGetItem",
"dynamodb:BatchWriteItem",
"dynamodb:ConditionCheckItem",
"dynamodb:DeleteItem",
"dynamodb:DescribeTable",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:UpdateItem"
]
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
security_group_ids = var.security_group_ids
subnet_group_name = aws_dax_subnet_group.subnet_group.id
server_side_encryption {
enabled = true
}
}