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.

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

    1. Add route tables padaonpremPublicRT

      • 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

    2. Buat route table padaawsPublicRT

      • 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

    3. Buat route table padaawsPrivateRT

      • 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

  1. Buat subnet group

    • klik subnet groups

    • klik create DB subnet group

    • name: A4LDBSNGROUP

    • Description: A4LDBSNGROUP

    • VPC: awsVPC

    • Availability Zones: us-east-1 dan us-east-1b

    • Subnets: privateA 10.16.32.0/20, privateB 10.16.96.0/20

    • klik create

  2. 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

  3. 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

  4. 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
      
  5. 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
      
  6. 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
      
  7. Pindahkan asset ke direktori html

    • masuk ke instance awsCatWeb

        cd /home/ec2-user
        cd html
        cp * -R /var/www/html/
      
  8. 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
      
  9. Akses wordpress, lihat publik IP awsCatWeb

Create DMS subnet

  1. 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

  2. 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

  3. 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

  4. 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

  5. Test connection

    • klik aws

    • klik test connection

    • klik run test, pastikan status successfull

  6. 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

  7. 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' );
        ...
      
  8. 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');"
    
  9. Stop instance on-premises, untuk melakukan ujicoba

    • stop instance catWeb

    • stop instance catDB

  10. 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:

  1. Hapus instance

  2. Hapus RDS

  3. Hapus Database migration tasks

  4. Hapus Endpoints

  5. Hapus replication instances

  6. Hapus route table entry

  7. Hapus peering connection

  8. Hapus CloudFormation stack

Thanks.


Referensi:

https://docs.aws.amazon.com/dms/latest/userguide/Welcome.html