Nothing Special   »   [go: up one dir, main page]

Command Line Interface? (CLI)

John Tyree
6 min readOct 29, 2023

--

What is CLI?

A command line interface is a software mechanism that is used to interact with an operating system using a keyboard. CLI text commands are used to configure, navigate, or run programs on any server or computer system. All operating systems including Linux, macOS, and Windows provide a CLI for faster system interaction. CLI can be convenient, instead of going into the AWS console to launch services, services can be launched from a command prompt on a windows PC or terminal on a mac by using a series of text commands.

Step 1: Create a t2.micro EC2 instance with a free tier OS. In the creation of the instance before launching, enter a script in the user-data field to automatically install and run an Apache service. After launching the instance, test the public IP address to verify.

If more details are needed on launching an EC2 instance I will place a previous article written below. Also, I will place a second article below where I did a similar sequence, but instead of a script that automatically runs Apache, I entered separate commands to install and start. Please use both articles if further basic details are needed that aren’t covered in this article.

For the new EC2 instance being launched. I will select a free tier CentOS and t2.micro instance type. I also will be selecting an existing key pair.

An existing security group with inbound rules that open port 22 and port 80. Port 22 allows for “ssh” connection and port 80 allows “HTTP” traffic. If there is no existing security group with these specific inbound rules, please create one before launching the instance.

Next, scroll down under “Advanced details” and locate “user data” and copy and paste a script, upload a file containing the script, or type it in. Once the script is in the field launch the instance.

The script is posted below:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd

Lets Verify!

Take the public IPv4 address, copy and paste it into a private browser.

My results below:

SUCCESS!

Step 2: Make an Amazon Machine Image using the instance created from Step 1. Launch the instance and verify if able to reach the web server from the newly created EC2 instance.

First, navigate to the created instance and select it. Use the “Actions” tab to “image and templates” and “create image” name the image and create.

The created image will be located in the EC2 dashboard under “AMIs” Once selecting the newly created image located under the “owned by me” dropdown. When ready it will read “available”, “launch instance from AMI”

Name the new instance, the AMI is preselected due to it being the image from the previous EC2 instance. Select the same keypair, Select the existing security group that was created earlier. User data will not be entered again due to it being a part of the created AMI.

Lets Verify!

Navigate to instances, the newly created instance from the created AMI should be listed and ready. Pull the public IPv4 address and paste it into a private browser to test.

Different IP address, same SUCCESSFUL results!

Step 3: Complete Step 1 and Step 2 using the AWS CLI

For this step I will be using the installed AWS CLI on my mac. Two links will be posted below; one link will be for installing/updating the AWS CLI, and the second link will be for ways to configure your AWS profile once the AWS CLI has been installed:

Next, navigate to where the user data that will be entered is stored to launch the EC2 instance from the AWS CLI.

Use the cd command to navigate into the directory where the user data is stored

6 pieces of information are needed:

  • Image ID — will be attached to the AMI selected
  • Instance type- t2.micro
  • Security Group ID- the security group selected or created
  • Subnet ID- located in the VPC that the security group is attached to
  • Keypair name- the keypair that is available for use
  • User Data- the file containing the script

Run the command below and enter in the above information in the designated places

aws ec2 run-instances --image-id PLACE AMI ID HERE --instance-type PLACE INSTANCE TYPE HERE --security-group-ids PLACE SECURITY GROUP ID HERE --subnet-id PLACE SUBNET ID HERE --key-name PLEASE KEY PAIR ID HERE--user-data file://EXAMPLE.txt

If successful, there will not be any errors in the CLI, the EC2 instance will begin to launch. Go into the AWS console under the EC2 dashboard to check if there is a new running instance:

AWS CLI command was SUCCESSFUL!

Verify using the public IP address

Test was SUCCESSFUL!

Next, use the AWS CLI to create an image of the new EC2 Instance:

aws ec2 create-image --instance-id PLACE INSTANCE ID HERE --name PLACE DESIRED NAME OF IMAGE HERE --no-reboot 
AWS CLI command was SUCCESSFUL!

Verify the image works the same as the first EC2 by launching another EC2 using the created image

Run the command and replace the previous AMI ID with the newly created image Instance ID:

aws ec2 run-instances --image-id PLACE AMI ID HERE --instance-type PLACE INSTANCE TYPE HERE --security-group-ids PLACE SECURITY GROUP ID HERE --subnet-id PLACE SUBNET ID HERE --key-name PLEASE KEY PAIR ID HERE--user-data file://EXAMPLE.txt

There are now two running instances. The first EC2 launched with the AWS CLI command is named“AWSCLI” and the EC2 launched using the image is named “AWSCLI2”

Verify by taking the IP address of the EC2 launched with the created image into a private browser:

Test was a SUCCESS!

Remember to stop or terminate the EC2 instances to avoid any unnecessary charges. There are two ways to terminate the EC2 instances; by going into the AWS console or by running the command below:

aws ec2 terminate-instances --instance-ids PLACE INSTANCE ID HERE PLACE INSTANCE ID HERE
Both running instances were terminated SUCCESSFULLY!

--

--

No responses yet