-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
95 lines (80 loc) · 2.3 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
variable "region" {
description = "The AWS region to deploy to."
type = string
default = "us-west-2"
}
provider "aws" {
region = var.region
access_key = var.credentials.access_key
secret_key = var.credentials.secret_key
}
variable "credentials" {
description = "The credentials for connecting to AWS."
type = object({
access_key = string
secret_key = string
})
sensitive = true
}
variable "vpc_id" {
description = "ID of the VPC where the Redis ElastiCache will be deployed."
type = string
}
variable "subnet_ids" {
description = "List of subnet IDs where the Redis ElastiCache will be deployed."
type = list(string)
}
variable "environment" {
description = "The environment name."
type = string
}
variable "app_name" {
description = "The application name."
type = string
}
variable "resource_name" {
description = "The name of the resource."
type = string
}
locals {
env_id = substr(var.environment, 0, min(15, length(var.environment)))
app_id = substr(var.app_name, 0, min(15, length(var.app_name)))
res_id = substr(split(".", var.resource_name)[3], 0, min(15, length(var.resource_name)))
cluster_id = replace(lower("${local.env_id}-${local.app_id}-${local.res_id}"), "_", "-")
}
resource "aws_security_group" "redis_security_group" {
vpc_id = var.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 6379
to_port = 6379
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_elasticache_subnet_group" "redis_subnet_group" {
name = local.cluster_id
subnet_ids = var.subnet_ids
}
resource "aws_elasticache_cluster" "redis_cluster" {
cluster_id = local.cluster_id
engine = "redis"
node_type = "cache.t3.micro"
num_cache_nodes = 1
parameter_group_name = "default.redis5.0"
engine_version = "5.0.6"
port = 6379
subnet_group_name = aws_elasticache_subnet_group.redis_subnet_group.name
security_group_ids = [aws_security_group.redis_security_group.id]
}
output "host" {
value = aws_elasticache_cluster.redis_cluster.cache_nodes.0.address
}
output "port" {
value = aws_elasticache_cluster.redis_cluster.cache_nodes.0.port
}