-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
84 lines (64 loc) · 2.28 KB
/
main.tf
File metadata and controls
84 lines (64 loc) · 2.28 KB
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
terraform {
required_version = ">= 1.0.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.0.0"
}
}
}
/*
* == ElastiCache
*
* Setup the service!
*/
resource "aws_elasticache_subnet_group" "this" {
name = "${var.service_name}-cache-subnet"
subnet_ids = var.subnet_ids
}
resource "aws_elasticache_replication_group" "this" {
replication_group_id = var.service_name
description = "Replication group for ElastiCache"
engine = var.engine
engine_version = var.engine_version
parameter_group_name = var.parameter_group_name
node_type = var.node_type
port = 6379
preferred_cache_cluster_azs = var.availability_zones
automatic_failover_enabled = length(var.availability_zones) > 1
multi_az_enabled = length(var.availability_zones) > 1
num_cache_clusters = length(var.availability_zones) == 0 ? 1 : length(var.availability_zones)
apply_immediately = var.apply_immediately
at_rest_encryption_enabled = true
transit_encryption_enabled = true
subnet_group_name = aws_elasticache_subnet_group.this.name
security_group_ids = [aws_security_group.this.id]
user_group_ids = var.user_group_ids
}
/*
* == Security Groups
*
* Allow the given services to communicate with Elasticache
*/
resource "aws_security_group" "this" {
name = "${var.service_name}-redis-cluster"
vpc_id = var.vpc_id
}
resource "aws_security_group_rule" "ingress_from_application_to_elasticache" {
count = length(var.security_group_ids)
security_group_id = aws_security_group.this.id
type = "ingress"
protocol = "tcp"
from_port = aws_elasticache_replication_group.this.port
to_port = aws_elasticache_replication_group.this.port
source_security_group_id = var.security_group_ids[count.index]
}
resource "aws_security_group_rule" "egress_from_application_to_elasticache" {
count = length(var.security_group_ids)
security_group_id = var.security_group_ids[count.index]
type = "egress"
protocol = "tcp"
from_port = aws_elasticache_replication_group.this.port
to_port = aws_elasticache_replication_group.this.port
source_security_group_id = aws_security_group.this.id
}