terraform_aurora_postgresql

This is an old revision of the document!


To do


data "aws_vpc" "main_vpc" {
  filter {
    name   = "tag:Name"
    values = ["${var.environment}-main"]
  }
}

resource "aws_key_pair" "deployer" {
  key_name   = "deployer-key"
  public_key = "${var.public_key}"
}

data "aws_subnet" "ec2_subnet" {
	id = data.aws_subnets.private_subnets.ids[0]
}

data "aws_subnets" "private_subnets" {
	filter {
		name = "tag:Name"
		values = ["*main-private*"]
	}
}

resource "aws_security_group" "pgsql_allow" {
  name        = "allow_postgresql_ingress"
  description = "Allow PostgreSQL & SSH access"
  vpc_id      =  data.aws_vpc.main_vpc.id
  
  tags = {
    Name = "allow_ssh_postgresql"
  }
}

data "aws_security_group" "selected_sg" {
	vpc_id      =  data.aws_vpc.main_vpc.id
	
	filter {
		name = "tag:Name"
		values = ["allow_ssh_postgresql"]
	
	}
	depends_on = [aws_security_group.pgsql_allow]
}

resource "aws_security_group_rule" "ingress_rules" {
  count = length(var.ingress_rules)
  type              = "ingress"
  from_port         = var.ingress_rules[count.index].from_port
  to_port           = var.ingress_rules[count.index].to_port
  protocol          = var.ingress_rules[count.index].protocol
  cidr_blocks       = [data.aws_subnet.ec2_subnet.cidr_block]
  description       = var.ingress_rules[count.index].description
  security_group_id = data.aws_security_group.selected_sg.id
}

resource "aws_instance" "ec2_instance" {
    ami = var.ami
	count = 1
    subnet_id = data.aws_subnets.private_subnets.ids[0]
    instance_type = "t3.micro"
    key_name = aws_key_pair.deployer.key_name
	
	user_data=<<-EOF
	#!/bin/bash
	yum -y update
	yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
	yum install -y postgresql14
	yum install -y barman
	yum install -y barman-cli
	yum install -y telnet
	yum install -y unzip
	curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
	unzip awscliv2.zip
	sudo ./aws/install
	EOF
	
	tags = {
		Name = "Barman"
		Environment = "Backup"
		Terraform = "true"
	}
}

module "cluster" {
  source  = "terraform-aws-modules/rds-aurora/aws"
  name           = "test-aurora-db-postgres145"
  engine         = "aurora-postgresql"
  engine_version = "14.5"
  manage_master_user_password = false
  master_username= "admin1"
  master_password= "Test1234#!"
  instances = {
    one = {
	  instance_class = "db.r5.large"
	}
    two = {
      instance_class = "db.r5.large"
	}
  }
  
  vpc_id               	 = data.aws_vpc.main_vpc.id
  vpc_security_group_ids = [data.aws_security_group.selected_sg.id]
  db_subnet_group_name   = "${var.environment}-db-main"
  skip_final_snapshot  	 = true

  tags = {
    Environment = "dev"
    Terraform   = "true"
  }
}
  • terraform_aurora_postgresql.1687273674.txt.gz
  • Last modified: 2023/06/20 15:07
  • by andonovj