Restore an Oracle RDS Database from a Snapshot

Introduction

The following code can be used to restore an Oracle RDS database from the latest snapshot or a specified snapshot.

- Terraform Version 0.14
- AWS Provider version 3.x

Step-By-Step - Restore from the Latest Snapshot

  1. Copy the main.auto.tfvars, variables.tf, data.tf and main.tf into a directory
  2. Update the main.auto.tfvars file with the correct details
  3. Update main.tf with values appropriate to your account
  4. Update the variables.tf file if required
  5. Run terraform init
  6. Run terraform apply

main.auto.tfvars

#
# main.auto.tfvars
#

aws_account = "myaccount"
rds_instance_name = "mydb1
vpc_sg_id  = "sg-123123"

variables.tf

#
# variables.tf
#

variable "application" {
  default = "oracle"
}

variable "aws_account" {}

variable "aws_region {
  default = "eu-west-1"
}

variable "rds_instance_class {
  default = "db.r5.large"
}

variable "rds_instance_name {}

variable "vpc_sg_id {}

data.tf

#
# data.tf
# 

#
# Get latest snapshot for a givwen DB
#

data "aws_db_snapshot" "db_snapshot" {
    most_recent = true
    db_instance_identifier = var.rds_instance_name
}

main.tf

#
# main.tf
#

#
# Set your provider
#

provider "aws" {
  region = var.aws_region
}

#
# Restore the DB from the Snapshot Above
#

resource "aws_db_instance" "my_db" {
  instance_class  = var.rds_instance_class
  identifier = var.rds_instance_name
  db_subnet_group_name = ${var.rds_instance_name}-sngrp
  snapshot_identifier = "${data.aws_db_snapshot.db_snapshot.id}"
  vpc_security_group_ids  = [var.vpc_sg_id]
  parameter_group_name = spfile${var.rds_instances_name}
  option_group_name = option-group-${var.rds_instance_name}
  skip_final_snapshot = false
  tags = {
    Name = "${var.aws_account}-${rds_instance_name}
    Application = var.application
}

Step-By-Step - Restore from a Named Snapshot

  1. Copy the main.auto.tfvars, variables.tf and main.tf into a directory
  2. Update the main.auto.tfvars file with the correct details
  3. Update main.tf with values appropriate to your account
  4. Update the variables.tf file if required
  5. Run terraform init
  6. Run terraform apply

main.auto.tfvars

#
# main.auto.tfvars
#

aws_account = "myaccount"
rds_instance_name = "mydb1
rds_snapshot = "my_named_snapshot"
vpc_sg_id  = "sg-123123"

variables.tf

#
# variables.tf
#

variable "application" {
  default = "oracle"
}

variable "aws_account" {}

variable "aws_region {
  default = "eu-west-1"
}

variable "rds_instance_class {
  default = "db.r5.large"
}

variable "rds_instance_name {}

variable "rds_snapshot" {}

variable "vpc_sg_id {}

main.tf

#
# main.tf
#

#
# Set your provider
#

provider "aws" {
  region = var.aws_region
}

#
# Restore the DB from the Snapshot Above
#

resource "aws_db_instance" "my_db" {
  instance_class = var.rds_instance_class
  identifier = var.rds_instance_name
  db_subnet_group_name = ${var.rds_instance_name}-sngrp
  snapshot_identifier = var.rds_snapshot
  vpc_security_group_ids  = [var.vpc_sg_id]
  skip_final_snapshot = false
  parameter_group_name = spfile${var.rds_instances_name}
  option_group_name = option-group-${var.rds_instance_name}
  tags = {
    Name = "${var.aws_account}-${rds_instance_name}
    Application = var.application
}

Published 1st September 2021

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License