Photo by Julia Craice on Unsplash
Migrasi Database kedalam AWS menggunakan AWS DMS (Database Migration Service)
Migrasi relasional database dari on-premises kedalam cloud AWS menggunakan AWS DMS.
Ikhtisar
AWS DMS (Database Migration Service) dapat digunakan untuk migrasi relasional database, data warehouse, NoSQL dan lainnya kedalam AWS. untuk lebih detailnya bisa dibaca disini.
Pada tulisan kali ini kita akan melakukan simulasi migrasi web apps yang ada di on-premises kedalam AWS menggunakan AWS DMS.
Prasyarat
Akun AWS
Biaya
Langkah-langkah
Provisioning resource
Provisioning resource menggunakan AWS CloudFormation dengan template berikut ini: template
Buat VPC Peering
klik VPC
pilih
Peering connections
klik
create peering connection
isi name
VPC ID (Requester): onpremVPC
Account: My account
Region: This Region
VPC ID (Acepter): awsVPC
klik create connection
klik Accept request
Buat Route Table
Add route tables pada
onpremPublicRT
pilih menu route tables
pilih
onpremPublicRT
pada tab Routes, klik Edit routes
klik Add route
Destination: isi IPv4 CIDR awsVPC
10.16.0.0/16
Target: peering connection
A4L-ON-PREMISES-TO-AWS
klik Save changes
Buat route table pada
awsPublicRT
pilih
awsPublicRT
pada tab Routes, klik Edit routes
klik add route
Destination: isi IPv4 CIDR onpremVPC
192.168.10.0/24
Target: peering connection
A4L-ON-PREMISES-TO-AWS
klik save changes
Buat route table pada
awsPrivateRT
pilih
awsPrivateRT
pada tab routes, klik Edit routes
klik add route
Destination: isi IPv4 CIDR onpremVPC
192.168.10.0/24
Target: peering connection
A4L-ON-PREMISES-TO-AWS
klik save changes
Buat RDS Database
Buat subnet group
klik subnet groups
klik create DB subnet group
name:
A4LDBSNGROUP
Description:
A4LDBSNGROUP
VPC:
awsVPC
Availability Zones:
us-east-1
danus-east-1b
Subnets: privateA
10.16.32.0/20
, privateB10.16.96.0/20
klik create
Buat database
pada menu databases
klik create database
Choose a database creation method: Standard create
Engine type: MariaDB
Templates: Free tier
DB instance identifier:
a4lwordpress
Master password:
cats-dogs-rabbits-chickens
Confirm master password:
cats-dogs-rabbits-chickens
Connectivity, Virtual private cloud (VPC):
awsVPC
DB subnet group: a4ldbsngroup
Existing VPC security groups:
DMS-awsSecurityGroupDB
Expand additional configuration
Initial database name:
a4lwordpress
klik create database
Buat EC2 Instance
masuk ke EC2
launch instance
name: awsCatWeb
Amazon Machine Image: Amazon Linux 2 AMI (HVM)
instance type: t2.micro
Network setting klik edit
VPC: awsVPC
subnet: aws-publicA
Firewall (security groups): Select an existing security group
Common security groups: DMS-awsSecurityGroupWeb
pilih Advanced details
IAM instance profile: DMS-awsInstanceProfile-
klik Launch instance
Install wordpress requirements
konek ke ec2
jalankan perintah dibawah ini
yum -y update yum -y install httpd mariadb amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2 systemctl enable httpd systemctl start httpd
Setup SSH
enable password authentication
sudo vi /etc/ssh/sshd_config ... PasswordAuthentication yes ...
change
ec2-user
password, masukkan DBPassword sebelumnya:cats-dogs-rabbits-chickens
passwd ec2-user
restart ssh
systemctl restart sshd
Tes konek ke server awsCatWeb (aws) dari catWeb (on-premises)
pada instance catWeb, konek menggunakan session manager
sudo bash cd /var/www
copy html directory ke
awsCatWeb
pada directory /ec2-user/home/scp -rp html ec2-user@10.16.51.36:/home/ec2-user
Pindahkan asset ke direktori html
masuk ke instance awsCatWeb
cd /home/ec2-user cd html cp * -R /var/www/html/
Set permissions and access wordpress
set permission
usermod -a -G apache ec2-user chown -R ec2-user:apache /var/www chmod 2775 /var/www find /var/www -type d -exec chmod 2775 {} \; find /var/www -type f -exec chmod 0664 {} \; sudo systemctl restart httpd
Akses wordpress, lihat publik IP awsCatWeb
Create DMS subnet
Buat subnet group
Masuk ke DMS
pilih subnet group
Isi name and description
vpc: awsVCP
subnet: aws-privateA and aws-privateB
klik Create subnet group
Create replication instance
pilih Replication instance
klik Create replication instance
isi name and description:
A4LONPREMTOAWS
Instance class: dms.t3.micro
Multi AZ: dev or test workload (single AZ)
vpc: awsVPC
Replication subnet group: a4ldmssngroup
VPC security groups: DMS-awsSecurityGroupDB-
klik create replication
Create DMS endpoint
pada menu endpoint
klik create endpoint
endpoint type: source endpoint
Endpoint identifier: CatDBOnpremises
Source engine: mariadb
Access to endpoint database: Provide access information manually
Server name: 192.168.10.80
port: 3306
user name: a4lwordpress
Password: cats-dogs-rabbits-chickens
klik create endpoint
Create target endpoint
pada menu endpoint
klik create endpoint
endpoint type: target endpoint
Endpoint identifier: a4lwordpress
Source engine: mariadb
Access to endpoint database: Provide access information manually
Server name: a4lwordpress.cipytdpxpa94.us-east-1.rds.ama..
port: 3306
user name: a4lwordpress
Password: cats-dogs-rabbits-chickens
klik create endpoint
Test connection
klik aws
klik test connection
klik run test, pastikan status successfull
Create migration task
pada menu Database migration tasks
klik create task
Task identifier: A4LONPREMTOAWSWORDPRESS
Replication instance: a4lonpremtoaws
Source database endpoint: catdbonpremises
Target database endpoint: a4lwordpress
Migration type: Migrate Existing data
Table mappings: Klik add new selection rule
Schema: Enter a schema
Source name: a4lwordpress
Klik Create task
Edit Config database
masuk ke instance awsCatWeb
ubah konfig pada file
wp-config.php
edit DB Host menjadi endpoint RDS
cd /var/www/html vi wp-config.php ... /** MySQL hostname */ define( 'DB_HOST', 'a4lwordpress.cipytdpxpa94.us-east-1.rds.amazonaws.com' ); ...
Jalankan script untuk update database dengan DNS name instance yang baru.
#!/bin/bash source <(php -r 'require("/var/www/html/wp-config.php"); echo("DB_NAME=".DB_NAME."; DB_USER=".DB_USER."; DB_PASSWORD=".DB_PASSWORD."; DB_HOST=".DB_HOST); ') SQL_COMMAND="mysql -u $DB_USER -h $DB_HOST -p$DB_PASSWORD $DB_NAME -e" OLD_URL=$(mysql -u $DB_USER -h $DB_HOST -p$DB_PASSWORD $DB_NAME -e 'select option_value from wp_options where option_id = 1;' | grep http) HOST=$(curl http://169.254.169.254/latest/meta-data/public-hostname) $SQL_COMMAND "UPDATE wp_options SET option_value = replace(option_value, '$OLD_URL', 'http://$HOST') WHERE option_name = 'home' OR option_name = 'siteurl';" $SQL_COMMAND "UPDATE wp_posts SET guid = replace(guid, '$OLD_URL','http://$HOST');" $SQL_COMMAND "UPDATE wp_posts SET post_content = replace(post_content, '$OLD_URL', 'http://$HOST');" $SQL_COMMAND "UPDATE wp_postmeta SET meta_value = replace(meta_value,'$OLD_URL','http://$HOST');"
Stop instance on-premises, untuk melakukan ujicoba
stop instance catWeb
stop instance catDB
Akses Wordpress pada instance awsCatWeb
akses Public IPv4 DNS
Pada gambar diatas terlihat bahwa website sudah bisa diakses, migrasi sudah berhasil dilakukan menggunakan service Database Migration Service.
Destroy Resources
Jangan lupa untuk menghapus sumber daya yang tidak digunakan lagi, agar menghindari tagihan di waktu yang akan datang. Adapun sumber daya yang akan dihapus adalah:
Hapus instance
Hapus RDS
Hapus Database migration tasks
Hapus Endpoints
Hapus replication instances
Hapus route table entry
Hapus peering connection
Hapus CloudFormation stack
Thanks.
Referensi:
https://docs.aws.amazon.com/dms/latest/userguide/Welcome.html