AWS Essentials for Java Developers (EC2, S3, RDS)
AWS Essentials Overview
For modern Java developers, understanding AWS is no longer optional. This guide explores the “big three” services that form the backbone of most cloud-hosted applications: EC2 for compute, S3 for object storage, and RDS for managed relational databases.
Core Concepts
1. Amazon EC2 (Elastic Compute Cloud)
EC2 provides resizable compute capacity in the cloud. It’s essentially a virtual server where you have full control over the OS and software stack.
- AMIs (Amazon Machine Images): Pre-configured templates for your instances.
- Instance Types: Optimized for different workloads (CPU, Memory, Storage).
- Security Groups: Virtual firewalls to control inbound and outbound traffic.
2. Amazon S3 (Simple Storage Service)
An object storage service that offers industry-leading scalability, data availability, security, and performance.
- Buckets: Containers for data.
- Objects: The files themselves, consisting of data and metadata.
- IAM Policies: Crucial for securing access to your data.
3. Amazon RDS (Relational Database Service)
Managed database service that makes it easy to set up, operate, and scale a relational database in the cloud (MySQL, PostgreSQL, Oracle, SQL Server, etc.).
- Automated Backups: Built-in recovery.
- Multi-AZ Deployment: High availability and failover support.
Practice Exercise: Launch and Connect
Building a bridge between a local Java application and AWS services.
Step 1: Launch an EC2 Instance
- Log in to AWS Console.
- Navigate to EC2 -> Launch Instance.
- Select Amazon Linux 2 (Free Tier eligible).
- Choose t2.micro.
- Create/Select a Key Pair (.pem file) for SSH.
- Under Security Group, ensure SSH (Port 22) is open to your IP.
Step 2: Install Java on EC2
Once connected via SSH:
ssh -i "your-key.pem" ec2-user@your-ec2-ip
# Update system
sudo yum update -y
# Install Corretto (AWS's OpenJDK distribution)
sudo yum install java-17-amazon-corretto-devel -y
# Verify
java -version
Step 3: S3 Upload via Java SDK
We will write a simple Java program to upload a file to an S3 bucket.
Maven Dependency:
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.20.0</version>
</dependency>
Java Code:
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import java.nio.file.Paths;
public class S3UploadDemo {
public static void main(String[] args) {
String bucketName = "my-unique-java-bucket-123";
String key = "test-file.txt";
String filePath = "./test-file.txt";
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder()
.region(region)
.build();
try {
System.out.println("Uploading object...");
s3.putObject(PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build(),
Paths.get(filePath));
System.out.println("Successfully uploaded!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Why This Works
- IAM Roles: In a production environment, instead of hardcoding credentials, you should attach an IAM Role to the EC2 instance. The AWS SDK will automatically detect the role’s temporary credentials, fulfilling the Principle of Least Privilege.
- Managed RDS: By using RDS instead of installing a database on EC2, you offload the heavy lifting of patching, backups, and scaling to AWS, allowing you to focus on application logic.
Summary
The synergy between EC2, S3, and RDS provides a robust environment for Java applications. Mastering these basics allows for seamless migration from local development to scalable cloud deployments.