Posts

Showing posts with the label Java Backend

AWS Lambda Explained with a Simple Java Example

AWS Lambda is a fully serverless compute service that lets you run code without provisioning or managing servers. You are billed only for the time your function actually runs—unlike EC2, where you pay even when the server is idle. Lambda automatically scales, executes only when triggered, and shuts down after execution. What AWS Lambda Does Runs small pieces of code called functions written in Java, Python, Node.js, Ruby, and more. No server, OS, CPU, RAM, or patching management—AWS handles everything. Billed only per invocation and execution time. Ideal for event-driven, bursty, or low-traffic workloads. How AWS Lambda is Triggered A Lambda function does nothing until a trigger fires. Common triggers include: S3 — Run code when files are uploaded or deleted (e.g., image resizing). SQS / SNS — Process messages as they arrive. API Gateway — Build serverless REST APIs where each HTTP call invokes Lambda. EventBridge / CloudWatch — Cron jobs...

Spring Boot on AWS EC2: Upload to S3 Securely Using IAM Role

When running a Spring Boot application on AWS EC2 , the most common security mistake is hardcoding AWS access keys inside application.properties . This is unnecessary and risky. AWS already provides a secure mechanism: IAM Roles attached to EC2 . In this blog, you will learn how to upload and download files to Amazon S3 from Spring Boot running on EC2 without any access keys , using: Spring Profiles AWS SDK v2 Instance Metadata Service (IMDS) IAM Roles Why Access Keys Are a Bad Idea on EC2 Typical S3 setups use: Access key Secret key Region Bucket This works only for local development. It should NOT be used on EC2. Problems with access keys: Stored in config or code (high risk) Might leak through Git, logs, or screenshots Require manual rotation Not needed if app runs on EC2 AWS automatically manages temporary credentials using the EC2 Instance Profile (IAM Role) . Architecture: EC2 → IAM Role → S3 Spring Boot app r...

Deploy Spring Boot Application on AWS EC2 (Step-by-Step)

Deploying a Spring Boot application on AWS EC2 is one of the easiest ways to host your Java backend in the cloud. In this guide, you will build a JAR file, copy it to an EC2 instance, install Java, run the app, expose port 8080, and keep the application running in the background using nohup . Overview of the Deployment Flow A Spring Boot REST API runs locally on port 8080 An EC2 instance (Amazon Linux, t2.micro) runs in AWS You build a JAR from the Spring Boot project You SSH into EC2 and copy the JAR You install Java 17 and run the JAR file You open port 8080 in the security group After deployment, anyone can access your API using the instance's public IP or DNS . Step 1: Create & Test a Simple Spring Boot App Start with Spring Initializr: Maven project Java 17 Packaging: JAR Dependency: Spring Web Create a simple REST API: @RestController public class HelloController { @GetMapping("/hello") public Str...

Spring Boot + AWS S3: Upload and Download Files

Connecting a Spring Boot application to Amazon S3 is one of the most common real-world backend requirements. Whether you want to store images, documents, logs, or app data, S3 provides secure, scalable, and durable object storage. In this tutorial, you’ll learn how to integrate Spring Boot with AWS S3 using AWS SDK v2 to build clean and reliable upload/download APIs. High-Level Architecture The overall workflow is simple: Your Spring Boot application runs locally or on a server. The app uses AWS SDK v2 to create an S3Client . The S3Client connects to your S3 bucket using AWS credentials. REST APIs handle upload and download requests and interact with S3 internally. This setup works for local development, on-prem servers, and cloud deployments. Step 1: Create a Spring Boot Project Create a new project using Spring Initializr: Project: Maven Language: Java Spring Boot: 3.x+ Dependencies: Spring Web Java Version: 17+ Import the project ...