AWS is the leading cloud platform and a mandatory skill for modern Java developers. This guide covers every AWS service you will be asked about in Java backend interviews, with practical Spring Boot integration examples.
| EC2 | Virtual servers — On-Demand, Reserved, Spot pricing |
| S3 | Object storage; unlimited scale; 11 nines durability |
| RDS | Managed relational DB (MySQL/Postgres/Oracle); ACID, backups |
| Lambda | Serverless function; billed per invocation; cold start risk |
| SQS | Message queue — point-to-point; decouples producers/consumers |
| SNS | Pub/sub fan-out — one message to many subscribers |
1. S3 — Spring Boot file upload with AWS SDK v2
@Service
public class S3Service {
private final S3Client s3Client;
private final String bucketName;
public String uploadFile(String key, InputStream content, long size) {
PutObjectRequest request = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.contentType("application/octet-stream")
.build();
s3Client.putObject(request, RequestBody.fromInputStream(content, size));
return "https://%s.s3.amazonaws.com/%s".formatted(bucketName, key);
}
public InputStream downloadFile(String key) {
GetObjectRequest request = GetObjectRequest.builder()
.bucket(bucketName).key(key).build();
return s3Client.getObject(request);
}
}
2. RDS vs DynamoDB
| Feature | RDS | DynamoDB |
|---|---|---|
| Type | Relational (MySQL, Postgres, Oracle) | NoSQL (key-value / document) |
| Schema | Fixed schema | Flexible schema |
| Scaling | Vertical + read replicas | Horizontal; auto-scaling |
| Best for | Financial data, complex joins | Session store, IoT, high-volume simple reads |
3. SQS vs SNS — key difference
// SQS — queue-based; one consumer reads each message
@SqsListener("order-queue")
public void processOrder(OrderMessage msg) {
orderService.fulfill(msg.getOrderId());
// message deleted from queue after successful processing
}
// SNS — fan-out; one message delivered to ALL subscribers
// Use SNS + multiple SQS queues for fan-out pattern:
// SNS Topic "order.created"
// → SQS Queue "fulfillment" (fulfillment service listens)
// → SQS Queue "notification" (notification service listens)
// → SQS Queue "analytics" (analytics service listens)
4. ElastiCache with Spring Boot @Cacheable
# application.yml
spring:
cache.type: redis
redis:
host: my-cluster.cache.amazonaws.com
port: 6379
@Service
public class ProductService {
@Cacheable(value = "products", key = "#id")
public Product getProduct(Long id) {
return productRepository.findById(id).orElseThrow();
}
@CacheEvict(value = "products", key = "#product.id")
public void updateProduct(Product product) {
productRepository.save(product);
}
}
5. IAM — Principle of Least Privilege
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::my-app-bucket/*"
},
{
"Effect": "Allow",
"Action": ["sqs:SendMessage", "sqs:ReceiveMessage", "sqs:DeleteMessage"],
"Resource": "arn:aws:sqs:ap-south-1:123456789:order-queue"
}
]
}
Always use IAM Roles (not access keys) for EC2/ECS/Lambda. Attach the role to the service — credentials are automatically rotated. Never embed access keys in application code or environment variables in production.
❓ Frequently Asked Questions
Q: What is the difference between ECS and EKS?
A: ECS (Elastic Container Service) is AWS's proprietary container orchestrator — simpler, tightly integrated with AWS services, lower operational overhead. EKS (Elastic Kubernetes Service) runs managed Kubernetes — more portable, complex, better for multi-cloud or teams with K8s expertise.
Q: How do you deploy a Spring Boot app to AWS Lambda?
A: Use Spring Cloud Function. Wrap your business logic as a Function bean. Package as a JAR with the AWS adapter. Deploy via Serverless Framework or AWS SAM. Use SnapStart (Java 17+) to reduce cold start times to under 1 second.
Q: What is CloudWatch and what can you monitor?
A: CloudWatch collects metrics (CPU, memory, request count, error rate), logs (application logs via CloudWatch Agent or SDK), and alarms (trigger SNS notifications or Auto Scaling). Spring Boot Actuator metrics can be published to CloudWatch with the cloudwatch-metrics-exporter.
Q: What is Elastic Beanstalk?
A: Elastic Beanstalk is a PaaS — you upload your JAR and AWS provisions EC2, ALB, Auto Scaling, and RDS automatically. Suitable for simple applications. For microservices and containers, ECS or EKS is preferred.
Q: How do you handle secrets in AWS (database passwords, API keys)?
A: Use AWS Secrets Manager or AWS Parameter Store. Spring Cloud AWS can automatically inject secrets as properties at startup. Never put secrets in environment variables that are visible in ECS task definitions — use Secrets Manager references instead.
Post a Comment
Add