Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.7 kB
2
Indexable
Never
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.6.2"
    }
  }
}

provider "aws" {
  region = var.aws_region
}


----


resource "aws_db_instance" "postgres" {
  engine               = "postgres"
  instance_class       = var.db_instance_class
  allocated_storage    = var.db_allocated_storage
  storage_type         = "gp2"
  engine_version       = var.db_engine_version
  username             = var.db_username
  password             = var.db_password
  name                 = var.db_name
  multi_az             = var.db_multi_az
}

resource "aws_db_instance" "postgres_replica" {
  count                = var.db_replica_count
  identifier_suffix    = "-replica-${count.index + 1}"
  engine               = "postgres"
  instance_class       = var.db_replica_instance_class
  allocated_storage    = var.db_allocated_storage
  storage_type         = "gp2"
  engine_version       = var.db_engine_version
  username             = var.db_username
  password             = var.db_password
  name                 = var.db_name
  multi_az             = false
  source_db_identifier =aws_db_instance.postgres.identifier
}

variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-west-2"
}

variable "aws_access_key" {
  description = "AWS access key"
  type        = string
}

variable "aws_secret_access_key" {
  description = "AWS secret access key"
  type        = string
}

variable "db_name" {
  description = "Database name"
  type        = string
  default     = "mydatabase"
}

variable "db_username" {
  description = "Database username"
  type        = string
  default     = "myuser"
}

variable "db_password" {
  description = "Database password"
  type        = string
  default     = "mypassword"
}

variable "db_instance_class" {
  description = "Database instance class"
  type        = string
  default     = "db.t2.micro"
}

variable "db_allocated_storage" {
  description = "Database allocated storage (in GB)"
  type        = number
  default     = 10
}

variable "db_engine_version" {
  description = "Database engine version"
  type        = string
  default     = "13.4"
}

variable "db_multi_az" {
  description = "Enable multiple availability zones for the primary instance"
  type        = bool
  default     = true
}

variable "db_replica_count" {
  description = "Number of read replicas"
  type        = number
  default     = 2
}

variable "db_replica_instance_class" {
  description = "Database read replica instance class"
  type        = string
  default     = "db.t2.micro"
}






---


provider "aws" {
  access_key = var.aws_access_key
  secret_access_key = var.aws_secret_access_key
}