Skip to content

Instantly share code, notes, and snippets.

@yasudacloud
Created October 28, 2022 09:11
Show Gist options
  • Save yasudacloud/6c2973f95e64dbcb32172635fb8babe4 to your computer and use it in GitHub Desktop.
Save yasudacloud/6c2973f95e64dbcb32172635fb8babe4 to your computer and use it in GitHub Desktop.
// VPCとサブネットの作成を省略してID指定します
variable "vpc_id" {
default = ""
}
variable "subnet_a_id" {
default = ""
}
variable "subnet_c_id" {
default = ""
}
locals {
private_domain = "aurora-private.com"
reader_endpoint_domain = "reader.aurora-private.com"
writer_endpoint_domain = "writer.aurora-private.com"
}
resource "aws_rds_cluster" "db-cluster" {
cluster_identifier = "aurora-cluster1"
engine = "aurora-mysql"
engine_version = "5.7.mysql_aurora.2.10.2"
availability_zones = ["ap-northeast-1a", "ap-northeast-1c"]
database_name = "demo"
master_username = "admin"
master_password = "yasudacloud-20221028"
db_subnet_group_name = aws_db_subnet_group.aurora-subnet-group.name
final_snapshot_identifier = "aurora-demo"
skip_final_snapshot = false
lifecycle {
ignore_changes = [
"availability_zones",
"master_password"
]
}
}
resource "aws_rds_cluster_instance" "test1" {
apply_immediately = true
cluster_identifier = aws_rds_cluster.db-cluster.id
identifier = "instance1"
instance_class = "db.t2.small"
engine = aws_rds_cluster.db-cluster.engine
engine_version = aws_rds_cluster.db-cluster.engine_version
}
resource "aws_rds_cluster_instance" "test2" {
apply_immediately = true
cluster_identifier = aws_rds_cluster.db-cluster.id
identifier = "instance2"
instance_class = "db.t2.small"
engine = aws_rds_cluster.db-cluster.engine
engine_version = aws_rds_cluster.db-cluster.engine_version
}
resource "aws_db_subnet_group" "aurora-subnet-group" {
subnet_ids = [
var.subnet_a_id,
var.subnet_c_id
]
}
resource "aws_route53_zone" "aurora-host-zone" {
name = local.private_domain
vpc {
vpc_id = var.vpc_id
}
}
# Readエンドポイント
resource "aws_route53_record" "reader-domain" {
type = "CNAME"
zone_id = aws_route53_zone.aurora-host-zone.id
ttl = 20
for_each = {
(local.reader_endpoint_domain) = aws_rds_cluster.db-cluster.reader_endpoint,
(local.writer_endpoint_domain) = aws_rds_cluster.db-cluster.endpoint,
}
name = each.key
records = [each.value]
depends_on = [
aws_rds_cluster.db-cluster
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment