How to Configure the webserver on Amazon EC2 instance using AWS CLI

Anu Chundawat
5 min readSep 3, 2021

Anu Chundawat

May 20·4 min read

We will launch EC2 instance by using AWS CLI.

AWS CLI is one of the ways to interact with AWS to use AWS services. It is useful in automation. AWS CLI is used in real-world to use, AWS services because we can implement automation using AWS CLI.

Now First of all we need to setup aws CLI on over system.

To download and install AWS CLI:

Download the AWS CLI for windows from the link https://awscli.amazonaws.com/AWSCLIV2.msi. after download, install it. then go to cmd to check the AWS is installed or not.

aws --version

Now to use AWS account we need to give credential Go to the AWS IAM service in aws portal in your account and click add user:

Give the programmatic access and then click next and attach a policy.

Now click next and create this user. we will use the credential of this user in cmd.

Go to CMD

aws configure

Now run above command, it will ask for credential and give those credential after it we can use aws from our command line.

Now we can check how to use it by help command with aws.

aws help

then check it is installed or not in the command prompt :

aws --version

Now we need to login to the AWS account in CMD using the access key and secret access key from the AWS portal.

Get the credentials from AWS by creating a using having programmatic access and attach policy by which it can access was services. you can attach power user access if you not aware of the policies.

After creating a user you will get an access key and secret access key which you can use to login in to AWS through AWS CLI.

let move to the command prompt and give the credential using given command:

aws configure

it will ask you to provide access key and secret access key give them and also you can provide default region id and default output type if we do not give any output type then by default it will use JSON format.

aws ec2 run-instances --image-id <image_id> --instance-type <type_of_instance> --count <number_of_instances> --subnet-id <subnet_id> --security-group-ids <security_group_id> --key-name <name_of_key>

Run above command it will launch an instance on AWS.

To configure webserver we have to follow the following steps:

Connect to AWS instance from AWS portal by connecting option or you can use putty software to connect.

To configure webserver we have to follow the following steps:

  1. install httpd software.
  2. start the httpd service.
  3. create webpage in /var/www/html folder .
yum install httpd
systemctl start httpd
cd /var/www/html
vim index.html

index.html

<h1> hello these is a webserver </h1>

Now We can make our document root durable/persistent by using EBS volume.

To make our data persistent we can mount /var/www/html folder to the EBS block volume. Let first, create EBS volume and attach it to created EC2 instance .

aws ec2 attach-volume --device /dev/sdf --volume-id <ebs_volume_id> --instance-id <instance_id>

Now create partitions in new EBS Volume and formate it and then mount it to folder /var/www/html

Now, format the created partition:

Then mount the created partition to /var/www/html folder which is document root :

mount /dev/xvdf1 /var/www/html

Now, whatever data we stores in /var/www/html folder. it will also stored in a EBS volume so our data will durable.

Now Using the public IP of EC2 instances we can connect to this URL:

Now Using the public IP of EC2 instances we can connect to this URL:

http://<public_ip>/index.html

Thank you for reading

--

--