Amazon EC2 Tutorial

What is Amazon EC2?

Amazon Elastic Compute Cloud (EC2) lets you run virtual servers in the cloud. EC2 gives you the ability to deploy web services and applications without having to physically maintain the hardware yourself.

EC2 is one of the most popular platforms for cloud computing used today. Bloggers, startups, and big corporations alike leverage EC2 to host their apps in the cloud with AWS.

Why cloud?


Cloud providers like AWS allow you to scale your processing up and down based on demand. Rather than anticipate hardware needs, you can auto scale applications to use the processing when they need it. This saves on cost during low traffic periods and keeps things working during high traffic periods.

Cost savings

Maintaining your own hardware gets expensive. Apart from physically housing the hardware somewhere, you also have to hire the IT support resources necessary to install it, upgrade it, and keep it running.

Offloading these responsibilities to a cloud provider saves companies both time and money.


Cloud infrastructure puts some of the responsibility on the provider. While security is everyone's concern, the security behind the hardware you're running on is managed by the cloud provider.

Cloud providers implement the highest standards when it comes to security. It's how they compete for your business as a hosted application. The chances that you're own security implementation is better than a cloud provider's is low.


Big companies like Amazon have the infrastructure to support physical servers all over the world. EC2 supports 24 regions and 76 availability zones globally.

Such a vast network of servers and locations can be leveraged to minimize both downtime and network latency. Cloud computing can distribute your application across different regions resulting in faster connections and higher availability.

The difference between AWS and EC2

EC2 is part of a larger AWS ecosystem. AWS also includes other popular services like S3, Lambda, and Beanstalk.

While AWS includes an overwhelming number of services for cloud computing, EC2 is the most popular. EC2 instances are the backbone behind AWS.

How is EC2 used?

EC2 is used for hosting applications in the cloud. These apps can be anything from a personal blog to a game engine. Tech giants like Netflix, LinkedIn, FaceBook, and Twitter all host cloud infrastructure on EC2.

If you're familiar with other AWS services like BeanStalk, understand that these platforms also run on EC2 instances behind the scenes. Even competitors like Heroku use EC2 to power their services.

How does EC2 work?

Customers create EC2 instances via AWS. Using an AWS account, they can easily pick instance types with a given OS, processor, and other specs that fit their needs. Through the AWS console, they can configure things like number of instances, network settings, etc.

Once an EC2 instance has been created, users can access the server remotely via SSH and use the server just like they would any virtual machine.

EC2 instance types

EC2 instance types come in many shapes and sizes. While some emphasize processing and memory, others are built for storage.

Instance types are grouped based on these distinctions. For example there is a "memory optimized" group and a "compute optimized" group.

Each group offers a set of instance types. For example the "compute optimized" group includes C6g, C5, C5a, C5n, and C4 instance types.

Each instance type comes in different sizes. For example the C4 instance type comes in large, xlarge, 2xlarge, 4xlarge, 8xlarge. These sizes differ in number of vCPUs, memory, storage, and network performance.

Feeling overwhelmed?

Finding the right instance type for your application can be overwhelming. Navigating within a group of instances to find the right type and size requires lots of comparison.

Here's a quick summary of each group and when it could be appropriate for your use case:

General purpose

General purpose instance types are the most popular. They offer a balance between computing, memory, and networking resources.

These instance types vary mostly in their consumption model. While the T family (T4g, T3, T3a, T2) offers burstable performance, M family (M6g, M5, M5a, M5n) offer fixed performance.

Burstable vs Fixed Performance?

"Fixed performance" means the instance has a fixed amount of CPU processing power. "Burstable performance" means the instance can dynamically change it's processing power based on demand spikes.

Burstable performance allows you to quickly scale when demand is high and save money when demand is low. This concept is perfect for websites or applications that experience high and low traffic periods.

Fixed performance makes more sense when workloads are consistent and predictable. Batch processing may be better suited for a fixed performance instance.

General purpose instance types are great for hosting web servers, development environments, and microservices. While you may think your needs fall outside the realm of "general purpose", remember that some of the M models offer up to 384GiB memory and 96 vCPUs!

Compute optimized

Compute optimized instances emphasize strong CPU performance. These types have a high ratio of CPU vs memory making them great for CPU intensive applications like dedicated gaming servers, scientific modeling, and machine learning.

There are only 5 instance types. They differ mainly in processing power, network bandwidth, and storage type (EBS vs SSD).

Memory optimized

Memory optimized instances are great for processing large data sets in memory. Things like Apache Spark run great on these machines.

Memory optimized types are great for high performance databases and in memory data stores.

Accelerated computing

Accelerated computing instance leverage GPUs to deliver high throughput and parallel processing. This makes them perfect for things like game streaming, video transcoding, and graphics-intensive applications.

Accelerated computing is a popular option for heavy machine learning and graphics processing.

Storage optimized

Storage optimized instances are perfect for most big data use cases. They provide low latency and high throughput for large volumes of read/write operations.

Storage instances can host virtually any type of database. They excel with distributed file systems like HDFS and Hadoop.

Use a storage optimized instance for data warehousing and efficient parallel processing for big data needs.

EC2 Pricing

EC2 pricing isn't straight forward. There are lots of ways to pay for the EC2 instances you procure with AWS.

On demand

On demand pricing means you pay for what you use when you use it. There's no upfront contract or cost.

This pricing model makes sense when you value no up-front contracts or commitments and the benefits of low cost computing with high availability.

Spot instances

Spot instances allow you to purchase EC2 instances at up to a 90% discount. While this sounds like a no brainer, it involves the time investment of bidding on unused resources that fluctuate in price and availability.

Spot instance pricing can be combined with more expensive on-demand pricing to save on predictable workloads that don't have hard time constraints. If you have a resource-intensive workload that can be run "whenever", then spot instances can save money.

Reserved instances

Reserved instances allow you to save through up-front contracts. When you reserve an instance, you can get up to a 75% discount on regular pricing.

Reserved instances saves money without the bidding headaches of spot instances. The main caveat with reserved instances is that resources are limited. This is why reserving instances makes the most sense when you can anticipate demand.

Savings plan

Savings plans are like reserving instances in that you have an up front agreement to save money. Savings plans offer up to 72% discount but require a 1 to 3 year commitment.

Similar to reserved instances, savings plan makes sense when you can easily forecast your needs.

Dedicated Hosts

This plan emphasizes licensing costs. When you're a big company and want to operate under your own license, dedicated hosts allow you to achieve this with EC2.

Dedicated hosts also provide physical dedicated hardware that helps your organization meet corporate compliance etc. This pricing model is most appropriate for large scale enterprise operations.

EC2 Tutorial: Host Java Web Application on AWS

There are several ways to deploy your Java web application to AWS. You can manually configure an instance to run your apps executable jar or use Elastic BeanStalk to automatically manage your instances.

Manually deploying your Java application to EC2

1) Launch the EC2 instance

Start by launching a new instance for your application. You should see an orange button like this:

launching amazon instance

2) Choose an AMI

The next step is to choose an Amazon machine instance (AMI) for your application. An AMI is a preconfigured template for your instance. Similar to a Docker image, it specifies which operating system, processor, libraries, etc. your instance will run on...

choosing an Amazon machine instance

3) Select an instance type

Next you'll want to select an instance type:

selecting instance type

The t2.micro offers a free tier and is recommended for initial AWS exploring...

4) Configure the instance

The next step will be configuring the instance. Only a few basic things are required but you can specify security policies, storage, number of instances, etc.

5) Generate a key pair

Once you've configured your instance, you'll be asked to generate a key pair for security. This set of keys is what allows you to securely communicate with the virtual machine.

You can either use an existing set of keys or generate a new pair like this:

generating a key pair for ec2

Be sure to download the key so you can use it to connect locally.

6) SSH into the EC2 instance

Once your instance has been created, you can SSH into the machine directly via something like this:

ssh -i ~/Downloads/mykey.pem

Notice how the -i argument specifies the .pem file downloaded from the previous step. This is what allows you to securely SSH into the remote EC2 environment.

7) Install Java

Now that you're able to access the instance, you'll need to install the right version of Java.

This command installs the OpenJDK version of Java 11.

sudo amazon-linux-extras install java-openjdk11

8) Verify Java install

To be sure Java is installed correctly, run java -version. You should see something like this:

[ec2-user@ip-172-31-46-157 ~]$ java -version
openjdk version "11.0.7" 2020-04-14 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10-LTS, mixed mode, sharing)
[ec2-user@ip-172-31-46-157 ~]$ 

9) Copy source code

The EC2 instance needs your runnable jar file to run your application. Using scp makes it easy to copy files to a remote location:

scp -i ~/Downloads/mykey.pem ~/Downloads/demo\ 2/target/demo.jar

Notice how the same .pem file is referenced. This command copies the local demo.jar file to the remote location /home/ec2-user/webapp.

10) Running the application

After the executable file has been copied over, you can run it on the EC2 instance via:

nohup java -jar demo.jar

This will start the Java application. By using nohup, the process will continue running even when the session ends.

11) Redirecting traffic

If you're running a Spring Boot app, it will default to running on port 8080.

While you can configure the app to run on any port, it's better to redirect traffic from port 80 to 8080:

sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

This is because all web traffic is sent to the load balancer on port 80. By redirecting all traffic to 8080, you forward all traffic to your application without having to specify a port like...

11) Verify it's running

To verify your Java app is running visit the public URL for your instance in the browser...

verifying a Java web app is running on EC2

Using Elastic Beanstalk

AWS Elastic Beanstalk is a service for quickly deploying and scaling web apps. Using Beanstalk, you can deploy applications without configuring EC2 instances.

Beanstalk is similar to Heroku in that it's a platform as a service (PaaS) running on top of EC2.

The benefits of Beanstalk are cost effectiveness and time savings. Beanstalk automatically provides load balancing, health monitoring, autoscaling, etc. for no additional cost. You only pay for the underlying EC2 instances being automatically managed for you.

Deploying with Beanstalk can be as easy as uploading your code...

uploading your code to AWS Beanstalk

Problems with Beanstalk?

If Beanstalk adds so many benefits for free then what's not to love? While Beanstalk certainly provides a more wholistic approach to hosting your web app, it can make deployment and debugging more of a "black box" operation.


EC2 is the backbone of AWS infrastructure and one of the most popular services powering cloud computing today. Using EC2, customers get virtual access to advanced computing power at a low cost.

EC2 instances come in many shapes and sizes. They are categorized based on processing, memory, storage, and networking capabilities.

EC2 pricing is both flexible and complex. Through different pricing models, customers can maximize both efficiency and cost savings.

You can deploy Java applications to EC2 a few different ways. While AWS gives you the underlying access you need to manually configure and deploy virtual machines, services like Beanstalk abstract away the need for devOps expertise.

Your thoughts?