DEV Community

Cover image for Launching EC2 Instances with AWS CLI and Advanced Features
Sushant Gaurav
Sushant Gaurav

Posted on

Launching EC2 Instances with AWS CLI and Advanced Features

In the previous article, we explored the various EC2 instance types and how to choose the right one for your workload. Now that you are familiar with EC2 instances, it’s time to understand how to launch EC2 instances, configure advanced features like Elastic Load Balancing (ELB) and Auto Scaling, and optimize your infrastructure for scalability and availability.

In this article, we’ll cover:

  1. How to launch an EC2 instance using the AWS Management Console and AWS CLI.
  2. Key concepts behind Elastic Load Balancing and Auto Scaling.
  3. How to configure and optimize EC2 instances for better performance, scalability, and cost optimization.

By the end of this article, you’ll be equipped to launch your EC2 instance using both the AWS Console and AWS CLI, and you'll understand how advanced features like Elastic Load Balancing and Auto Scaling can improve your application's scalability and availability.

Launching an EC2 Instance via AWS Management Console

Let’s first walk through the process of launching an EC2 instance using the AWS Management Console, the web interface that provides an easy-to-use interface for managing EC2 instances.

Step-by-Step Guide to Launching an EC2 Instance (Console)

  1. Sign in to AWS Console: Begin by navigating to the AWS Management Console and signing in with your AWS credentials.
  2. Navigate to EC2: In the AWS Console, search for EC2 in the search bar and select EC2 from the Services menu.
  3. Launch Instance:
    • On the EC2 Dashboard, click on the Launch Instance button to start the process.
  4. Choose an AMI (Amazon Machine Image):
    • You will be prompted to select an AMI (Amazon Machine Image). An AMI is a pre-configured operating system image for your instance, such as Amazon Linux 2, Ubuntu, or Windows. Select the one that best fits your needs.
  5. Choose an Instance Type:
    • Choose the EC2 instance type that fits your workload requirements. For example, if you need a lightweight instance for testing or development, you might opt for a t3.micro instance.
  6. Configure Instance:
    • Set configurations such as Network, Subnet, and IAM Role (if needed). For basic configurations, the default settings are usually fine, but you can customize them based on your needs.
  7. Add Storage:
    • You can add additional EBS (Elastic Block Store) volumes to your instance to increase storage. Typically, EC2 instances come with a default root volume, but you may need extra storage for your application.
  8. Add Tags:
    • Tags help in identifying and organizing resources in AWS. You can create tags with key-value pairs, such as Name=MyInstance.
  9. Configure Security Group:
    • A Security Group acts as a virtual firewall for your instance. You can configure rules to allow inbound traffic, such as SSH for Linux instances (port 22) or RDP for Windows instances (port 3389).
  10. Review and Launch:
    • After reviewing your instance settings, click on Launch. You will be prompted to select an existing Key Pair (or create a new one) for SSH access to your instance.
  11. Connect to the Instance:

    • Once the instance is running, you can connect to it using SSH for Linux-based instances or RDP for Windows-based instances. If you are using Linux, you can access it using the following command:
      ssh -i MyKeyPair.pem ec2-user@<Public-IP>
    

Launching an EC2 Instance via AWS CLI

The AWS CLI provides a powerful way to manage AWS resources programmatically. Let’s walk through the process of launching an EC2 instance using the AWS Command Line Interface.

Pre-requisites:

  • Ensure that the AWS CLI is installed. You can install it using pip (pip install awscli) or by downloading it from the AWS CLI download page.
  • Configure your AWS CLI by running aws configure and setting your AWS credentials (Access Key, Secret Key), default region, and output format.

Steps to Launch an EC2 Instance with CLI:

  1. Choose an AMI:

    • First, you need to find the AMI ID for the operating system you want to use. You can list available AMIs by running:
     aws ec2 describe-images --owners amazon --filters "Name=platform,Values=Linux"
    
  2. Launch EC2 Instance:

    • After selecting your AMI, use the following AWS CLI command to launch an EC2 instance:
     aws ec2 run-instances --image-id ami-12345678 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-12345678 --subnet-id subnet-12345678
    
  • Replace the placeholders:
    • ami-12345678: Your chosen AMI ID.
    • t2.micro: The instance type you wish to launch.
    • MyKeyPair: Your existing SSH key pair name.
    • sg-12345678: Your security group ID.
    • subnet-12345678: The subnet ID in which to launch the instance.
  1. Monitor the Instance:

    • To monitor the status of your instance, run:
     aws ec2 describe-instances --instance-ids i-12345678
    
  2. SSH Access:

    • Once the instance is running, connect to it via SSH:
     ssh -i MyKeyPair.pem ec2-user@<Public-IP>
    

Elastic Load Balancing (ELB) and Auto Scaling: An Overview

As your infrastructure grows, simply launching EC2 instances may not be enough to ensure high availability, performance, and scalability. This is where Elastic Load Balancing (ELB) and Auto Scaling come into play.

While we will dive deeper into these topics in future articles, let’s quickly go over the basics.

Elastic Load Balancing (ELB)

ELB automatically distributes incoming application traffic across multiple EC2 instances to ensure no single instance is overwhelmed, which increases availability and fault tolerance.

Types of Load Balancers:
  • Application Load Balancer (ALB): Best for HTTP and HTTPS traffic, providing advanced routing features.
  • Network Load Balancer (NLB): Ideal for low-latency and high-performance use cases, as it operates at the network layer (Layer 4).
  • Classic Load Balancer (CLB): Legacy option suitable for simple applications.
Key Features:
  • Traffic Distribution: ELB helps distribute incoming traffic evenly across all healthy instances.
  • Automatic Scaling: ELB can automatically adjust the number of instances it routes traffic to, depending on the load.
  • SSL Termination: ELB can manage SSL certificates, reducing the burden on EC2 instances.

Auto Scaling

Auto Scaling automatically adjusts the number of EC2 instances in response to changes in demand. This helps you ensure that the right number of instances are running at all times.

How Auto Scaling Works:
  • Scaling Policies: Define when to scale in (remove instances) or scale out (add instances) based on metrics like CPU usage or network traffic.
  • Auto Scaling Groups: Organize instances that can automatically scale based on these policies.
  • CloudWatch Alarms: Use CloudWatch to monitor instance metrics and trigger scaling actions.

In future articles, we will dive deeper into configuring Elastic Load Balancing and Auto Scaling, including practical examples and step-by-step guides.

Cost Optimization

While launching and scaling EC2 instances, you’ll need to consider cost management strategies to avoid unnecessary spending.

Cost Optimization Techniques:

  • Reserved Instances: Save up to 75% by committing to a 1- or 3-year term for certain instance types.
  • Spot Instances: Leverage unused EC2 capacity at a discount (up to 90% off regular prices).
  • Savings Plans: A flexible pricing model that allows you to save money on specific instance usage over a 1- or 3-year period.

Conclusion

In this article, we’ve covered how to launch EC2 instances using both the AWS Console and the AWS CLI. We also introduced the concepts of Elastic Load Balancing (ELB) and Auto Scaling, which will help you optimize your infrastructure for high availability and performance. These features are critical as your application grows and requires more flexibility.

In upcoming articles, we will dive deeper into Elastic Load Balancing and Auto Scaling, where we’ll look at configuration, advanced use cases, and best practices to maximize the efficiency and cost-effectiveness of your architecture.

Stay tuned for the next article where we’ll explore EC2 Instance Storage Options, including EBS, EFS, and Instance Store, and discuss when to use each based on your storage requirements.

Next Article: EC2 Instance Storage Options: EBS, EFS, and Instance Store

Top comments (0)