Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To set up PostgresSQL in AWS Aurora V2 with Terraform, you can follow these steps:

  1. First, create an AWS Aurora V2 instance with the appropriate parameters for your use case, such as the region, engine, and instance class type.

  2. Next, create a subnet group that will allow the instance to connect to your VPC.

  3. Create an RDS security group that will allow incoming traffic to your Aurora instance.

  4. Set up the PostgresSQL parameters for your Aurora instance, including the database name, username, and password.

  5. Use Terraform to create and manage these resources, ensuring that you have proper access and permissions.

  6. Finally, test your PostgresSQL connection by accessing it using a client application or tool.

Here's some example Terraform code to create an Aurora PostgresSQL database:

resource "aws_rds_cluster_instance" "postgresql" {
  identifier           = "postgresql"
  cluster_identifier   = aws_rds_cluster.postgresql.id
  instance_class       = "db.r5.large"
  engine               = "aurora-postgresql"
  engine_version       = "11.8"
  publicly_accessible  = false
  db_subnet_group_name = aws_db_subnet_group.postgresql.id
  vpc_security_group_id = aws_security_group.postgresql.id
  apply_immediately    = true
  tags = {
    Name = "postgresql-instance"
  }
}


resource "aws_db_subnet_group" "postgresql" {
  name          = "postgresql-subnet-group"
  subnet_ids    = aws_subnet.postgresql.*.id
  description   = "Subnet group for PostgreSQL instances"
}

resource "aws_security_group" "postgresql" {
  name_prefix = "postgresql"
  description = "Security group for PostgreSQL instances"
  vpc_id      = aws_vpc.postgresql.id

  ingress {
    from_port   = 5432
    to_port     = 5432
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_rds_cluster" "postgresql" {
  engine             = "aurora-postgresql"
  engine_version     = "11.8"
  database_name      = "mydatabase"
  master_username    = "admin"
  master_password    = "password"
  cluster_identifier = "postgresql"
  db_subnet_group_name = aws_db_subnet_group.postgresql.id
  vpc_security_group_ids = [aws_security_group.postgresql.id]

  scaling_configuration {
    auto_pause = true
    max_capacity = 2
    min_capacity = 2
  }

  tags = {
    Name = "postgresql-cluster"
  }
}

resource "aws_subnet" "postgresql" {
  count = var.availability_zones_count

  cidr_block = cidrsubnet(var.vpc_cidr_block, 4, count.index)
  availability_zone = element(var.availability_zones, count.index)

  tags = {
    Name = "postgresql-subnet-${count.index}"
  }
}