Module 2: Creating EC2 Instances

 Lesson 2: Launching and Managing EC2 Instances



Welcome to Lesson 2 of our "Fundamentals of AWS" series! In this module, we dive into the heart of AWS compute with a focus on Elastic Compute Cloud (EC2) instances. By the end of this lesson, you'll be equipped with the skills to not only create EC2 instances but also manage them efficiently.


Let's outline what we aim to achieve in this lesson:

1. Gain Hands-On Experience:

   - Understand the step-by-step process of launching EC2 instances.

   - Explore the various configuration options available.

2. Understand Configuration Options:

   - Dive into the intricacies of instance types and their relevance to specific use cases.

   - Learn the importance of security groups and key pairs for a robust and secure EC2 environment.

3. Explore Advanced Concepts:

   - Introduction to Elastic Load Balancing (ELB) for efficient traffic distribution.

   - Monitoring and troubleshooting EC2 instances using AWS CloudWatch.

 1. Creating an EC2 Instance:

 Step-by-Step Guide:

Let's break down the process of launching an EC2 instance into actionable steps:

   Step 1: Choose an Amazon Machine Image (AMI):

     - Select an AMI based on your application requirements.

   Step 2: Choose an Instance Type:

     - Consider factors like CPU, memory, and networking capabilities.

     - Example: For general-purpose applications, choose t3.micro.

   Step 3: Configure Instance Details:

     - Set up networking options, IAM roles, and user data scripts.

   Step 4: Add Storage:

     - Specify the size and type of the root device and additional volumes.

   Step 5: Configure Security Group:

     - Define rules for inbound and outbound traffic.

   Step 6: Review and Launch:

     - Confirm your settings and launch the instance.

 Selection of Instance Types Based on Use Case:

- t3.micro: Low-cost, general-purpose instances suitable for small applications.

- m5.large: Balanced compute, memory, and networking for diverse workloads.

- p3.2xlarge: High-performance instances optimized for machine learning.

 2. Security Groups and Key Pairs:

 Configuring Security Groups:

- Definition: Virtual firewalls for your instances controlling inbound and outbound traffic.

- Example:

  - Allow SSH (port 22) only from a specific IP range.

  - Permit HTTP (port 80) access to the world.


# AWS CLI command to create a security group

aws ec2 create-security-group --group-name MySecurityGroup --description "My security group" --vpc-id vpc-1a2b3c4d


 Importance of Key Pairs:

- Definition: Key pairs provide secure access to EC2 instances.

- Example:

  - Generate a key pair using the AWS Management Console.

  - Associate the key pair with your EC2 instance during launch.


# AWS CLI command to associate a key pair with an instance

aws ec2 run-instances --image-id ami-xxxxxxxx --key-name MyKeyPair


 3. Elastic Load Balancing (ELB):

  Introduction to ELB:

- Definition: Distributes incoming application traffic across multiple EC2 instances.

- Example:

  - Create an Application Load Balancer for distributing HTTP/HTTPS traffic.

  - Associate EC2 instances with the load balancer.


# AWS CLI command to create an Application Load Balancer

aws elbv2 create-load-balancer --name MyLoadBalancer --subnets subnet-xxxxxxxx subnet-yyyyyyyy --security-groups sg-xxxxxxxx


 4. Monitoring and Troubleshooting:

  Utilizing AWS CloudWatch:

- Definition: Monitor AWS resources, including EC2 instances, in real-time.

- Example:

  - Set up CloudWatch alarms for CPU utilization.

  - Create custom metrics for specific application requirements.


# AWS CLI command to create a CloudWatch alarm

aws cloudwatch put-metric-alarm --alarm-name MyEC2Alarm --alarm-description "Alarm for EC2 CPU utilization" --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 300 --threshold 70 --comparison-operator GreaterThanThreshold --dimensions Name=InstanceId,Value=i-xxxxxxxxxxxxxxxxx


  Common Troubleshooting Tips:

- Check instance status using the AWS Management Console.

- Review system and application logs for errors.

- Use CloudWatch metrics to identify performance bottlenecks.

 Example: Walkthrough of Multi-Tier Architecture:

Let's bring it all together with a practical example – creating a multi-tier architecture:

  1. Launch EC2 instances for web servers and a database.
  2. Configure security groups to allow web traffic to the web servers and database access to the database instance.
  3. Create an Application Load Balancer to distribute incoming traffic to the web servers.
  4. Monitor the performance of instances using CloudWatch.

This hands-on example will reinforce your understanding of EC2 instance management and the associated services.

In conclusion, mastering the creation and management of EC2 instances is a fundamental skill for any AWS practitioner. With these insights, you're well on your way to harnessing the full potential of AWS compute services. Stay tuned for more AWS adventures in our upcoming lessons!