Installing NGINX using AWS CLI

Johncle
5 min readJul 11, 2022

--

Overview:

In this article, we will be utilizing the free tier of an EC2 instance. This EC2 instance will have NGINX installed and configured by using AWS CLI.

To give your mind a quick of idea things we need, look over the items in BOLD in this command below. This command lists the general requirements that we need to launch the instance. Aside from configuring your environment.

aws ec2 run-instances \
--image-id <AMI_ID> \
--count 1 \
--instance-type t2.micro \
--key-name <Key_Pair> \
--security-group-ids <Security_Group_ID> \
--user-data file://<Script_Name>

Step 1:Installing AWS CLI

First let’s get AWS CLI installed. Instructions for installing AWS CLI can be found here.

Once you have installed AWS CLI. We can validate that we have AWS installed by running the following command.

aws --version
This shows a successful install

Step 2: Setup AWS environment

Assuming this is your first time using AWS CLI on your system. You will need to setup your environment by running the following command.

aws configure

Mine has already been setup and you need to enter your own access key and Secret Access key for your IAM user.

Step 3: Creating and configuring our security group

We will need to create our security group, which acts as firewall that controls traffic. This will be used to secure our EC2 instance. Inside our security group we have rules that control ingress and egress traffic.

To create one, we first need to get our VpcID which is our Virtual Private Cloud ID that will be tied to our security group.

Running the following command will show us our all our VPC’s

aws ec2 describe-vpcs
Here we can see the VpcID, that we will use

Now we need to create our security group using the following command:

aws ec2 create-security-group --group-name <Security_group_name> --description <Description> --vpc-id <VPC_ID>

Once we create our security group, it provides us with our GroupID.

When we create our Security Group rules, we will assign it to this GroupID.

We’re going to open inbound port 80 for HTTP access and SSH access. HTTP access will only allowed for everyone and SSH access will only be limited to my ip.

The following command will give you your public ip address.

curl ifconfig.me

Now run these commands to add the rules to your Security group:

aws ec2 authorize-security-group-ingress --group-id <GroupID> --protocol tcp --port 80 --cidr 0.0.0.0/0aws ec2 authorize-security-group-ingress --group-id <GroupID> --protocol tcp --port 22 --cidr <My_IP_Address>/32

We can see here that that our Return value is set to true. This signifies that our request has succeeded.

Step 4: Bootstrapping our AMI

In order to bootstrap our AMI, we need to add our script to the user data section. Once we launch the instance, the script will get initiated to perform the actions within the script. In our case, it will update the system, install NGINX, enable and start the service on startup. We’ll create a file to hold our script first.

vim nginx_install.sh

We’ll put in the following into our script

#!/bin/bash
yum update -y
amazon-linux-extras install nginx1 -y
systemctl start nginx
systemctl enable nginx

Since this is bootstrapped with no user interaction, we need to make sure to use the -y command so that it answers yes for all prompts. Also notice that sudo does not need to be as user scripts run as root.

Step 5 : Setting up your Key Pair

Next we need to setup our key pair to allow us to login to our instance. We can create a keypair using the following commands.

aws ec2 create-key-pair \
--key-name <Key_name> \
--query "KeyMaterial" \
--output text > <Key_Name>

Now we need to setup our permissions on the keypair.

chmod 400 <Key_Pair_Name>

Step 6: Launching our EC2 Instance

One last step before we launch our EC2 instance is that we need to find the AMI ID that we plan to launch.

The easiest way to find it is to go to the AWS Console -> EC2 -> Launch Instance. Select the image you want to use and grab the AMI ID.

Now, we have everything that we need to launch our instance.

Run the following with your settings to launch your EC2 instance with CLI.

aws ec2 run-instances \
--image-id <AMI_ID> \
--count 1 \
--instance-type t2.micro \
--key-name <Key_Pair> \
--security-group-ids <Security_Group_ID> \
--user-data file://<Script_Name>

After running this command, you should see the instance created and in a running state.

Now let’s grab our public ip address of the instance and see if NGINX is installed.

Success! We have successfully launched an EC2 instance from AWS CLI.

Stay tuned for new guides on from me and don’t forget to follow me!!

--

--