How to Launch an EC2 Instance in AWS: Step-by-Step Guide with AWS CLI Setup

How to Launch an EC2 Instance in AWS: Step-by-Step Guide with AWS CLI Setup

Introduction

Amazon Web Services (AWS) provides cloud-based virtual machines (VMs) known as EC2 (Elastic Compute Cloud) instances. These instances allow users to deploy and run applications on the cloud. This blog will cover:

  • Creating an EC2 instance in AWS

  • Writing scripts to automate EC2 management

  • Installing and configuring AWS CLI


1. Creating an EC2 Instance in AWS

1. Log in to AWS Console

  1. Navigate to AWS Console

  2. Sign in with your credentials.

2. Launch an EC2 Instance

  1. Search for EC2 in the AWS services search bar and click on EC2.

  2. Click on the dashboard and Launch Instance.

  3. Enter an instance name.

  4. Choose an Amazon Machine Image (AMI), such as Ubuntu, Amazon Linux, or Windows Server.

  5. Select an instance type (e.g., t2.micro for free-tier users).

  6. Create and Configure Key Pair

    • The key pair is crucial for accessing your EC2 instance securely.

    • Without the private key file, you cannot SSH into the instance, and access will be lost.

    • If you already have a key pair, select an existing one. Otherwise, create a new key pair:

      1. Click on Create new key pair.

      2. Provide a name for the key pair.

      3. Choose the key format (.pem for Linux/macOS or .ppk for Windows ).

      4. Download and store the key file securely.

Note :- Once you Delete or lose the file you cant access into your ssh and also you wont able to download it again. so make sure to keep it safe in a folder

  1. Set up the security group (allow SSH, HTTP, or other required ports).

Click Launch Instance.

3. Connect to the EC2 Instance

Once the instance is running:

  1. Click on the instance and find the Public IPv4 DNS.

  2. Open a terminal and connect via SSH:

     ssh -i /path/your-key.pem ubuntu@your-ec2-ip
    

    (Replace your-key.pem with your actual key file and your-ec2-ip with the instance's IP.)

NOTE :- If it Denies Permission then Run this command

chmod 600 /path/your-key.pem

Writing Scripts for EC2 Automation

Types of Scripts for AWS Automation

  1. Shell Scripts (Bash for Linux/macOS, PowerShell for Windows)

  2. AWS CLI Scripts (Command-line automation)

  3. Python Scripts (Boto3) (Programmatic access using AWS SDK)

  4. TypeScript Scripts (AWS SDK for JavaScript/Node.js) (For modern cloud automation)

Installing and Configuring AWS CLI

1. Install AWS CLI

For Linux & MacOS

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /

For Windows

Download and install AWS CLI from AWS CLI Installer.

2. Verify Installation

Check if AWS CLI is installed:

aws --version

To get your AWS Access Key ID and Secret Access Key, follow these steps:


1. Sign in to AWS Management Console

  1. Go to AWS Management Console.

  2. Sign in with your AWS account.

2. Create a New Access Key

For Root User

3. Store Your Credentials Securely

4. Configure AWS CLI

After installation, configure AWS CLI with your credentials:

aws configure

Enter the required details:

  • AWS Access Key ID

  • AWS Secret Access Key

  • Default region name (e.g., us-east-1)

  • Default output format (json, table, or text)


Conclusion

This guide covered:

  • How to create an EC2 instance in AWS

  • Importance of key pairs and how to create them

  • Installing and configuring AWS CLI

Using AWS CLI and automation scripts, you can efficiently manage EC2 instances and scale your infrastructure with ease.