> ## 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.

# Create an AWS EC2 Instance and run some 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/create-an-aws-ec2-instance-and-run-some-aws-cli-commands" target="_blank" rel="noreferrer">
  Open IP Lab Portal
</a>

## Overview

### Lab details

1. This Lab walks you through the steps to create an EC2 instance and run a few AWS CLI Commands.
2. Duration: **45 minutes**
3. AWS Region: **US East (N. Virginia) us-east-1.**

### Architecture diagram

<img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/001.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=85afe9a0310a48fbd7d9cfcb96d7eaca" alt="" width="1700" height="780" data-path="images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/001.png" />

### Task Details

1. Sign into the AWS Management Console.
2. Launch an EC2 instance.
3. SSH into the EC2 Instance.
4. AWS CLI Command to create a KeyPair.
5. AWS CLI Command to create a Security group.
6. AWS CLI Command to create an EC2 Instance.
7. View the EC2 instance.
8. Validation of the lab
9. AWS CLI Command to terminate the EC2 instance.

### 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 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 **Username** and **Password** in the Lab Console to the **IAM Username and Password** in 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.**

#### Task 2: Launching an EC2 Instance

1. Make sure you are in the **N.Virginia** Region.
2. Navigate to **Services** menu in the top, then click on **EC2** in the **Compute** section.
3. Click on **Instances** from the left sidebar and then click on **Launch Instances**
4. Name : Enter **MyEC2Instance**
5. **For Amazon Machine Image (AMI):** Search for **Amazon Linux 2023 AMI** in the Quick Start menu.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/002.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=b69efc7cb58961658576a0fb41835feb" alt="" width="1222" height="796" data-path="images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/002.png" />

6. **For Instance Type:** select **t2.micro**

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/003.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=e74b4d77fb426ed45eabb6762bfda749" alt="" width="1222" height="279" data-path="images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/003.png" />

7. **For Key pair:** Select **Create a new key pair** Button

   * Key pair name: **MyEC2-key**
   * Key pair type: **RSA**
   * Private key file format: **.pem**

8. Select **Create key pair** Button.

9. In Network Settings Click on **Edit**:

   1. Auto-assign public IP: **Enable**
   2. Select **Create new Security group**
   3. Security group name : Enter **MyEC2-SG**
   4. Description : Enter **Security Group to allow traffic to EC2**

* To add **SSH**,

  * Choose Type: **SSH**
  * Source: **Anywhere** (From ALL IP addresses).

10. Expand **Advanced details :**

    * In IAM instance profile Select **EC2LimitedLaunchRole**
11. Keep rest thing Default and Click on **Launch Instance** Button.
12. Select **View all Instances** to View the Instance you Created
13. Launch Status: Your instance is now launching. Click on the instance ID and wait until the initialization status changes to running.

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/004.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=fed877d8858c215d1465b67285d96c3a" alt="" width="1222" height="108" data-path="images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/004.png" />

#### Task 3: SSH into the EC2 Instance

* Please follow the steps in [**SSH into EC2 Instance**](/aws-cp/support/ssh-into-ec2-instance)**.**

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

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

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

* Run this command, to set necessary permissions.

  ```
  chmod 400 MyCLIKeyPair.pem
  ```

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

* 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
```

* Once you get the below output, you have successfully created a Security Group.

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/005.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=e806ce119898092381bc6be6fc2db488" alt="" width="2516" height="210" data-path="images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/005.png" />

#### Task 6: AWS CLI command to create an EC2 Instance

* The below command will create one t2.micro EC2 instance with Amazon Linux 2 AMI in the us-east-1 region.

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

<img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/006.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=423cd5c63140859280219b9da870ce43" alt="" width="2870" height="1536" data-path="images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/006.png" />

#### Task 7: View the EC2 instance you created

1. Make sure you are in the **N.Virginia** Region.
2. Navigate to **Services** menu in the top, then click on **EC2** in the **Compute** section.
3. Navigate to **Instances** in the left panel.
4. You will be able to see the new EC2 instance has been created. Copy the instance ID for later use.

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/007.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=3c41107f1cf4e2e24afb29b6248cb9b4" alt="" width="1222" height="107" data-path="images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/007.png" />

<Tip>
  **Do you know?**

  AWS CLI's integration with EC2 showcases the prowess of command-line tools in modern cloud infrastructure management. It empowers users to harness the full potential of AWS services while offering a robust and versatile platform for automating, managing, and scaling EC2 instances with ease.
</Tip>

#### Task 8 : Validation Test

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/TSQ7Mvha7YyTZ0k2/images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/008.gif?s=5c7ea9b26760c52dabf5ca836223a20e" alt="" width="860" height="514" data-path="images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/008.gif" />

#### Task 9: AWS CLI command to Delete the EC2 instance

The below command will terminate the EC2 instance which was previously created.

```
aws ec2 terminate-instances --instance-ids <Enter your Instance ID> --region us-east-1
```

**Note: Replace the \<Enter your instance ID> with the instance ID of new instance you have created.**

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

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/009.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=7dc2bffb96679d470cdd6879ff9b5fc1" alt="" width="2156" height="552" data-path="images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/009.png" />
* Now navigate to your EC2 dashboard, and you will be able to see that the EC2 instance state changed to **terminated.**

  <img src="https://mintcdn.com/ip-cloud-architect-pathway/TSQ7Mvha7YyTZ0k2/images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/010.png?fit=max&auto=format&n=TSQ7Mvha7YyTZ0k2&q=85&s=227ad4289ab1564d369bdfe5d4bee7ce" alt="" width="1222" height="69" data-path="images/labs/create-an-aws-ec2-instance-and-run-some-aws-cli-commands/010.png" />

### Completion and conclusion

* You have successfully created and launched an Amazon EC2 Instance.
* You have successfully logged into an EC2 instance by SSH.
* You have successfully created a key pair via AWS CLI command.
* You have successfully created a Security group via AWS CLI command.
* You have successfully created an EC2 Instance via AWS CLI command.
* You have successfully terminated the EC2 Instance via AWS CLI command.

### 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:

* **Launch an EC2 Instance** — Check whether an EC2 Instance is launched or not.
* **Launch EC2 AMI type Amazon Linux** — Check whether the EC2 instance is launched using an Amazon AMI.

## Related help

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