yikegaya’s blog

yikegayaのブログ

public access可能なRDSをterraformで作成した時のメモ

実運用ではダメだけどちょっと試したいことがあってpublic access可能なRDSをterraformで作った時のメモ

必要なこと

  • RDSのpublicly_accessibleをtrueにする
  • VPCのenable_dns_supportとenable_dns_hostnamesをtrueにする
  • RDSに紐づいているsubnetのaccessをpublicにする

public accessできないterraformとのdiff

git diff
diff --git a/rds.tf b/rds.tf
index 1aaca8b..49f41a0 100644
--- a/rds.tf
+++ b/rds.tf
@@ -15,4 +15,5 @@ resource "aws_db_instance" "movie-backend" {
   port                    = 3306
   vpc_security_group_ids  = [aws_security_group.movie-backend-rds-sg.id]
   db_subnet_group_name    = aws_db_subnet_group.movie-backend-db-subnet.name
+  publicly_accessible     = true
 }
diff --git a/subnet_group.tf b/subnet_group.tf
index 1c329c8..b4d8a70 100644
--- a/subnet_group.tf
+++ b/subnet_group.tf
@@ -1,4 +1,4 @@
 resource "aws_db_subnet_group" "movie-backend-db-subnet" {
   name        = "movie-rds-subnet-group"
-  subnet_ids  = [aws_subnet.movie-backend-private-1a.id, aws_subnet.movie-backend-private-1c.id, aws_subnet.movie-backend-private-1d.id]
+  subnet_ids  = [aws_subnet.movie-backend-public-1a.id, aws_subnet.movie-backend-public-1c.id, aws_subnet.movie-backend-public-1d.id]
 }
diff --git a/vpc.tf b/vpc.tf
index 6365c63..79f2602 100644
--- a/vpc.tf
+++ b/vpc.tf
@@ -1,5 +1,7 @@
 resource "aws_vpc" "movie-backend" {
   cidr_block = "10.0.0.0/16"
+  enable_dns_support   = true
+  enable_dns_hostnames = true

   tags = {
     Name = "movie-backend"