> ## Documentation Index
> Fetch the complete documentation index at: https://cloud-architect.ipoint-labs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Use AWS CloudShell and run AWS CLI Commands

> Hands-on lab · 45m

Launch this lab in the IP Lab Portal, then follow the steps below in the AWS console.

<a className="ip-lab-portal-btn" href="https://labs.intellectualpoint.com/labs/use-aws-cloudshell-and-run-aws-cli-commands" target="_blank" rel="noreferrer">
  Open IP Lab Portal
</a>

## Overview

### Lab details

1. This lab guides you through the process of utilizing AWS CloudShell to execute CLI commands. The objective is to create an EC2 instance, establish an SSH connection to the instance, and subsequently install the Apache web server.
2. Duration of the Lab: **45 minutes**
3. **AWS Region: US East (N. Virginia) us-east-1**

### Introduction

#### What is AWS Cloud Shell?

AWS CloudShell is a browser-based shell that makes it easy to manage, explore, and interact securely with your AWS resources. With CloudShell, you can quickly run scripts with the AWS Command Line Interface (AWS CLI), experiment with AWS service APIs using the AWS SDKs, or use a range of other tools

#### Advantages :

1. CloudShell inherits the credentials of the user signed in to the AWS Management Console.
2. A fully managed Amazon Linux 2 environment that has the latest versions of popular tools already installed and updated.
3. With 1 GB of persistent storage per Region, you can store scripts, files, configuration preferences, etc in your home directory.

### Architecture diagram

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/001.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=0f233e417ae750b9ce17450a91bc607f" alt="" width="3298" height="1826" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/001.png" />

### Task details

1. Sign into AWS Management Console
2. Create an Environment in CloudShell
3. AWS CLI Command to create a KeyPair
4. AWS CLI Command to create a Security group
5. AWS CLI Command to launch an EC2 Instance
6. SSH into EC2 and install Apache
7. Validation of the Lab
8. Deleting AWS Resources.

### Launching the lab environment

1. To launch the lab environment, Click on the **Launch lab** button.

2. Please wait until the cloud environment is provisioned. It will take less than a minute to provision.

3. Once the Lab is started, you will be provided with **IAM user name**, **Password**, **Access** **Key**, and **Secret** **Access** **Key**.

<Note>
  You can only start one lab at any given time
</Note>

## Lab guide

### Lab steps

#### Task 1: Sign in to the AWS Management Console

1. Click on the **Open console** button, and you will get redirected to AWS Console in a new browser tab.
2. On the AWS sign-in page,

* Leave the Account ID as default. Never edit/remove the 12-digit Account ID present in the AWS Console. otherwise, you cannot proceed with the lab.
* Now copy your **User Name** and **Password** in the Lab Console to the **IAM Username and Password** in the AWS Console and click on the **Sign in** button

3. Once Signed In to the AWS Management Console, Make the default AWS Region as **US East (N. Virginia) us-east-1.**

**Note: There is no Validation function for this lab.**

#### Task 2: Create an Environment in CloudShell

1. Make sure you are in the **N.Virginia** Region.
2. Click on the **Cloud Shell icon** on the top right AWS menu bar.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/002.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=85e8d4ae8bcbeac7fe7e1014aaa7a392" alt="" width="1577" height="610" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/002.png" />

3. You will see a creating environment message on the screen.

4. Wait for a few minutes to complete the environment creation. Once the environment is created, You are ready to use the terminal.

#### Task 3: AWS CLI command to create a Key Pair

1. ?To configure credentials paste the below command and provide credentials from lab overview page.

aws configure

2. Provide access key, secret key and region as **us-east-1** and for Default output format hit enter.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/003.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=ee57c6b112b57789bd3e1acd830cb532" alt="" width="604" height="94" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/003.png" />

3. This command will create a Key Pair in the us-east-1 region.

```
aws ec2 create-key-pair --key-name MyEC2Key --query 'KeyMaterial' --output text > MyEC2Key.pem --region us-east-1
```

* **Note**: If you try to execute the command more than once, it will throw an error that the MyEC2Key.pem file exists.

4. You won't get any output for this CLI Command, once you have successfully created a key pair.

5.The above command creates a Key pair and stores the key in a PEM file. You can list all the directories and files to view the file.

ls

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/004.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=f5ef7281f36cba3546efdfd27838f65d" alt="" width="981" height="60" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/004.png" />

#### Task 4: AWS CLI command to create a Security Group

1. The below command will create a Security Group in the us-east-1 region.

```
aws ec2 create-security-group --group-name my-sg --description "My security group" --region us-east-1
```

2. You will have a Group ID as output for this command, which means you have successfully created a Security Group. Type **wq** if **:** appears

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/005.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=4434f623841ccaf7c81e4f45f0d76100" alt="" width="882" height="92" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/005.png" />

3. Add SSH port rule in inbound

* Syntax :

  ```
  aws ec2 authorize-security-group-ingress --group-id <SecurityGroup Id> --protocol tcp --port 22 --cidr 0.0.0.0/0
  ```
* Replace the Security Group ID with the Group ID you got in the above step to create a security group.
* Example : **aws ec2 authorize-security-group-ingress --group-id sg-023ce9d45c379afbe --protocol tcp --port 22 --cidr 0.0.0.0/0**

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/006.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=3e869f3e7215462f93ab9a30251f3e82" alt="" width="990" height="301" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/006.png" />

4. Add HTTP port rule in inbound

   * Syntax :

     ```
     aws ec2 authorize-security-group-ingress --group-id <SecurityGroup Id> --protocol tcp --port 80 --cidr 0.0.0.0/0
     ```
   * Replace the Security Group ID with the Group ID you got in the above step to create a security group.
   * Example : **aws ec2 authorize-security-group-ingress --group-id sg-023ce9d45c379afbe --protocol tcp --port 80 --cidr 0.0.0.0/0**

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/007.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=f028464905169cb68bdccc719ba3a1f7" alt="" width="985" height="290" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/007.png" />

#### Task 5: AWS CLI command to launch an EC2 Instance

1. The below command will create one t2.micro EC2 instance with **Amazon Linux 2023 kernel-6.1** AMI in the us-east-1 region. Type **wq** if **:** appears.

   ```
   aws ec2 run-instances --image-id  ami-0150ccaf51ab55a51 --count 1 --instance-type t2.micro --key-name MyEC2Key --security-groups my-sg --region us-east-1
   ```

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/008.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=765ed6d33b913b2aa8862d111641d7bb" alt="" width="1310" height="536" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/008.png" />

#### Task 6: SSH into EC2 and install Apache

1. Navigate to **EC2** by clicking on the **Services** menu at the top, then click on **EC2** in the **Compute** section.

2. Navigate to **Instances** in the left panel.

3. You will be able to see the EC2 instance which you have launched through CLI Command.

4. Click on the instance ID and copy the **IPv4 Public IP** address of your instance and place it in your text editor

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/009.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=152a5ecfa799871dffd794008af43bec" alt="" width="1581" height="565" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/009.png" />

5. Navigate back to the CloudShell browser tab.

6. Change the permission of KeyPair.

```
chmod 400 MyEC2Key.pem
```

7. To SSH into the EC2 instance, copy & paste the following command and replace the IP address with your EC2’s Public IP address.

* Syntax :

  ```
  ssh ec2-user@<IPv4 Public IP> -i MyEC2Key.pem
  ```
* Example : **ssh ec2-user\@52.23.205.128 -i MyEC2Key.pem**
* Here’s the output :

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/010.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=8c8567925214aac7c830687e397e3297" alt="" width="696" height="342" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/010.png" />
* If you get a prompt to confirm the connection enter **yes**

8. Install Apache and Add a webpage (run the commands one by one).

```
sudo su
```

```
sudo dnf install httpd -y
```

```
echo "<html><h1> Welcome to Whizlabs Server 1 </h1><html>" > /var/www/html/index.html
```

```
systemctl start httpd
```

```
systemctl enable httpd
```

9. Now open a new tab in your browser, copy and paste the IPv4 Public IP of your Instance, and hit \[enter]. You will be able to see the below web page.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/011.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=4e797c06b6d4d68324e69414a00e049c" alt="" width="645" height="355" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/011.png" />

10. Now to return to CloudShell for your EC2 Instance, Enter **exit** and hit \[Enter] again Enter **exit** and hit **\[Enter]**.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/012.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=09720eeb36e283d0089ca8df29b914d1" alt="" width="379" height="119" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/012.png" />

<Tip>
  **Do you know?**

  The AWS CLI allows you to perform actions even without an active internet connection by using local caching and offline mode, which can be helpful in certain scenarios.
</Tip>

#### Task 7: Validation of the Lab

1. Once the lab steps are completed, please click on the **Check my work** button on the left side panel.
2. This will validate the resources in the AWS account and displays whether you have completed this lab successfully or not.
3. Sample output :

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/013.gif?s=ec8b199f74296acae178dc3c7cb7d061" alt="" width="1000" height="612" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/013.gif" />

#### Task 8: Delete AWS Resources

##### 7.1 AWS CLI command to Delete the EC2 instance

1. Use the **aws configure** to locate the credentials. Paste the Access Key and Secret Key. These keys are available under the **Open console** button while starting the lab.
2. Syntax :

   ```
   aws ec2 terminate-instances --instance-ids <instance id> --region us-east-1
   ```
3. Example : **aws ec2 terminate-instances --instance-ids i-0557e626ed160301b --region us-east-1**

**Note: Replace the instance ID with your ID.**

4. Once you see the below output, your EC2 instance has started to terminate.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/014.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=74298c331131f6543fe4d6f28fa5edaf" alt="" width="370" height="242" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/014.png" />

5. Now navigate to your EC2 dashboard and you will be able to see that the EC2 instance state changed to **Shutting-down/ Terminated.**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/iAkc8cvYvGsOYFCQ/images/labs/use-aws-cloudshell-and-run-aws-cli-commands/015.png?fit=max&auto=format&n=iAkc8cvYvGsOYFCQ&q=85&s=6133102fd5bf16efa299961049f9d3b7" alt="" width="781" height="76" data-path="images/labs/use-aws-cloudshell-and-run-aws-cli-commands/015.png" />

##### 8.2 Deleting the CloudShell directory

1. Navigate to the CloudShell tab.
2. Click on the **Actions** button on the top right corner and select Delete AWS CloudShell home directory.
3. Enter **delete** in the text input field and click on the **Delete** button and close the browser tab.

### Completion and conclusion

* You have successfully created an Environment in CloudShell.
* You have successfully created a key pair via the AWS CLI command.
* You have successfully created a Security group via the AWS CLI command.
* You have successfully created an EC2 Instance via the AWS CLI command.
* You have successfully SSH into the EC2 and installed Apache.
* You have successfully terminated the EC2 Instance via the AWS CLI command.
* You have successfully deleted the CloudShell directory.

### End lab

1. Sign out of AWS Account.
2. You have successfully completed the lab.
3. Once you have completed the steps click on **End lab** in the IP Lab Portal.

## What gets checked

When you press **Check my work**, the platform verifies each of these:

* **Install Apache Web Server** — Check whether Apache Web Server is installed in EC2 Instance or not.

## Related help

* [FAQs and Troubleshooting](/aws-cp/support/faqs-and-troubleshooting)
* [SSH into EC2 Instance](/aws-cp/support/ssh-into-ec2-instance)
