DEV Community

Cover image for Configuring and Launching an EC2 Instance: A Comprehensive Guide with Detailed Commands
Tanvir Rahman
Tanvir Rahman

Posted on

Configuring and Launching an EC2 Instance: A Comprehensive Guide with Detailed Commands

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

Image description

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

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

Selecting Instance Type and Key Pair Configuration

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

Image description

  1. 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
Image description

Network and Storage Settings

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

Image description

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

Image description

Launching and Monitoring the Instance

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

Image description

Image description

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

  1. 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/.

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

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.

Top comments (0)