How To Launch An EC2 Instance With Nginx Using AWS CLI

Reading Time: 5 minutes

Hello Readers ! In this blog we’ll see How To Launch An EC2 Instance With Nginx Using AWS CLI . Firstly we’ll see about basic about what is AWS , AWS CLI and Nginx .

So Let’s Get Start

INTRODUCTION

AWS

Amazon Web Services (AWS) is a complete cloud computing platform that consists of infrastructure as a service (IaaS) and platform as a service (PaaS) offerings. AWS offerings provide scalable answers for compute, storage, databases, analytics, and more.

AWS CLI

This is a device you could down load and configure to manipulate a couple of AWS offerings proper out of your command line. We’ll use AWS CLI to begin an AWS Elastic Compute Cloud (EC2) instance. An EC2 can release as many digital servers as you need, configure safety and networking, and control storage.

Nginx

NGINX is open supply software program for internet serving, opposite proxying, caching, load balancing, media streaming, and more. It commenced out as an internet server designed for max overall performance and stability. In addition to its HTTP server capabilities, NGINX also can feature as a proxy.

Steps

With an EC2 instance, we’ll get an NGINX web server up and running using a bootstrap script.

PREREQUISITES:

  • An account with AWS
  • Installed AWS CLI

Now Let’s do this step by step :

STEP 1 — Configure AWS CLI

Before we get began out walking instructions and launching our web server, we’ll need to set our default credentials. We can use the subsequent command to do simply that:

aws configure

You’ll be asked for your AWS Access Key ID, your AWS Secret Access Key, the Default region name, and the Default output format.

STEP 2 — CREATE A SECURITY GROUP

We will need to create a Security Group as our digital firewall for our example to govern inbound and outbound traffic. To create this Security Group we want our VPC (Virtual Private Cloud) ID. Our Security Group could be assigned to this VPC. To discover the VPC ID use the subsequent command:

aws ec2 describe-vpcs

This will allow us to find and copy the VPC ID so that we can add it to our security group setup.

And then we will use this command to create our security group:

aws ec2 create-security-group --group-name <Your_Project_Name> --description 

<Your_Description> --vpc-id <Your_VPC>

This will then give you your Group ID.

Copy this Group ID to use it when needed .

STEP 3 — ADD A RULE TO THE SECURITY GROUP:

Next, we can upload policies to our Security Group on the way to manipulate the inbound site visitors that’s allowed to attain our web server.

We will set our NGINX web server to port 22 and port 80. Port 22 lets in inbound SSH get admission to from IPv4 addresses in our community and port 80 lets in inbound HTTP get admission to from all IPv4 addresses.

To do this we will use these two commands:

aws ec2 authorize-security-group-ingress --group-id <Your_Group_ID> --

protocol tcp --port 80 --cidr 0.0.0.0/0 


AND 


aws ec2 authorize-security-group-ingress --group-id <Your_Group_ID> --

protocol tcp --port 22 --cidr 0.0.0.0/0

Looks like we are set ,

STEP 4 — CREATE A KEY PAIR

A key pair is made from a public key used to encrypt information and a personal key to decrypt information.

The command to create our key pair is:

aws ec2 create-key-pair --key-name <Your_Key_Pair_Name>

And we can check that key pair by using:

aws ec2 describe-key-pairs --key-name <Your_Key_Pair_Name>
STEP 5 — ADD A BOOTSTRAP SCRIPT

Bootstrapping in AWS approach that we can upload instructions or scripts to AWS EX2’s example User Data phase that may be accomplished while the example starts. This script will replace all packages, set up NGINX, and permit and begin the service.

To create this script we can run this command;

nano nginx_script.sh

And once inside the editor we can input this script:

#!/bin/bash

sudo yum update -y

sudo install nginx -y

sudo systemctl start nginx

sudo systemctl enable nginx
STEP 6 — GET YOUR AMI ID

Now we want to discover our AMI (Amazon Machine Image).

To try this we want to visit our AWS EC2 Dashboard. We will pick Launch Instance, scroll right all the way down to the Application and OS Images (Amazon Machine Image) section, and duplicate the AMI ID.

STEP 7 — LAUNCH INSTANCE

We now have all we need to create our EC2 instance. We will use the following command to create our instance.

aws ec2 run-instances --image-id <Your_AMI> --count 1 --instance-type 

t2.micro --key-name <Your_Key_Pair_Name> --security-group-ids 

<Your_Security_Group_ID> --user-data <Your_Script_File>

Now we want to discover our AMI (Amazon Machine Image).

In this command we’re telling AWS we need to begin up an example the usage of our decided on AMI, that we need one example, and that it’s going to be a t2.micro.

We also are such as our key pair, safety group, and our script with inside the User Data area of our example.

Let’s run this command now to test and notice if EC2 is up and running.

aws ec2 describe-instances

Here you can see a partial screenshot of what you’ll see if your instance launched correctly.

STEP 8 — CHECK THE WEB SERVER

Lastly, we’ll want to copy the IP address from our AWS EC2 Dashboard.

And then paste it into a browser to see if it works. If you’ve been successful, you’ll see this image:

Great ! We have Launch An EC2 Instance With Nginx Using AWS CLI.

CONCLUSION

If you accompanied at the side of me, you have been capable of release an EC2 example and begin up an NGINX web server with a bootstrap script, all out of your AWS CLI! Once you’re comfortable with the AWS CLI you’ll be capable of paintings quicker and keep time. Have a few a laugh with it and notice what else you could do.

Happy Learning !!! ALL THE BEST

Thank you

REFERENCE

CLICK HERE

Written by 

Deeksha Tripathi is a Software Consultant at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is DevOps. When not working, she will be busy in listening music , and spending time with her family .