---
title: "How To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLI"
lang: "en"
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/post/how-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli
---

![Blog post image for How To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLI - In this post, I will show you how to share a database and files between two EC2 instances using AWS CLI. I will use AWS CLI to create a VPC, EC2 instances, EBS, EFS, and security groups. Then I will replicate the database between the instances and share files between them.](/_astro/hero.839lq2GB_ZfS909.webp)

[Home](/)›[Blog](/blog)›[All Categories](/blog/categories)›[AWS](/blog/categories/aws)

Blog

[Prev in AWSHow To Connect A Two EC2 Instances Data Transfer Using AWS CLI Without AWS EFS](/blog/post/how-to-connect-a-two-ec2-instances-data-transfer-using-aws-cli-without-aws-efs)[Next in AWSHow to Connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO](/blog/post/how-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo)

[AWS](/blog/categories/aws)[EC2](/blog/categories/ec2)[EBS](/blog/categories/ebs)[EFS](/blog/categories/efs)[Database](/blog/categories/database)[Networking](/blog/categories/networking)

# How To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLI

[Mohammad Abu Mattar](/authors/mohammad-abu-mattar)Published: 13 Nov 202214 Mins read22 Mins listen

[Markdown for AI(opens in a new tab)](/post/how-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli/index.md "Open the plain-Markdown version of this page, for pasting into an AI tool")

TL;DR

In this post, I will show you how to share a database and files between two EC2 instances using AWS CLI. I will use AWS CLI to create a VPC, EC2 instances, EBS, EFS, and security groups. Then I will replicate the database between the instances and share files between them.

Series

[AWS EC2 Interconnection](/series/aws-ec2-interconnection)2/2

[PreviousHow To Connect A Two EC2 Instances Data Transfer Using AWS CLI Without AWS EFS](/blog/post/how-to-connect-a-two-ec2-instances-data-transfer-using-aws-cli-without-aws-efs)

All posts in this series (2)

Blog2

1.  [How To Connect A Two EC2 Instances Data Transfer Using AWS CLI Without AWS EFS](/blog/post/how-to-connect-a-two-ec2-instances-data-transfer-using-aws-cli-without-aws-efs)
2.  [How To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLIYou are here](/blog/post/how-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli)

### How To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLI

Contents

[Introduction](#introduction)[Prerequisites](#prerequisites)[Create a VPC](#create-a-vpc)[Step 1: create a VPC](#step-1-create-a-vpc)[Step 2: modify your custom VPC and enable DNS hostname support and DNS support](#step-2-modify-your-custom-vpc-and-enable-dns-hostname-support-and-dns-support)[Step 3: create a public subnet](#step-3-create-a-public-subnet)[Step 4: enable auto-assign public IP on the subnet](#step-4-enable-auto-assign-public-ip-on-the-subnet)[Step 5: create an Internet Gateway](#step-5-create-an-internet-gateway)[Step 6: attach the Internet Gateway to the VPC](#step-6-attach-the-internet-gateway-to-the-vpc)[Step 7: create a route table](#step-7-create-a-route-table)[Step 8: create a custom route table association](#step-8-create-a-custom-route-table-association)[Step 9: associate the subnet with the route table, making it a public subnet](#step-9-associate-the-subnet-with-the-route-table-making-it-a-public-subnet)[Step 10: create a security group](#step-10-create-a-security-group)[Step 11: Add a rule to the security group](#step-11-add-a-rule-to-the-security-group)[Create two EC2 instances](#create-two-ec2-instances)[Step 1: get the latest AMI ID](#step-1-get-the-latest-ami-id)[Step 2: create a key pair](#step-2-create-a-key-pair)[Step 3: create the user data scripts](#step-3-create-the-user-data-scripts)[Step 4: create the two EC2 instances](#step-4-create-the-two-ec2-instances)[Create an EBS volume](#create-an-ebs-volume)[Step 1: create an EBS volume](#step-1-create-an-ebs-volume)[Step 2: attach the EBS volume to the first EC2 instance](#step-2-attach-the-ebs-volume-to-the-first-ec2-instance)[Step 3: create a file system and mount the EBS volume on the first instance](#step-3-create-a-file-system-and-mount-the-ebs-volume-on-the-first-instance)[Step 4: attach the EBS volume to the second EC2 instance](#step-4-attach-the-ebs-volume-to-the-second-ec2-instance)[Step 5: create a file system and mount the EBS volume on the second instance](#step-5-create-a-file-system-and-mount-the-ebs-volume-on-the-second-instance)[Create an EFS file system](#create-an-efs-file-system)[Step 1: create an EFS file system](#step-1-create-an-efs-file-system)[Step 2: create an EFS mount target](#step-2-create-an-efs-mount-target)[Step 3: mount the EFS file system on both EC2 instances](#step-3-mount-the-efs-file-system-on-both-ec2-instances)[Create a database replication on EBS](#create-a-database-replication-on-ebs)[Step 1: create a database replication on EBS](#step-1-create-a-database-replication-on-ebs)[Test the replication](#test-the-replication)[Step 1: test the replication](#step-1-test-the-replication)[Why replication matters](#why-replication-matters)[Conclusion](#conclusion)[Resources](#resources)

## [Introduction](#introduction)

In this post, I will show you how to share a database and files between two EC2 instances using AWS CLI. I will use AWS CLI to create a VPC, EC2 instances, EBS, EFS, and security groups. Then I will replicate the database between the instances and share files between them.

## [Prerequisites](#prerequisites)

-   AWS CLI installed and configured
-   IAM user with the following permissions:
    -   AmazonVPCFullAccess
    -   AmazonEC2FullAccess
    -   AmazonElasticBlockStoreFullAccess
    -   AmazonElasticFileSystemFullAccess

## [Create a VPC](#create-a-vpc)

### [Step 1: create a VPC](#step-1-create-a-vpc)

To create a VPC, run the following command:

Terminal window

```
1# Create a VPC2AWS_VPC=$(aws ec2 create-vpc \3  --cidr-block 10.0.0.0/16 \4  --query 'Vpc.VpcId' \5  --output text)6
7# Add a name tag to the VPC8aws ec2 create-tags \9  --resources $AWS_VPC \10  --tags Key=Name,Value=aws-vpc
```

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](#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:

Terminal window

```
1# Enable DNS hostnames2aws ec2 modify-vpc-attribute \3  --vpc-id $AWS_VPC \4  --enable-dns-hostnames "{\"Value\":true}"5
6# Enable DNS support7aws ec2 modify-vpc-attribute \8  --vpc-id $AWS_VPC \9  --enable-dns-support "{\"Value\":true}"
```

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](#step-3-create-a-public-subnet)

To create a public subnet, run the following command:

Terminal window

```
1# Create a public subnet2AWS_PUBLIC_SUBNET=$(aws ec2 create-subnet \3  --vpc-id $AWS_VPC \4  --cidr-block 10.0.1.0/24 \5  --query 'Subnet.SubnetId' \6  --output text)7
8# Add a name tag to the public subnet9aws ec2 create-tags \10  --resources $AWS_PUBLIC_SUBNET \11  --tags Key=Name,Value=aws-public-subnet
```

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](#step-4-enable-auto-assign-public-ip-on-the-subnet)

To enable auto-assign public IP on the subnet, run the following command:

Terminal window

```
1# Enable auto-assign public IP on the subnet2aws ec2 modify-subnet-attribute \3  --subnet-id $AWS_PUBLIC_SUBNET \4  --map-public-ip-on-launch
```

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](#step-5-create-an-internet-gateway)

To create an Internet Gateway, run the following command:

Terminal window

```
1# Create an Internet Gateway2AWS_INTERNET_GATEWAY=$(aws ec2 create-internet-gateway \3  --query 'InternetGateway.InternetGatewayId' \4  --output text)5
6# Add a name tag to the Internet Gateway7aws ec2 create-tags \8  --resources $AWS_INTERNET_GATEWAY \9  --tags Key=Name,Value=aws-internet-gateway
```

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](#step-6-attach-the-internet-gateway-to-the-vpc)

To attach the Internet Gateway to the VPC, run the following command:

Terminal window

```
1# Attach the Internet Gateway to the VPC2aws ec2 attach-internet-gateway \3  --internet-gateway-id $AWS_INTERNET_GATEWAY \4  --vpc-id $AWS_VPC
```

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](#step-7-create-a-route-table)

To create a route table, run the following command:

Terminal window

```
1# Create a route table2AWS_ROUTE_TABLE=$(aws ec2 create-route-table \3  --vpc-id $AWS_VPC \4  --query 'RouteTable.RouteTableId' \5  --output text)6
7# Add a name tag to the route table8aws ec2 create-tags \9  --resources $AWS_ROUTE_TABLE \10  --tags Key=Name,Value=aws-route-table
```

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](#step-8-create-a-custom-route-table-association)

To create a custom route table association, run the following command:

Terminal window

```
1# Create a custom route table association2aws ec2 associate-route-table \3  --subnet-id $AWS_PUBLIC_SUBNET \4  --route-table-id $AWS_ROUTE_TABLE
```

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 the route table, making it a public subnet](#step-9-associate-the-subnet-with-the-route-table-making-it-a-public-subnet)

To associate the subnet with the route table, making it a public subnet, run the following command:

Terminal window

```
1# Associate the subnet with route table, making it a public subnet2aws ec2 create-route \3  --route-table-id $AWS_ROUTE_TABLE \4  --destination-cidr-block 0.0.0.0/0 \5  --gateway-id $AWS_INTERNET_GATEWAY
```

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](#step-10-create-a-security-group)

To create a security group, run the following command:

Terminal window

```
1# Create a security group2AWS_SECURITY_GROUP=$(aws ec2 create-security-group \3  --group-name aws-security-group \4  --description "AWS Security Group" \5  --vpc-id $AWS_VPC \6  --query 'GroupId' \7  --output text)8
9# Add a name tag to the security group10aws ec2 create-tags \11  --resources $AWS_SECURITY_GROUP \12  --tags Key=Name,Value=aws-security-group
```

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](#step-11-add-a-rule-to-the-security-group)

To add a rule to the security group, run the following command:

Terminal window

```
1# Add a rule to the security group2
3# Add SSH rule4aws ec2 authorize-security-group-ingress \5--group-id $AWS_SECURITY_GROUP \6--protocol tcp \7--port 22 \8--cidr 0.0.0.0/0 \9--output text10
11# Add HTTP rule12aws ec2 authorize-security-group-ingress \13--group-id $AWS_SECURITY_GROUP \14--protocol tcp \15--port 80 \16--cidr 0.0.0.0/0 \17--output text18
19# Add HTTPS rule20aws ec2 authorize-security-group-ingress \21--group-id $AWS_SECURITY_GROUP \22--protocol tcp \23--port 443 \24--cidr 0.0.0.0/0 \25--output text26
27# Add NFS rule28aws ec2 authorize-security-group-ingress \29--group-id $AWS_SECURITY_GROUP \30--protocol tcp \31--port 2049 \32--cidr 0.0.0.0/0 \33--output text34
35# Add mysql/arura rule36aws ec2 authorize-security-group-ingress \37--group-id $AWS_SECURITY_GROUP \38--protocol tcp \39--port 3306 \40--cidr 0.0.0.0/0 \41--output text
```

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 two EC2 instances](#create-two-ec2-instances)

### [Step 1: get the latest AMI ID](#step-1-get-the-latest-ami-id)

To get the latest AMI ID, run the following command:

Terminal window

```
1# Get the latest AMI ID2AWS_AMI=$(aws ec2 describe-images \3  --owners 'amazon' \4  --filters 'Name=name,Values=amzn2-ami-hvm-2.0.*' \5  'Name=state,Values=available' \6  --query 'sort_by(Images, &CreationDate)[-1].[ImageId]' \7  --output 'text')
```

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](#step-2-create-a-key-pair)

To create a key pair, run the following command:

Terminal window

```
1# Create a key pair2aws ec2 create-key-pair \3--key-name aws-key-pair \4--query 'KeyMaterial' \5--output text > aws-key-pair.pem6
7# Change the permission of the key pair8chmod 400 aws-key-pair.pem
```

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 the user data scripts](#step-3-create-the-user-data-scripts)

To create a user data script, run the following command:

```
1# Create a user data script2cat <<EOF > user-data.sh3#!/bin/bash4
5# Variables6ROOT_DB_PASSWORD="121612"7DB_NAME="DB"8DB_USER="user"9DB_PASSWORD="121612"10
11# update the system12sudo yum update -y13
14# install efs-utils15sudo yum install git rpm-build make -y16
17# clone the efs-utils repository18git clone https://github.com/aws/efs-utils19
20# change directory to efs-utils21cd efs-utils22
23# build the efs-utils24make rpm25
26# install the efs-utils27sudo yum install build/amazon-efs-utils*rpm -y28
29# install httpd30sudo yum install httpd -y31
32# start httpd33sudo systemctl start httpd34
35# enable httpd36sudo systemctl enable httpd37
38# At first, we will enable amazon-linux-extras so that we can specify the PHP version that we want to install.39sudo amazon-linux-extras enable php7.4 -y40
41# Install PHP42sudo yum install php php-{pear,cgi,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip,imap} -y43
44# Install MariaDB45sudo yum install mariadb-server -y46
47# Start MariaDB48sudo systemctl start mariadb49
50# Enable MariaDB51sudo systemctl enable mariadb52
53# Restart Apache54sudo systemctl restart httpd55
56# We will now secure MariaDB.57sudo mysql_secure_installation <<EOF258
59y60$ROOT_DB_PASSWORD61$ROOT_DB_PASSWORD62y63y64y65y66EOF267
68# Create a database69sudo mysql -u root -p$ROOT_DB_PASSWORD -e "CREATE DATABASE $DB_NAME;"70
71# Create a user72sudo mysql -u root -p$ROOT_DB_PASSWORD -e "CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASSWORD';"73
74# Grant privileges75sudo mysql -u root -p$ROOT_DB_PASSWORD -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost';"76
77# Flush privileges78sudo mysql -u root -p$ROOT_DB_PASSWORD -e "FLUSH PRIVILEGES;"79
80# Create PHP page81
82cat <<EOF3 > /var/www/html/index.php83<?php84\$servername = "localhost";85\$username = "$DB_USER";86\$password = "$DB_PASSWORD";87\$dbname = "$DB_NAME";88
89// Create connection90try {91    \$conn = new PDO("mysql:host=\$servername;dbname=\$dbname", \$username, \$password);92    // set the PDO error mode to exception93    \$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);94    echo "Connected successfully";95} catch(PDOException \$e) {96    echo "Connection failed: " . \$e->getMessage();97}98?>99EOF3100
101# Restart Apache102sudo systemctl restart httpd103
104EOF
```

The `user-data.sh` 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.

```
1# Create a user data script2cat <<EOF > user-data2.sh3#!/bin/bash4
5# Variables6ROOT_DB_PASSWORD="121612"7DB_NAME="DB"8DB_USER="user"9DB_PASSWORD="121612"10
11# update the system12sudo yum update -y13
14# install efs-utils15sudo yum install git rpm-build make -y16
17# clone the efs-utils repository18git clone https://github.com/aws/efs-utils19
20# change directory to efs-utils21cd efs-utils22
23# build the efs-utils24make rpm25
26# install the efs-utils27sudo yum install build/amazon-efs-utils*rpm -y28
29# install httpd30sudo yum install httpd -y31
32# start httpd33sudo systemctl start httpd34
35# enable httpd36sudo systemctl enable httpd37
38# At first, we will enable amazon-linux-extras so that we can specify the PHP version that we want to install.39sudo amazon-linux-extras enable php7.4 -y40
41# Install PHP42sudo yum install php php-{pear,cgi,common,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip,imap} -y43
44# Install MariaDB45sudo yum install mariadb-server -y46
47# Start MariaDB48sudo systemctl start mariadb49
50# Enable MariaDB51sudo systemctl enable mariadb52
53# Restart Apache54sudo systemctl restart httpd55
56# We will now secure MariaDB.57sudo mysql_secure_installation <<EOF258
59y60$ROOT_DB_PASSWORD61$ROOT_DB_PASSWORD62y63y64y65y66EOF267
68EOF
```

The second script, `user-data2.sh`, stops after securing MariaDB. It 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 the two EC2 instances](#step-4-create-the-two-ec2-instances)

To create two EC2 instances, run the following command:

Terminal window

```
1# Create two EC2 instances2AWS_EC2_INSTANCE_1=$(aws ec2 run-instances \3--image-id $AWS_AMI \4--instance-type t2.micro \5--key-name aws-key-pair \6--monitoring "Enabled=false" \7--security-group-ids $AWS_SECURITY_GROUP \8--subnet-id $AWS_PUBLIC_SUBNET \9--user-data file://user-data.sh \10--query 'Instances[0].InstanceId' \11--output text)12
13AWS_EC2_INSTANCE_2=$(aws ec2 run-instances \14--image-id $AWS_AMI \15--instance-type t2.micro \16--key-name aws-key-pair \17--monitoring "Enabled=false" \18--security-group-ids $AWS_SECURITY_GROUP \19--subnet-id $AWS_PUBLIC_SUBNET \20--user-data file://user-data2.sh \21--query 'Instances[0].InstanceId' \22--output text)23
24# Add a tag to the instances25aws ec2 create-tags \26--resources $AWS_EC2_INSTANCE_1 \27--tags Key=Name,Value=ec2-instance-128
29aws ec2 create-tags \30--resources $AWS_EC2_INSTANCE_2 \31--tags Key=Name,Value=ec2-instance-2
```

Explanation:

-   `AWS_EC2_INSTANCE_1` and `AWS_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](#create-an-ebs-volume)

### [Step 1: create an EBS volume](#step-1-create-an-ebs-volume)

To create an EBS volume, run the following command:

Terminal window

```
1AWS_AVAILABILITY_ZONE=$(aws ec2 describe-availability-zones \2--query 'AvailabilityZones[*].ZoneName' \3--output text)4
5# Create an EBS volume with all availability zones6AWS_EBS_VOLUME=$(aws ec2 create-volume \7--availability-zone $AWS_AVAILABILITY_ZONE \8--size 1 \9--volume-type io1 \10--iops 100 \11--query 'VolumeId' \12--output text)13
14# Add a tag to the EBS volume15aws ec2 create-tags \16--resources $AWS_EBS_VOLUME \17--tags Key=Name,Value=ebs-volume
```

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](#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:

Terminal window

```
1# Attach the EBS volume to the first EC2 instance2aws ec2 attach-volume \3--device /dev/sdf \4--instance-id $AWS_EC2_INSTANCE_1 \5--volume-id $AWS_EBS_VOLUME
```

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 on the first instance](#step-3-create-a-file-system-and-mount-the-ebs-volume-on-the-first-instance)

To create a file system and mount the EBS volume on the first instance, run the following command:

Terminal window

```
1# connect to the first EC2 instance2ssh -i aws-key-pair.pem ec2-user@$AWS_EC2_INSTANCE_1_PUBLIC_IP3
4# create a file system5sudo mkfs -t ext4 /dev/xvdf6
7# create a directory8sudo mkdir /data9
10# mount the EBS volume11sudo mount /dev/xvdf /data12
13# add the EBS volume to the fstab file14sudo echo "/dev/xvdf /data ext4 defaults 0 0" >> /etc/fstab15
16# exit the first EC2 instance17exit
```

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](#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:

Terminal window

```
1# Attach the EBS volume to the second EC2 instance2aws ec2 attach-volume \3--device /dev/sdf \4--instance-id $AWS_EC2_INSTANCE_2 \5--volume-id $AWS_EBS_VOLUME
```

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 on the second instance](#step-5-create-a-file-system-and-mount-the-ebs-volume-on-the-second-instance)

To create a file system and mount the EBS volume on the second instance, run the following command:

Terminal window

```
1# connect to the second EC2 instance2ssh -i aws-key-pair.pem ec2-user@$AWS_EC2_INSTANCE_2_PUBLIC_IP3
4# create a file system5sudo mkfs -t ext4 /dev/xvdf6
7# create a directory8sudo mkdir /data9
10# mount the EBS volume11sudo mount /dev/xvdf /data12
13# add the EBS volume to the fstab file14sudo echo "/dev/xvdf /data ext4 defaults 0 0" >> /etc/fstab15
16# exit the second EC2 instance17exit
```

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](#create-an-efs-file-system)

### [Step 1: create an EFS file system](#step-1-create-an-efs-file-system)

To create an EFS file system, run the following command:

Terminal window

```
1# Create an EFS file system2AWS_EFS_FILE_SYSTEM=$(aws efs create-file-system \3--performance-mode generalPurpose \4--throughput-mode bursting \5--query 'FileSystemId' \6--output text)7
8# Add a tag to the EFS file system9aws efs create-tags \10--file-system-id $AWS_EFS_FILE_SYSTEM \11--tags Key=Name,Value=efs-file-system
```

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](#step-2-create-an-efs-mount-target)

To create an EFS mount target, run the following command:

Terminal window

```
1# Create an EFS mount target2AWS_EFS_MOUNT_TARGET=$(aws efs create-mount-target \3--file-system-id $AWS_EFS_FILE_SYSTEM \4--subnet-id $AWS_PUBLIC_SUBNET \5--security-groups $AWS_SECURITY_GROUP \6--query 'MountTargetId' \7--output text)
```

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 on both EC2 instances](#step-3-mount-the-efs-file-system-on-both-ec2-instances)

To mount the EFS file system on both EC2 instances, run the following command:

Terminal window

```
1# connect to the first EC2 instance2ssh -i aws-key-pair.pem ec2-user@$AWS_EC2_INSTANCE_1_PUBLIC_IP3
4# mount the EFS file system5sudo 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/html6
7# add the EFS file system to the fstab file8sudo 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/fstab9
10# exit the first EC2 instance11exit12
13# connect to the second EC2 instance14ssh -i aws-key-pair.pem ec2-user@$AWS_EC2_INSTANCE_2_PUBLIC_IP15
16# mount the EFS file system17sudo 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/html18
19# add the EFS file system to the fstab file20sudo 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/fstab21
22# exit the second EC2 instance23exit
```

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](#create-a-database-replication-on-ebs)

### [Step 1: 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.

Terminal window

```
1# Connect to the first EC2 instance2ssh -i aws-key-pair.pem ec2-user@$AWS_EC2_INSTANCE_1_PUBLIC_IP
```

-   Create the replication user.

Terminal window

```
1# Variables2ROOT_DB_PASSWORD="121612"3
4# Create a database Replication5sudo mysql -u root -p$ROOT_DB_PASSWORD -e "CREATE USER 'replication'@'%' IDENTIFIED BY '121612';"6sudo mysql -u root -p$ROOT_DB_PASSWORD -e "GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%';"7sudo mysql -u root -p$ROOT_DB_PASSWORD -e "FLUSH PRIVILEGES;"8sudo mysql -u root -p$ROOT_DB_PASSWORD -e "SHOW MASTER STATUS;"
```

Explanation:

-   The `sudo mysql -u root -p$ROOT_DB_PASSWORD -e "CREATE USER 'replication'@'%' IDENTIFIED BY '121612';"` command creates the `replication` user.
    
-   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.
    

Terminal window

```
1# Connect to the second EC2 instance2ssh -i aws-key-pair.pem ec2-user@$AWS_EC2_INSTANCE_2_PUBLIC_IP
```

-   Start replication from the first instance.

Terminal window

```
1# Variables2ROOT_DB_PASSWORD="121612"3AWS_EC2_INSTANCE_1_PRIVATE_IP=''4
5# Create a database Replication6sudo 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;"7sudo mysql -u root -p$ROOT_DB_PASSWORD -e "START SLAVE;"8sudo mysql -u root -p$ROOT_DB_PASSWORD -e "SHOW SLAVE STATUS\G;"
```

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](#test-the-replication)

### [Step 1: test the replication](#step-1-test-the-replication)

To test the replication, run the following command:

-   Connect to the first EC2 instance.

Terminal window

```
1# Connect to the first EC2 instance2ssh -i aws-key-pair.pem ec2-user@$AWS_EC2_INSTANCE_1_PUBLIC_IP3
4# Variables5ROOT_DB_PASSWORD="121612"6
7# Create a database MK LLC8sudo mysql -u root -p$ROOT_DB_PASSWORD -e "CREATE DATABASE MKLLC;"9
10# Create a table users11sudo 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));"12
13# Insert a record to the table users14sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; INSERT INTO users (FIRST_NAME, LAST_NAME) VALUES ('John', 'Doe');"15sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; INSERT INTO users (FIRST_NAME, LAST_NAME) VALUES ('Jane', 'Doe');"16sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; INSERT INTO users (FIRST_NAME, LAST_NAME) VALUES ('Jack', 'Doe');"17sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; INSERT INTO users (FIRST_NAME, LAST_NAME) VALUES ('Jill', 'Doe');"18sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; INSERT INTO users (FIRST_NAME, LAST_NAME) VALUES ('Joe', 'Doe');"19
20# Select all records from the table users21sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; SELECT * FROM users;"
```

Explanation:

-   The `sudo mysql -u root -p$ROOT_DB_PASSWORD -e "CREATE DATABASE MKLLC;"` command creates the `MKLLC` database.
    
-   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 the `users` table.
    
-   The `sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; INSERT INTO users (FIRST_NAME, LAST_NAME) VALUES ('John', 'Doe');"` command inserts a record into the `users` table. The tutorial runs it five times, once per row.
    
-   The `sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; SELECT * FROM users;"` command selects all records from the `users` table.
    
-   Connect to the second EC2 instance.
    

Terminal window

```
1# Connect to the second EC2 instance2ssh -i aws-key-pair.pem ec2-user@$AWS_EC2_INSTANCE_2_PUBLIC_IP3
4# Variables5ROOT_DB_PASSWORD="121612"6
7# Select all records from the table users8sudo mysql -u root -p$ROOT_DB_PASSWORD -e "USE MKLLC; SELECT * FROM users;"
```

If the replication is successful, the command returns the following output:

Terminal window

```
1+----+------------+-----------+2| id | FIRST_NAME | LAST_NAME |3+----+------------+-----------+4|  1 | John       | Doe       |5|  2 | Jane       | Doe       |6|  3 | Jack       | Doe       |7|  4 | Jill       | Doe       |8|  5 | Joe        | Doe       |9+----+------------+-----------+
```

Those are the five rows created on the first instance, now readable on the second.

## [Why replication matters](#why-replication-matters)

Replication gives you high availability, disaster recovery, data redundancy, and stronger data security.

## [Conclusion](#conclusion)

In this article, you learned how to replicate a MariaDB database between two EC2 instances, and how to share storage between them with EBS and EFS. You also learned why replication is worth the setup.

## [Resources](#resources)

-   [Amazon EC2 User Guide for Linux Instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html)
-   [Amazon EBS User Guide](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonEBS.html)
-   [Amazon EFS User Guide](https://docs.aws.amazon.com/efs/latest/ug/whatisefs.html)
-   [Amazon VPC User Guide](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html)
-   [AWS Command Line Interface (CLI) User Guide](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)
-   [AWS CLI Command Reference - ec2](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/index.html)
-   [AWS CLI Command Reference - efs](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/efs/index.html)
-   [AWS Identity and Access Management (IAM) User Guide](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)
-   [MySQL Replication Documentation](https://dev.mysql.com/doc/refman/8.0/en/replication.html)
-   [Mounting EFS file systems](https://docs.aws.amazon.com/efs/latest/ug/mounting-fs.html)
-   [Attaching an Amazon EBS volume to an instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-attaching-volume.html)
-   [User data and shell scripts for EC2 instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html)
-   [Security groups for your VPC](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html)
-   [Tutorial: Installing a LAMP Web Server on Amazon Linux 2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2.html) (Relevant for setting up MariaDB/PHP)

Was this useful?

## Tags

[#AWS CLI](/blog/tags/aws-cli)[#EC2 Instance Communication](/blog/tags/ec2-instance-communication)[#EBS Volume](/blog/tags/ebs-volume)[#EFS File System](/blog/tags/efs-file-system)[#MySQL Replication](/blog/tags/mysql-replication)[#NFS](/blog/tags/nfs)[#Linux Server](/blog/tags/linux-server)[#VPC Configuration](/blog/tags/vpc-configuration)[#AWS Security Groups](/blog/tags/aws-security-groups)[#Data Transfer](/blog/tags/data-transfer)

## Share

[Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli "Share on Facebook")[Twitter](https://twitter.com/intent/tweet/?text=How%20To%20Connect%20A%20Two%20EC2%20Instances%20Database%20and%20Files%20Transfer%20Using%20AWS%20CLI&url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli "Share on Twitter")[LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli&title=How%20To%20Connect%20A%20Two%20EC2%20Instances%20Database%20and%20Files%20Transfer%20Using%20AWS%20CLI&summary=In%20this%20post%2C%20I%20will%20show%20you%20how%20to%20share%20a%20database%20and%20files%20between%20two%20EC2%20instances%20using%20AWS%20CLI.%20I%20will%20use%20AWS%20CLI%20to%20create%20a%20VPC%2C%20EC2%20instances%2C%20EBS%2C%20EFS%2C%20and%20security%20groups.%20Then%20I%20will%20replicate%20the%20database%20between%20the%20instances%20and%20share%20files%20between%20them.&source=https://mkabumattar.com "Share on LinkedIn")[WhatsApp](https://wa.me/?text=How%20To%20Connect%20A%20Two%20EC2%20Instances%20Database%20and%20Files%20Transfer%20Using%20AWS%20CLI%20https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli "Share on WhatsApp")[Telegram](https://t.me/share/url?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli&text=How%20To%20Connect%20A%20Two%20EC2%20Instances%20Database%20and%20Files%20Transfer%20Using%20AWS%20CLI "Share on Telegram")[Reddit](https://www.reddit.com/submit?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli&title=How%20To%20Connect%20A%20Two%20EC2%20Instances%20Database%20and%20Files%20Transfer%20Using%20AWS%20CLI "Share on Reddit")[Hacker News](http://news.ycombinator.com/submitlink?u=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli&t=How%20To%20Connect%20A%20Two%20EC2%20Instances%20Database%20and%20Files%20Transfer%20Using%20AWS%20CLI "Share on Hacker News")[Pinterest](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli&media=&description=In%20this%20post%2C%20I%20will%20show%20you%20how%20to%20share%20a%20database%20and%20files%20between%20two%20EC2%20instances%20using%20AWS%20CLI.%20I%20will%20use%20AWS%20CLI%20to%20create%20a%20VPC%2C%20EC2%20instances%2C%20EBS%2C%20EFS%2C%20and%20security%20groups.%20Then%20I%20will%20replicate%20the%20database%20between%20the%20instances%20and%20share%20files%20between%20them. "Share on Pinterest")[Email](<mailto:?subject=How%20To%20Connect%20A%20Two%20EC2%20Instances%20Database%20and%20Files%20Transfer%20Using%20AWS%20CLI&body=Check out this article: https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2Fhow-to-connect-a-two-ec2-instances-database-and-files-transfer-using-aws-cli>)

## Comments

## You might also enjoy

More posts on similar topics

[![How To Connect A Two EC2 Instances Data Transfer Using AWS CLI Without AWS EFS](/_astro/hero.WG5UXtzN_2uuusz.webp)](/blog/post/how-to-connect-a-two-ec2-instances-data-transfer-using-aws-cli-without-aws-efs)

## [How To Connect A Two EC2 Instances Data Transfer Using AWS CLI Without AWS EFS](/blog/post/how-to-connect-a-two-ec2-instances-data-transfer-using-aws-cli-without-aws-efs)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [EC2](/blog/categories/ec2)
-   [S3](/blog/categories/s3)
-   [Data Transfer](/blog/categories/data-transfer)
-   [AWS CLI](/blog/categories/aws-cli)

Introduction In this post, I will show you how to transfer data between two EC2 instances using AWS CLI, without AWS EFS. We will use an AWS S3 bucket as the middleman. We will create an AWS S3 b

[#AWS S3 Sync](/blog/tags/aws-s3-sync)[#EC2 Data Transfer](/blog/tags/ec2-data-transfer)[#AWS CLI Automation](/blog/tags/aws-cli-automation)+5 tags

[read more](/blog/post/how-to-connect-a-two-ec2-instances-data-transfer-using-aws-cli-without-aws-efs)

[![How To Connect A EBS Volume To An Windows EC2 Instance Using Powershell/GUI](/_astro/hero.CPGBv147_1yKKK8.webp)](/blog/post/how-to-connect-a-ebs-volume-to-an-windows-ec2-instance-using-powershell-gui)

## [How To Connect A EBS Volume To An Windows EC2 Instance Using Powershell/GUI](/blog/post/how-to-connect-a-ebs-volume-to-an-windows-ec2-instance-using-powershell-gui)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [Windows Server](/blog/categories/windows-server)
-   [EC2](/blog/categories/ec2)
-   [EBS](/blog/categories/ebs)
-   [PowerShell](/blog/categories/powershell)

Introduction In this post, we will learn how to connect an EBS volume to a Windows EC2 instance using PowerShell or the Disk Management GUI, and how to mount the volume on the instance. Prereq

[#AWS EBS](/blog/tags/aws-ebs)[#Windows EC2](/blog/tags/windows-ec2)[#Attach EBS Volume](/blog/tags/attach-ebs-volume)+5 tags

[read more](/blog/post/how-to-connect-a-ebs-volume-to-an-windows-ec2-instance-using-powershell-gui)

[![How to Install Jenkins on AWS EC2 Instance](/_astro/hero.BzqOEmzv_YuUyx.webp)](/blog/post/install-jenkins-on-aws-ec2-instance)

## [How to Install Jenkins on AWS EC2 Instance](/blog/post/install-jenkins-on-aws-ec2-instance)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [Jenkins](/blog/categories/jenkins)
-   [EC2](/blog/categories/ec2)
-   [DevOps](/blog/categories/devops)
-   [CI/CD](/blog/categories/cicd)

Introduction In this post, I will show you how to create an EC2 instance on AWS and install Jenkins on it. PrerequisitesAWS CLI installed and configured IAM user with the following permi

[#Jenkins Installation](/blog/tags/jenkins-installation)[#AWS EC2 Setup](/blog/tags/aws-ec2-setup)[#Linux Server](/blog/tags/linux-server)+4 tags

[read more](/blog/post/install-jenkins-on-aws-ec2-instance)

[![How to Connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO](/_astro/hero.BbyjrsxP_xhG8d.webp)](/blog/post/how-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo)

## [How to Connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO](/blog/post/how-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [RDS](/blog/categories/rds)
-   [EC2](/blog/categories/ec2)
-   [PHP](/blog/categories/php)
-   [MySQL](/blog/categories/mysql)
-   [Database](/blog/categories/database)

Introduction In this post, we will learn how to connect an AWS RDS MySQL database to an EC2 instance with PHP, using PDO. PrerequisitesAWS Account EC2 Instance Amazon Linux 2 RDS MySQL

[#AWS RDS MySQL](/blog/tags/aws-rds-mysql)[#EC2 PHP PDO](/blog/tags/ec2-php-pdo)[#Database Connection](/blog/tags/database-connection)+5 tags

[read more](/blog/post/how-to-connect-to-aws-rds-mysql-database-to-ec2-instance-with-php-by-using-pdo)

[![How To Setup Bastion Host on AWS using AWS CLI](/_astro/hero.DGhoBA0r_KL9IM.webp)](/blog/post/how-to-setup-bastion-host-on-aws-using-aws-cli)

## [How To Setup Bastion Host on AWS using AWS CLI](/blog/post/how-to-setup-bastion-host-on-aws-using-aws-cli)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [AWS CLI](/blog/categories/aws-cli)
-   [Security](/blog/categories/security)
-   [Networking](/blog/categories/networking)
-   [DevOps](/blog/categories/devops)

Introduction Security is the top priority for any infrastructure and application, and that's why a Bastion host is a must-have in your infrastructure if you want to secure your remote connections.

[#AWS Bastion Host](/blog/tags/aws-bastion-host)[#AWS CLI](/blog/tags/aws-cli)[#VPC Setup](/blog/tags/vpc-setup)+7 tags

[read more](/blog/post/how-to-setup-bastion-host-on-aws-using-aws-cli)

[![How To Setup Bastion Host on AWS using CloudFormation Template](/_astro/hero.DX_FnUff_Z21DeWl.webp)](/blog/post/how-to-setup-bastion-host-on-aws-using-cloudformation-template)

## [How To Setup Bastion Host on AWS using CloudFormation Template](/blog/post/how-to-setup-bastion-host-on-aws-using-cloudformation-template)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [AWS](/blog/categories/aws)
-   [CloudFormation](/blog/categories/cloudformation)
-   [Security](/blog/categories/security)
-   [Networking](/blog/categories/networking)
-   [DevOps](/blog/categories/devops)

Introduction In the previous post, How To Setup Bastion Host on AWS using AWS CLI, we set up a Bastion host with the AWS CLI. Here we w

[#AWS Bastion Host](/blog/tags/aws-bastion-host)[#CloudFormation Template](/blog/tags/cloudformation-template)[#VPC Setup](/blog/tags/vpc-setup)+7 tags

[read more](/blog/post/how-to-setup-bastion-host-on-aws-using-cloudformation-template)

6 related posts
