이제 private subnet에 rds를 만들어 볼 것 이다.
//rds
resource "aws_security_group" "rds_security" {
name = "rds_security"
description = "Security group for rds instance"
vpc_id = aws_vpc.meme_vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "rds_security"
}
}
이런식으로 rds 를 위한 보안 규칙을 먼저 만들어준다
그런다음
# RDS Subnet Group 생성
resource "aws_db_subnet_group" "db-subnet-group" {
name = "meme-db-subnet-group"
subnet_ids = [aws_subnet.db_private_subnet1.id, aws_subnet.db_private_subnet2.id]
# 서브넷에 이미 소속 VPC, AZ 정보를 입력하여 생성하였기 때문에, 서브넷 id만 나열해주면 subnet group 생성
}
resource "aws_db_instance" "mysql_db" {
engine = "mysql"
identifier = var.DB_IDENTIFIER
allocated_storage = 20
engine_version = "5.7"
instance_class = "db.t2.micro"
username = var.DB_USERNAME
password = var.DB_PASSWORD
parameter_group_name = "default.mysql5.7"
vpc_security_group_ids = [aws_security_group.rds_security.id] # db 보안그룹 지정
skip_final_snapshot = true
db_subnet_group_name = aws_db_subnet_group.db-subnet-group.name # DB가 배치될 서브넷 그룹(.name으로 지정)
}
이런식으로 만들어주었다.
그런다음 elastiCache도 만들어주었다.
비슷하게 보안 규칙을 만들어주고
//elasticache
resource "aws_security_group" "elasticache_security" {
name = "elasticache_security"
description = "Security group for elasticache "
vpc_id = aws_vpc.meme_vpc.id
ingress {
from_port = 6379
to_port = 6379
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "elasticcache_security"
}
}
그런다음 비슷하게 서브넷 그룹과 replication group도 만들어준다
resource "aws_elasticache_subnet_group" "example" {
name = "my-cache-subnet"
subnet_ids = [aws_subnet.db_private_subnet1.id, aws_subnet.db_private_subnet2.id]
}
resource "aws_elasticache_replication_group" "elasticache_cluster" {
replication_group_id = "tf-redis-cluster"
description = "MeMe-ElastiCache"
node_type = "cache.t2.micro"
port = 6379
parameter_group_name = "default.redis7"
engine_version = "7.1"
subnet_group_name = aws_elasticache_subnet_group.example.name
security_group_ids = [aws_security_group.elasticache_security.id]
num_cache_clusters = 1
}
이렇게 하면 rds와 elastiCache를 만들 수 있다.