Blog

Amazon EC2 Tutorial

Key Takeaways

  • Amazon EC2 allows you to run virtual servers in the cloud, providing scalable resources without physical maintenance.
  • EC2 offers various instance types tailored for different needs: general-purpose, compute, memory, accelerated computing, and storage optimized.
  • EC2 pricing models, including on-demand, reserved instances, and spot instances, offer flexibility to balance cost and performance.
  • Deploy Java applications on EC2 through manual setup or using Elastic Beanstalk for simplified management.

What is Amazon EC2?

Amazon Elastic Compute Cloud (EC2) is a web service that enables you to launch and manage virtual servers, or instances, in the cloud. With EC2, you can deploy applications and services without dealing with physical hardware.

EC2 is a cornerstone of Amazon Web Services (AWS), widely used by startups, large enterprises, and individual developers to host applications efficiently in the cloud.

Why Cloud?

Scalability

Cloud platforms like AWS allow you to scale your resources up and down based on demand. Auto-scaling ensures cost-effectiveness during low traffic and robustness during peak periods.

Cost Savings

Running your own hardware can be costly and resource-intensive. Cloud providers handle infrastructure, reducing the need for physical space and IT support, saving both time and money.

Security

Cloud providers like AWS offer robust security measures, often surpassing what individual users or companies can implement. They continuously update their security protocols to protect your data.

Availability

Amazon EC2 operates in various global regions, enhancing service availability and performance. Using multiple availability zones, you can minimize downtime and latency by distributing your application worldwide.

The Difference between AWS and EC2

EC2 is part of the broader AWS ecosystem, which includes services like S3, Lambda, and RDS. While AWS provides an extensive suite of cloud services, EC2 is fundamental as it powers many of these services through its virtual computing infrastructure.

How Is EC2 Used?

EC2 is versatile, hosting applications ranging from personal blogs to complex enterprise systems like Netflix or LinkedIn. Even some AWS competitors use EC2 to support their platforms.

How Does EC2 Work?

Using your AWS account, you can launch EC2 instances by selecting the desired operating system, processor, and other specifications via the AWS Management Console. Once launched, you can access the instance over SSH and use it like a typical server.

EC2 Instance Types

EC2 instance types are classified into several families, optimized for different purposes like compute, memory, and storage. Choosing the right instance can be daunting, so here’s a breakdown:

General Purpose

General purpose instances, like the T and M series, offer a balance of compute, memory, and network resources. They're perfect for web servers and development environments.

Compute Optimized

These instances provide high CPU power for tasks like gaming servers, high-performance computing, or scientific modeling.

Memory Optimized

Ideal for memory-intensive tasks, such as running large in-memory databases or processing massive data sets, using software like Apache Spark.

Accelerated Computing

Accelerated computing instances use GPUs for high-throughput processing, suitable for machine learning and video rendering tasks.

Storage Optimized

These instances are built for workloads that require high IOPS, ideal for databases and data warehouses.

EC2 Pricing

EC2's pricing structures offer flexibility and options for cost management.

On-Demand

Pay for compute capacity by the hour or second with no long-term commitments. It's handy for unpredictable workloads.

Spot Instances

Buy unused EC2 capacity at discounted rates. This model is economical for non-urgent and flexible workloads.

Reserved Instances

Make upfront payments to reserve resources at lower hourly rates. It's best for applications with steady-state needs.

Savings Plans

Commit to using a specific amount of AWS compute power and gain a discount, similar to reserved instances but with added flexibility.

Dedicated Hosts

Get a physical server exclusively for your use, ensuring compliance with software licensing or regulatory requirements for certain industries.

EC2 Tutorial: Host Java Web Application on AWS

You can deploy a Java application on EC2 manually or use AWS Elastic Beanstalk for automation.

Manually Deploying Your Java Application to EC2

1) Launch the EC2 Instance

Start by launching a new EC2 instance from the AWS Management Console.

2) Choose an AMI

Select an Amazon Machine Image (AMI) to define the instance's software configuration. Choose one that suits your OS and runtime environment needs.

3) Select an Instance Type

Choose an instance type that matches your application's resource requirements. The t2.micro offers a free tier for AWS exploration.

4) Configure the Instance

Configure networking, storage, and other settings as needed.

5) Generate a Key Pair

Create or use an existing key pair for secure access to your instance.

6) SSH into the EC2 Instance

ssh -i ~/Downloads/mykey.pem ec2-user@ec2-xx-xx-xx-xx.compute-1.amazonaws.com

7) Install Java

Install the required Java version. As of now, Java 17 is widely adopted:

sudo amazon-linux-extras install java-openjdk17

8) Verify Java Install

java -version

This command should display the current version of Java.

9) Copy Source Code

scp -i ~/Downloads/mykey.pem ~/path/to/your.jar ec2-user@ec2-xx-xx-xx-xx.compute-1.amazonaws.com:/home/ec2-user/webapp

10) Running the Application

nohup java -jar demo.jar &

This command runs your Java application in the background.

11) Redirecting Traffic

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

12) Verify It's Running

Visit the public URL of your instance in a browser to confirm the application is running.

Using Elastic Beanstalk

Elastic Beanstalk simplifies deployment, automatically managing EC2 resources with minimal manual configuration. You simply upload your code, and Beanstalk handles the rest, including load balancing and auto-scaling.

Problems with Beanstalk?

Despite its ease of use, Elastic Beanstalk can make debugging more opaque than manually managing your stack.

Conclusion

EC2 is a vital part of AWS, offering flexible cloud computing resources at different scales and price points. Whether you choose to manage your instances manually or leverage services like Elastic Beanstalk, AWS's EC2 empowers developers to efficiently deploy applications in the cloud.

FAQ

What is the latest Java version for EC2?

As of now, Java 17 is the current long-term support (LTS) version, but always check for the latest updates and security patches.

Can I use any operating system on EC2?

EC2 supports numerous operating systems through its AMI options, including Linux distributions, Windows Server, and others.

Is Elastic Beanstalk free?

You don’t pay for Elastic Beanstalk itself, but you incur costs for the underlying AWS resources it uses.

Are spot instances always available?

No, spot instance availability fluctuates based on AWS's excess capacity. They’re great for flexible workloads without time-sensitive constraints.

Mastering the tech interviewWhat everyone is doing wrong in tech interviews