Configuring and Launching an EC2 Instance: A Comprehensive Guide with Detailed Commands
Tags: ec2, devops, aws, codenewbie
Published: 2023-11-25 10:53:43

Launching an Amazon EC2 (Elastic Compute Cloud) instance is a foundational skill for leveraging the AWS cloud platform. This guide will walk you through the steps required to set up, connect to, and manage an EC2 instance, including specific commands for accessing instance metadata.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cymd3nstwqt1ksaqhxow.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzyghuv0b2i4ket12sn7.png)

**Initial Setup: Naming, Tagging, and Image Selection**

1. **Name and Tags Section**: Assign a name to your EC2 instance for easy identification. Tags, defined as Key/Value pairs, aren't mandatory but highly recommended for efficient organization in production environments.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mnmlum8tumu77geah06l.png)

2. **Application and OS Images**: Select 'Amazon Linux' under Quick Start options. Amazon provides a range of AMIs, featuring various versions of Linux and Windows, each pre-configured with essential software packages.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrsujt4627ee3rk4yfly.png)

**Selecting Instance Type and Key Pair Configuration**

3. **Instance Type Selection**: Review the list of available instance types, focusing on their hardware resources like CPU and memory.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cssssx9bnmbfykl35nw7.png)

4. **Key Pair Creation**: Generate a new key pair for secure SSH access. Download the `.pem` file containing your private key after maintaining the default settings.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vi04prtaerx9pez5z6yt.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2b70gmx1d2jswepxfra.png)

**Network and Storage Settings**

5. **Network Settings**: Ensure SSH traffic is allowed from 'Anywhere' in the Security Groups section.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsbjjilt3iu2h4ke7vx1.png)

6. **Configure Storage**: Stick with the default 8 GiB gp3 root volume unless your application demands otherwise.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/as5aesawrj40562zr54n.png)

**Launching and Monitoring the Instance**

7. **Launching the Instance**: Launch your instance after reviewing your configurations. Monitor its deployment on the EC2 console's Instances screen.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/if1u9x802j5cugicoj9c.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihqah0zatstyc8gr1ma0.png)

**Establishing an SSH Connection**

- Set the correct permissions for your key file: `chmod 400 /path/to/your/keypair.pem`.
- Connect to your instance: `ssh -i /path/to/your/keypair.pem ec2-user@server-ip`, where `server-ip` is your instance's Public IP.


**Accessing EC2 Instance Metadata**

Instance metadata provides valuable information about your running instance.

1. **Creating a Token**: Generate a token for secure metadata access: `TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 3600")`.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz42h7agr9qefb70h3ju.png)


2. **Listing Metadata**: Retrieve all instance metadata with the command: `curl -w "\n" -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/`.

3. **Extracting Specific Metadata**: Use the following commands to get detailed information:
   - Security groups: `curl -w "\n" -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/security-groups`
   - AMI ID: `curl -w "\n" -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/ami-id`
   - Hostname: `curl -w "\n" -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/hostname`
   - Instance ID: `curl -w "\n" -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id`
   - Instance type: `curl -w "\n" -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-type`

**Instance Termination Process**

Learn to terminate your instance via the AWS console, an important step for managing costs and resources.

- In the AWS console, navigate to 'Instances', select your instance, and choose 'Terminate instance' from the 'Instance State' dropdown.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvj2e6yfofyxaueo5jrx.png)



This guide provides a thorough understanding of launching and managing an EC2 instance, including detailed commands for accessing instance metadata, ensuring you are well-equipped to utilize this powerful AWS service.