We will use Terraform to build an AWS RDS Oracle database which uses the default VNC.
All the following operations have been completed on a Centos VM.
If the following git repository hasn’t been download already then execute the following:
$ cd ~/git
$ git clone https://github.com/paulhhallam/AWS-RDS-Oracle
$ cd AWS-RDS-Oracle
The files in this terraform configuration are :
ORACLE_example.tf
README.txt
aws.tf
secret.tf
aws.tf
- Define the variables
variable “region” {default = “us-east-2”}
variable “aws_instance_type” {
default = “t2.micro”
}
variable “amis” {
type = “map”
default = {
“us-east-1” = “ami-b374d5a5”
“us-east-2” = “ami-5e8bb23b”
“us-west-2” = “ami-4b32be2b”
}
}
secret.tf
Defnie the variables that will be populated with the AWS access keys
variable "access_key" {}
variable "secret_key" {}
ORACLE_example.tf
- Create the RDS Oracle database
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
version = "~> 1.30"
}
# Data sources to get VPC, subnets and security group details
#
data "aws_vpc" "default" {
default = true
}
data "aws_subnet_ids" "all" {
vpc_id = "${data.aws_vpc.default.id}"
}
data "aws_security_group" "default" {
vpc_id = "${data.aws_vpc.default.id}"
name = "default"
}
resource "aws_db_instance" "OracleRDSdb" {
source = "terraform-aws-modules/rds/aws"
allocated_storage = 30
identifier = "demodb"
publicly_accessible = "true"
engine = "oracle-ee"
engine_version = "12.1.0.2.v8"
instance_class = "db.t2.micro"
allocated_storage = 10
storage_encrypted = false
license_model = "bring-your-own-license"
# Make sure that database name is capitalized, otherwise RDS will try to recreate RDS instance every time
name = "DEMODB"
username = "user1"
password = "Password1"
port = "1521"
iam_database_authentication_enabled = false
vpc_security_group_ids = ["${data.aws_security_group.default.id}"]
vpc_security_group_ids = ["sg-a42140ce"]
maintenance_window = "Mon:00:00-Mon:03:00"
backup_window = "03:00-06:00"
disable backups to create DB faster
backup_retention_period = 0
tags = {
Owner = "user"
Environment = "dev"
}
# DB subnet group
subnet_ids = ["subnet-92e3acfa", "subnet-a4be3ade", "subnet-a806e9e4"]
DB parameter group
family = "oracle-ee-12.1"
DB option group
major_engine_version = "12.1"
Snapshot name upon DB deletion
final_snapshot_identifier = "demodb"
See here for support character sets https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.OracleCharacterSets.html
character_set_name = "AL32UTF8"
}
OUTPUT
output "EndPoint" {
value = "${aws_db_instance.OracleRDSdb.endpoint}"
}
output "Identifier" {
value = "${aws_db_instance.OracleRDSdb.identifier}"
}
output "DB_Name" {
value = "${aws_db_instance.OracleRDSdb.name}"
}
output "Port" {
value = "${aws_db_instance.OracleRDSdb.port}"
}
output "Username" {
value = "${aws_db_instance.OracleRDSdb.username}"
}
Initialise
$ tfi
OR
$ terraform init -var-file=”/home/devopsdba/Documents/SECRET_variables.auto.tfvars”
Initializing modules…
- module.OracleRDSdb
Found version 1.22.0 of terraform-aws-modules/rds/aws on registry.terraform.io
Getting source “terraform-aws-modules/rds/aws” - module.OracleRDSdb.db_subnet_group
Getting source “./modules/db_subnet_group” - module.OracleRDSdb.db_parameter_group
Getting source “./modules/db_parameter_group” - module.OracleRDSdb.db_option_group
Getting source “./modules/db_option_group” - module.OracleRDSdb.db_instance
Getting source “./modules/db_instance”
Initializing provider plugins…
- Checking for available provider plugins on https://releases.hashicorp.com…
- Downloading plugin for provider “aws” (1.54.0)…
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running “terraform plan” to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Execute
$ tfa
OR
$ terraform apply -var-file=”/home/devopsdba/Documents/SECRET_variables.auto.tfvars”
data.aws_vpc.default: Refreshing state…
data.aws_security_group.default: Refreshing state…
data.aws_subnet_ids.all: Refreshing state…
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols
+ Create
Terraform will perform the following actions:
:
:
:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only ‘yes’ will be accepted to approve.
Enter a value:
Entering YES will produce:
module.OracleRDSdb.module.db_subnet_group.aws_db_subnet_group.this: Creating…
arn: “” => “”
description: “” => “Database subnet group for demodb”
name: “” => “”
name_prefix: “” => “demodb-“
subnet_ids.#: “” => “3”
subnet_ids.1865186897: “” => “subnet-a4be3ade”
subnet_ids.2008161303: “” => “subnet-92e3acfa”
subnet_ids.533165975: “” => “subnet-a806e9e4”
tags.%: “” => “3”
tags.Environment: “” => “dev”
tags.Name: “” => “demodb”
tags.Owner: “” => “user”
:
:
:
module.OracleRDSdb.db_instance.aws_db_instance.this: Still creating… (20m40s elapsed)
module.OracleRDSdb.module.db_instance.aws_db_instance.this: Creation complete after 20m43s (ID: demodb)
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
Outputs:
DB_Name = DEMODB
EndPoint = demodb.cffnmgekx1nd.us-east-2.rds.amazonaws.com:1521
Identifier = demodb
Port = 1521
Username = user1
We can now start Oracle SQL Developer and test the database
Destroy
Remember to destroy the database when finished
$ tfd
OR
$ terraform destroy -var-file=”/home/devopsdba/Documents/SECRET_variables.auto.tfvars”‘