How To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLI
- Mohammad Abu Mattar
- Cloud Computing
- 13 Nov, 2022
- 13 Mins read
Introduction
In this post, I will show you how to connect a two EC2 instances database and files transfer using AWS CLI. I will use AWS CLI to create a VPC, EC2 instances, EBS, EFS, and security groups. I will use the EC2 instances to connect to the database and files transfer.
Prerequisites
- AWS CLI installed and configured
- IAM user with the following permissions:
- AmazonVPCFullAccess
- AmazonEC2FullAccess
- AmazonElasticBlockStoreFullAccess
- AmazonElasticFileSystemFullAccess
Create VPC
Step 1: Create VPC
To create a VPC, run the following command:
Explanation:
aws ec2 create-vpc
- Create a VPC--cidr-block
- The IPv4 network range for the VPC, in CIDR notation.--query
- The JMESPath query that is applied to the output.--output
- The output format of the command.--resources
- The IDs of the resources.--tags
- The tags to apply to the resource.
Step 2: Modify your custom VPC and enable DNS hostname support, and DNS support
To modify your custom VPC and enable DNS hostname support, and DNS support, run the following command:
Explanation:
aws ec2 modify-vpc-attribute
- Modifies the specified attribute of the specified VPC.--vpc-id
- The ID of the VPC.--enable-dns-hostnames
- Indicates whether the instances launched in the VPC get DNS hostnames.--enable-dns-support
- Indicates whether DNS resolution is supported for the VPC.
Step 3: Create a Public Subnet
To create a public subnet, run the following command:
Explanation:
aws ec2 create-subnet
- Creates a subnet in an existing VPC.--vpc-id
- The ID of the VPC.--cidr-block
- The IPv4 network range for the subnet, in CIDR notation.--query
- The JMESPath query that is applied to the output.--output
- The output format of the command.--resources
- The IDs of the resources.--tags
- The tags to apply to the resource.
Step 4: Enable Auto-assign Public IP on the subnet
To enable auto-assign public IP on the subnet, run the following command:
Explanation:
aws ec2 modify-subnet-attribute
- Modifies a subnet attribute.--subnet-id
- The ID of the subnet.--map-public-ip-on-launch
- Specify true to indicate that network interfaces created in the specified subnet should be assigned a public IPv4 address.
Step 5: Create an Internet Gateway
To create an Internet Gateway, run the following command:
Explanation:
aws ec2 create-internet-gateway
- Creates an Internet gateway for use with a VPC.--query
- The JMESPath query that is applied to the output.--output
- The output format of the command.--resources
- The IDs of the resources.--tags
- The tags to apply to the resource.
Step 6: Attach the Internet Gateway to the VPC
To attach the Internet Gateway to the VPC, run the following command:
Explanation:
aws ec2 attach-internet-gateway
- Attaches an Internet gateway to a VPC, enabling connectivity between the Internet and the VPC.--internet-gateway-id
- The ID of the Internet gateway.--vpc-id
- The ID of the VPC.
Step 7: Create a Route Table
To create a route table, run the following command:
Explanation:
aws ec2 create-route-table
- Creates a route table for the specified VPC.--vpc-id
- The ID of the VPC.--query
- The JMESPath query that is applied to the output.--output
- The output format of the command.--resources
- The IDs of the resources.--tags
- The tags to apply to the resource.
Step 8: Create a custom route table association
To create a custom route table association, run the following command:
Explanation:
aws ec2 associate-route-table
- Associates a subnet with a route table.--subnet-id
- The ID of the subnet.--route-table-id
- The ID of the route table.
Step 9: Associate the subnet with route table, making it a public subnet
To associate the subnet with route table, making it a public subnet, run the following command:
Explanation:
aws ec2 create-route
- Creates a route in a route table within a VPC.--route-table-id
- The ID of the route table for the route.--destination-cidr-block
- The IPv4 CIDR address block used for the destination match.--gateway-id
- The ID of an Internet gateway or virtual private gateway attached to your VPC.
Step 10: Create a Security Group
To create a security group, run the following command:
Explanation:
aws ec2 create-security-group
- Creates a security group.--group-name
- The name of the security group.--description
- A description for the security group.--vpc-id
- The ID of the VPC.--query
- The JMESPath query that is applied to the output.--output
- The output format of the command.--resources
- The IDs of the resources.--tags
- The tags to apply to the resource.
Step 11: Add a rule to the security group
To add a rule to the security group, run the following command:
Explanation:
aws ec2 authorize-security-group-ingress
- Adds one or more ingress rules to a security group.--group-id
- The ID of the security group.--protocol
- The IP protocol name or number.--port
- The port number.--cidr
- The IPv4 CIDR range.--output
- The output format of the command.
Create a Two EC2 Instances
Step 1: Get the latest AMI ID
To get the latest AMI ID, run the following command:
Explanation:
aws ec2 describe-images
- Describes one or more of the images (AMIs, AKIs, and ARIs) available to you.--owners
- Filters the images by the owner.--filters
- The filters to apply to the images.--query
- The JMESPath query that is applied to the output.--output
- The output format of the command.
Step 2: Create a Key Pair
To create a key pair, run the following command:
Explanation:
aws ec2 create-key-pair
- Creates a 2048-bit RSA key pair with the specified name.--key-name
- The name for the key pair.--query
- The JMESPath query that is applied to the output.--output
- The output format of the command.chmod 400 aws-key-pair.pem
- Changes the permission of the key pair, making it read-only.
Step 3: Create a User Data Script
To create a user data script, run the following command:
The user data script performs the following tasks:
- Updates the system.
- Installs the EFS utilities.
- Installs Apache HTTP Server.
- Installs PHP.
- Installs MariaDB.
- Secures MariaDB.
- Creates a database.
- Creates a user.
- Grants privileges to the user.
- Creates a PHP page.
- Restarts Apache HTTP Server.
The user data script performs the following tasks:
- Updates the system.
- Installs the EFS utilities.
- Installs Apache HTTP Server.
- Installs PHP.
- Installs MariaDB.
- Restarts Apache HTTP Server.
- Secures MariaDB.
Step 4: Create a Two EC2 Instances
To create two EC2 instances, run the following command:
Explanation:
AWS_EC2_INSTANCE_1
andAWS_EC2_INSTANCE_2
are variables that store the instance IDs of the two EC2 instances.- The
aws ec2 run-instances
command creates two EC2 instances. - The
--image-id
option specifies the AMI ID. - The
--instance-type
option specifies the instance type. - The
--key-name
option specifies the key pair name. - The
--monitoring
option specifies whether detailed monitoring is enabled. - The
--security-group-ids
option specifies the security group ID. - The
--subnet-id
option specifies the subnet ID. - The
--user-data
option specifies the user data script. - The
--query
option specifies the query to retrieve the instance ID. - The
--output
option specifies the output format. - The
aws ec2 create-tags
command adds a tag to the instances.
Create an EBS Volume
Step 1: Create an EBS Volume With Multiple
To create an EBS volume with all availability zones, run the following command:
Explanation:
AWS_AVAILABILITY_ZONE
is a variable that stores the availability zone.- The
aws ec2 describe-availability-zones
command retrieves the availability zone. - The
--query
option specifies the query to retrieve the availability zone. - The
--output
option specifies the output format. - The
aws ec2 create-volume
command creates an EBS volume. - The
--availability-zone
option specifies the availability zone. - The
--size
option specifies the size of the volume, in GiB. - The
--volume-type
option specifies the volume type. - The
--iops
option specifies the number of I/O operations per second (IOPS) that the volume supports. - The
--query
option specifies the query to retrieve the volume ID. - The
--output
option specifies the output format. - The
aws ec2 create-tags
command adds a tag to the EBS volume.
Step 2: Attach the EBS Volume to the First EC2 Instance
To attach the EBS volume to the first EC2 instance, run the following command:
Explanation:
- The
aws ec2 attach-volume
command attaches the EBS volume to the first EC2 instance. - The
--device
option specifies the device name. - The
--instance-id
option specifies the instance ID. - The
--volume-id
option specifies the volume ID.
Step 3: Create a File System and Mount the EBS Volume
To create a file system and mount the EBS volume, run the following command:
Explanation:
- The
ssh
command connects to the first EC2 instance. - The
sudo mkfs -t ext4 /dev/xvdf
command creates a file system. - The
sudo mkdir /data
command creates a directory. - The
sudo mount /dev/xvdf /data
command mounts the EBS volume. - The
sudo echo "/dev/xvdf /data ext4 defaults 0 0" >> /etc/fstab
command adds the EBS volume to the fstab file. - The
exit
command exits the first EC2 instance.
Step 4: Attach the EBS Volume to the Second EC2 Instance
To attach the EBS volume to the second EC2 instance, run the following command:
Explanation:
- The
aws ec2 attach-volume
command attaches the EBS volume to the second EC2 instance. - The
--device
option specifies the device name. - The
--instance-id
option specifies the instance ID. - The
--volume-id
option specifies the volume ID.
Step 5: Create a File System and Mount the EBS Volume
To create a file system and mount the EBS volume, run the following command:
Explanation:
- The
ssh
command connects to the second EC2 instance. - The
sudo mkfs -t ext4 /dev/xvdf
command creates a file system. - The
sudo mkdir /data
command creates a directory. - The
sudo mount /dev/xvdf /data
command mounts the EBS volume. - The
sudo echo "/dev/xvdf /data ext4 defaults 0 0" >> /etc/fstab
command adds the EBS volume to the fstab file. - The
exit
command exits the second EC2 instance.
Create an EFS File System
Step 1: Create an EFS File System
To create an EFS file system, run the following command:
Explanation:
- The
aws efs create-file-system
command creates an EFS file system. - The
--performance-mode
option specifies the performance mode. - The
--throughput-mode
option specifies the throughput mode. - The
--query
option specifies the query to retrieve the file system ID. - The
--output
option specifies the output format. - The
aws efs create-tags
command adds a tag to the EFS file system.
Step 2: Create an EFS Mount Target
To create an EFS mount target, run the following command:
Explanation:
- The
aws efs create-mount-target
command creates an EFS mount target. - The
--file-system-id
option specifies the file system ID. - The
--subnet-id
option specifies the subnet ID. - The
--security-groups
option specifies the security group ID. - The
--query
option specifies the query to retrieve the mount target ID. - The
--output
option specifies the output format.
Step 3: Mount the EFS File System to Two EC2 Instances
To mount the EFS file system to two EC2 instances, run the following command:
Explanation:
- The
ssh
command connects to the first EC2 instance. - The
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 $AWS_EFS_FILE_SYSTEM.efs.$AVAILABILITY_ZONE.amazonaws.com:/ /var/www/html
command mounts the EFS file system. - The
sudo echo "$AWS_EFS_FILE_SYSTEM.efs.$AVAILABILITY_ZONE.amazonaws.com:/ /var/www/html nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 0 0" >> /etc/fstab
command adds the EFS file system to the fstab file. - The
exit
command exits the first EC2 instance. - The
ssh
command connects to the second EC2 instance. - The
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 $AWS_EFS_FILE_SYSTEM.efs.$AVAILABILITY_ZONE.amazonaws.com:/ /var/www/html
command mounts the EFS file system. - The
sudo echo "$AWS_EFS_FILE_SYSTEM.efs.$AVAILABILITY_ZONE.amazonaws.com:/ /var/www/html nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 0 0" >> /etc/fstab
command adds the EFS file system to the fstab file. - The
exit
command exits the second EC2 instance.
Create a Database Replication on EBS
Step 1: Create a Database Replication on EBS
To create a database replication on EBS, run the following command:
- Connect to the first EC2 instance.
- Create a database Replication
Explanation:
-
The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "CREATE USER 'replication'@'%' IDENTIFIED BY '121612';"
command creates a database replication. -
The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%';"
command grants replication slave on all databases to the replication user. -
The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "FLUSH PRIVILEGES;"
command flushes the privileges. -
The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "SHOW MASTER STATUS;"
command shows the master status. -
Connect to the second EC2 instance.
- Create a database Replication
Explanation:
- The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "CHANGE MASTER TO MASTER_HOST='$AWS_EC2_INSTANCE_1_PRIVATE_IP', MASTER_USER='replication', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=0;"
command changes the master to the first EC2 instance. - The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "START SLAVE;"
command starts the slave. - The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "SHOW SLAVE STATUS\G;"
command shows the slave status.
Test the Replication
Step 1: Test the Replication
To test the replication, run the following command:
- Connect to the first EC2 instance.
Explanation:
-
The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "CREATE DATABASE MKLLC;"
command creates a database MK LLC. -
The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; CREATE TABLE users (id INT NOT NULL AUTO_INCREMENT, FIRST_NAME VARCHAR(255) NOT NULL, LAST_NAME VARCHAR(255) NOT NULL, PRIMARY KEY (id));"
command creates a table users. -
The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; INSERT INTO users (FIRST_NAME, LAST_NAME) VALUES ('FIRST_NAME', 'FIRST_NAME');"
command inserts a record to the table users. -
The
sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; SELECT * FROM users;"
command selects all records from the table users. -
Connect to the second EC2 instance.
If the replication is successful, the command returns the following output:
The command tests the replication.
Why is the Replication Important?
The replication is important because it provides the following benefits:
- It provides high availability.
- It provides disaster recovery.
- It provides data redundancy.
- It provides data security.
The replication is important.
Conclusion
In this article, you learned how to create a database replication on EBS and EFS. You also learned why the replication is important.