We will use Terraform to build an AWS RDS MySQL 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-MySQLdb-terraform
$ cd AWS-RDS-MySQLdb-terraform
The files in this terraform configuration are :
rds.tf
sg.tf
README.md
vars.tf
provider.tf
secret.tf
vpc.tf
vars.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”
}
}
variable “RDSPWD” {
default = “Password”
}
provider.tf
- Define the instance type (t2.micro is free tier eligible)
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
version = "~> 1.30"
}
resource "aws_instance" "instance2" {
ami = "${var.amis[var.region]}"
instance_type = "${var.aws_instance_type}"
subnet_id = "${aws_subnet.instance2-Pub1.id}"
vpc_security_group_ids = ["${aws_security_group.instance2.id}"]
}
sg.tf
- Define the security group
resource "aws_security_group" "instance2" {
name = "instance2"
description = "RDS db servers (terraform-managed)"
vpc_id = "${aws_vpc.instance2.id}"
ingress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
# Allow all outbound traffic.
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = "instance2"
}
}
secret.tf
Defnie the variables that will be populated with the AWS access keys
variable "access_key" {}
variable "secret_key" {}
RDS.tf
- Create the RDS MySQL database
resource "aws_db_instance" "instance2" {
skip_final_snapshot = true
engine = "mysql"
engine_version = "5.7.19"
identifier = "instance2"
instance_class = "db.t2.small"
multi_az = false
name = "instance2"
publicly_accessible = true
allocated_storage = 5
name = "instance2"
username = "meme"
password = "${var.RDSPWD}"
parameter_group_name = "default.mysql5.7"
db_subnet_group_name = "${aws_db_subnet_group.instance2.name}"
vpc_security_group_ids = ["${aws_security_group.instance2.id}"]
depends_on = ["aws_internet_gateway.instance2"]
tags {
Name = "instance2"
}
}
output "EndPoint" {
value = "${aws_db_instance.instance2.endpoint}"
}
output "Identifier" {
value = "${aws_db_instance.instance2.identifier}"
}
output "DB_Name" {
value = "${aws_db_instance.instance2.name}"
}
output "Port" {
value = "${aws_db_instance.instance2.port}"
}
output "MasterUsername" {
value = "${aws_db_instance.instance2.username}"
}
Execute
$ terraform init -var-file="/home/devopsdba/Documents/...../SECRET_variables.auto.tfvars"
:
$ terraform apply -var-file="/home/devopsdba/Documents/..../SECRET_variables.auto.tfvars"
:
Plan: 14 to add, 0 to change, 0 to destroy.
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:
aws_vpc.instance2: Creating…
:
aws_db_instance.instance2: Creation complete after 4m5s (ID: instance2)
Outputs:
Apply complete! Resources: 14 added, 0 changed, 0 destroyed.
Outputs:
DB_Name = instance2
EndPoint = instance2.cffnmgekx1nd.us-east-2.rds.amazonaws.com:3306
Identifier = instance2
MasterUsername = meme
Port = 3306
$
We can now start MySQL Woirkbench and test the database
AuthoradminPosted onDecember 23, 2018CategoriesMySQL/MariaDB, UncategorizedEdit