In the previous post we saw how to easily deploy (in just some mouse clicks) MySQL Database Service on two different regions with data replicated from the source to the replica using OCI Resource Manager Stack.
In this article, we will, this time, see how we can create similar architecture using the same Terraform modules in command line.
To make it a bit more fun, I will extend the architecture to a third region. What we gonna deploy will look like this:
The main region will be in USA (Ashburn) and will consist in a MDS HA instance using 3 availability domains. Then we will have replicas in two different regions: Europe (Germany) and Japan (Tokyo). Europe and Japan will be linked to USA using Dynamic Routing Gateways (DGR).
Get the code
The first step is to get the code on your computer:
$ git clone https://github.com/lefred/oci-mds-multi-region.git
Be sure you have a recent terraform binary in your path:
$ terraform --version Terraform v1.0.8 on linux_amd64 Your version of Terraform is out of date! The latest version is 1.0.9. You can update by downloading from https://www.terraform.io/downloads.html
If it’s not the case, just get the latest version:
$ cd ~/bin $ wget https://releases.hashicorp.com/terraform/1.0.9/terraform_1.0.9_linux_amd64.zip $ unzip terraform_1.0.9_linux_amd64.zip Archive: terraform_1.0.9_linux_amd64.zip replace terraform? [y]es, [n]o, [A]ll, [N]one, [r]ename: y inflating: terraform $ terraform --version Terraform v1.0.9 on linux_amd64
We can go back to the git repository we cloned and initialize Terraform to get the latest OCI Provider and modules:
$ terraform init Initializing modules... - mds-instance in modules/mds-instance - mds-instance_replica in modules/mds-instance - mysql-shell-replica in modules/mysql-shell - mysql-shell-source in modules/mysql-shell Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/oci... - Finding latest version of hashicorp/tls... - Finding latest version of hashicorp/template... - Installing hashicorp/oci v4.48.0... - Installed hashicorp/oci v4.48.0 (signed by HashiCorp) - Installing hashicorp/tls v3.1.0... - Installed hashicorp/tls v3.1.0 (signed by HashiCorp) - Installing hashicorp/template v2.2.0... - Installed hashicorp/template v2.2.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. 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
Terraform OCI Configuration
In the code we just cloned, there is a file called terraform.tfvars.template that we need to copy and fill with some information:
$ cp terraform.tfvars.template terraform.tfvars
The easiest way to get some important information is to use OCI Console:
Some information like compartment_ocid is not part of the data copied and must be retrieved manually.
When you edited the file, you realized that some two regions were already present, we need to add an extra one. The file should look like this:
Adding another region
To add the third region, we will need to modify several files manually.
The repository provides a script that will prepare the files for an extra domain, just use it like this:
$ tools/add_new_region.sh adding a new region...
We can verify the content of terraform.tfvars and modify the regions and the vcn cidr. At the end of the file we should see these new lines:
region_replica2 = "ap-tokyo-1" vcn_cidr_replica2 = "10.2.0.0/16"
If you want to specify custom values for any variables defined in variables.tf you need to specify them in this file.
By default, a compute instance with MySQL Shell will be created on the region acting as Source and one in the first replica. If you want to change that, this is specified in the file compute.tf.
It’s time to check if everything is correct by running these two commands:
$ terraform init $ terraform plan
Deployment in OCI
If you don’t have any error, you should see all resources that will be deployed and you can start the deployment:
$ terraform apply
At the end we should see something like this:
Let’s have a look in OCI Console to confirm that everything is up and running as expected:
And as expected, we see that we have the DRG attached from the source to the other two regions:
And more
After that, I destroyed everything and started again from scratch. This time I ran ./tools/add_new_region.sh 4 times. I modified the new added lines in terraform.tfvars to look like this:
region_replica2 = "ap-tokyo-1" vcn_cidr_replica2 = "10.2.0.0/16" region_replica3 = "eu-amsterdam-1" vcn_cidr_replica3 = "10.3.0.0/16" region_replica4 = "uk-london-1" vcn_cidr_replica4 = "10.4.0.0/16"
And I deployed everything:
$ terraform init $ terraform apply
After +/- 6 minutes, the output was like this:
As you can see it’s very easy to deploy MDS replicas in different region of the world.
If we connect to the MySQL Source instance, we can also quickly verify that the replicas are connected:
All good and we can compare their IP and see that they are in the VCN cidr we specified.
MySQL Database Service, OCI and Terraform is a really nice combination to quickly deploy in the cloud.
Enjoy MySQL and MySQL Database Service !