Deploying a Webserver on the top of AWS through CLI

Task Description

➤ Create a key pair

➤ Create a security group

➤ Launch an instance using the above created key pair and security group.

➤ Create an EBS volume of 1 GB.

➤ The final step is to attach the above created EBS volume to the instance you created in the previous steps.

All the above steps must be performed using AWS CLI.

Prerequisite:

  • Must have an aws account
  • create an IAM user.
  • aws CLI installed and configured.

Before starting the article I would like to give some basic ideas about AWS and its service which we will use to deploy a webserver so that you can relate to every bit of this article. So let’s start with the introduction of AWS.

Introduction of AWS

** Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 175 fully featured services from data centers globally. Millions of customers — including the fastest-growing startups, largest enterprises, and leading government agencies — are using AWS to lower costs, become more agile, and innovate faster.

** Amazon Web Services is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis.

** These cloud computing web services provide a variety of basic abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet.

What is aws CLI?

The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

What is a key-pair?

A key pair, consisting of a private key and a public key, is a set of security credentials that you use to prove your identity when connecting to an instance. Amazon EC2 stores the public key, and you store the private key. You use the private key, instead of a password, to securely access your instances. Anyone who possesses your private keys can connect to your instances, so it’s important that you store your private keys in a secure place.

What is a security group?

A security group acts as a virtual firewall for your instance to control inbound and outbound traffic. When you launch an instance in a VPC, you can assign up to five security groups to the instance. Security groups act at the instance level, not the subnet level. Therefore, each instance in a subnet in your VPC can be assigned to a different set of security groups.

What is an instance?

Instances in AWS are basically virtual environments. These virtual environments are isolated from the underlying base OS. It’s an On-demand service, i.e. a user can rent the virtual server(instances) on an hourly base and deploy their applications on it.

What is EBS (Elastic Block Storage)?

Amazon Elastic Block Store (EBS) is an easy to use, high performance block storage service designed for use with Amazon Elastic Compute Cloud (EC2) for both throughput and transaction intensive workloads at any scale. A broad range of workloads, such as relational and non-relational databases, enterprise applications, containerized applications, big data analytics engines, file systems, and media workflows are widely deployed on Amazon EBS.

What is Amazon S3?

Amazon Simple Storage Service is storage for the Internet. It is designed to make web-scale computing easier for developers.

Amazon S3 has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, inexpensive data storage infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.

What is Amazon CloudFront?

Amazon CloudFront is a web service that speeds up distribution of your static and dynamic web content, such as .html, .css, .js, and image files, to your users. CloudFront delivers your content through a worldwide network of data centers called edge locations. When a user requests content that you’re serving with CloudFront, the user is routed to the edge location that provides the lowest latency (time delay), so that content is delivered with the best possible performance.

Step-1) Install AWS CLI and configure it.

  • Here I am installing awscliv2 on Redhat Linux.
  • Follow the given link to install it.
  • After installing confirms it.
aws --version
  • Now we need to configure AWS CLI.
aws configure
  • Now you need to type your access and secret key of IAM user.
  • After type region name.

Step-2)Create a key pair

  • Type the following command:
aws ec2 create-key-pair --key-name 'mykey' --query 'KeyMaterial' --output text > mykey.pem

Step-3)Create a security group

  • Type the following command:
aws ec2 create-security-group --description "Allow 22 & 80 Ports" --group-name "MySG"
  • Allow Port 22 for accessing the shh protocol which you will use to login with your instance remotely.
aws ec2 authorize-security-group-ingress --group-name "MySG" --protocol "tcp" --port 22 --cidr "0.0.0.0/0"
  • Allow port 80 to access the Webserver.
aws ec2 authorize-security-group-ingress --group-name "MySG" --protocol "tcp" --port 80 --cidr "0.0.0.0/0"

Step-4)Launch an instance using the above created key pair and security group.

  • Type the following command:
aws ec2 run-instances --image-id ami-0e306788ff2473ccb --instance-type "t2.micro" --key-name "mykey" --security-groups "MySG"
  • Use the below command to create a tag for instance so that you can easily find out your instance.
aws ec2 create-tags --resource instance-id --tags Key=Name,Value=WebServer

Step-5)Create an EBS volume of 1 GB.

  • Type the following command:
aws ec2 create-volume --availability-zone "ap-south-1a" --size 1

Step-6)Attach the above created EBS volume to the instance you created in the previous steps.

  • Type the following command:
aws ec2 attach-volume --device "xvdb" --instance-id "id of instance" --volume-id "vol-0b3306753e956ac19
  • Now EBS Volume has been attached with instance now we have to log in with the instance with help of ssh protocol. So that we can mount the volume as well as do all those things which we will need to deploy the webserver.

Step-7)Create partition , format it and mount on /var/www/html

  • Now log in to your EC2-instance.
  • Type the following commands:
fdisk -l
  • Here we can see our EBS has been attached.
fdisk /dev/xvdb
  • Creating partition.
mkfs.ext4 /dev/xvdb1
  • Here I am formatting the partition type ext4.
mount /dev/xvdb1 /var/www/html
  • After formatting, we need to mount on /var/www/html for making our web code persistent.

Step-7)Create an S3 bucket for static content

  • Type the following code for creating an S3 bucket.
aws s3 mb s3://cli-webserver

Step-8) Upload content in the S3 bucket

  • Type the following code for creating an S3 bucket.
aws s3 cp . s3://cli-webserver --recursive --acl public-read

Step-9) Setting up Content Delivery Network using CloudFront and using the origin domain as an S3 bucket.

  • Type the following code for creating an S3 bucket.
aws cloudfront create-distribution --origin-domain-name cli-webserver.s3.amazonaws.com

Step-10) Place the Cloud Front URL on the web app code for security and low latency.

  • Now we need to use the domain name provided by CloudFront.

Step-10) Open the url to get your webpage.

  • As you can see the data is coming from S3 using Cloudfront.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Codeless Future — Myths & Reality

Developer’s Toolkit: 4 linux command-line tools for debugging everyday issues that all developers…

HackTheBox: Previse

New Coder? Stop Thinking, Start Doing.

The Best Project Management Tool for Slack Teams

RecyclerView

Level Up as a Software Engineer by Writing a Chess Engine

Become a Website Developer…but why?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Skabhi

Skabhi

More from Medium

Tips on how to communicate with the business CFO meets CIO

How to Install Redis on Ubuntu 18.04?

Command And Conquer