Ask Your Question
1

How can Terraform be utilized to set up postgressql in AWS Aurora V2 (serverless)?

asked 2023-05-18 01:39:05 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-18 01:52:01 +0000

qstack gravatar image

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}"
  }
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-18 01:39:05 +0000

Seen: 8 times

Last updated: May 18 '23