Monday, November 25, 2019

Interacting with AWS S3 using Java on EC2

Many web applications are being built on top of the Cloud Infrastructure. Let's take the case of a photo sharing website like Instagram. The website can be deployed on EC2 and let it interact with S3 to store the pictures. Building a full fledged photo sharing website is beyond the scope of this blog, but we will explore how to execute a Java program on top of EC2 to interact with S3.

Here are the sequence of steps with the assumption that the reader is familiar with the basics of AWS. Also, all the steps in this blog will fall under the free tier.

Step 1: Create an Ubuntu EC2 instance (t2.micro) and connect to it via Putty or any other means. For the EC2 SecurityGroup the port 22/SSH has to be opened in the inbound.

Step 2: Get the list of softwares and install maven, java-common by executing the below commands on Ubuntu EC2 instance.
sudo apt-get update
sudo apt install maven java-common

Step 3: By default EC2 has no permission to interact with S3 or in fact with any other service. Create a role with AmazonS3FullAccess policy attached. This policy gives full permissions to all the folders and files in S3, which is not recommended. It's always best to give limited privileges by creating a custom policy and attaching it to the EC2.

Step 4: Attach the policy to the EC2. Now the EC2 has permissions to interact with S3.

Step 5: Get the link to the latest Amazon Corretto Java from this link and replace the link in the below wget command. Execute the wget commands in the Ubuntu EC2 to download Amazon Corretto Java. OpenJDK or Oracle JDK can also be used.


Install Amazon Corretto Java on Ubuntu using the dpkg command as an administrator.

sudo dpkg --install java-11-amazon-corretto-jdk_11.0.5.10-1_amd64.deb

Step 6: Create basic maven package using the below command. This will create a myapp folder with pom.xml, and other artifacts.

mvn -B archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DgroupId=org.example.basicapp \
Step 6: In the myapp folder, remove the pom.xml and replace with pom.xml mentioned here. Replace the exec-maven-plugin (1) and aws-java-sdk (1) versions with the latest versions got from the maven repository in the pom.xml file.

Step 7: Remove the file created by maven.

cd /home/ubuntu/myapp/src/main/java/org/example/basicapp

Create a file by the with the java program mentioned here in the above folder.

Step 8: Execute the below commands to compile the program and execute it.

cd ~/myapp
mvn clean compile exec:java

Step 9: Note that interaction with the S3 happening in the output of the above command towards the end as shown below. A bucket is created in S3, a file uploaded/downloaded. The S3 cleanup is also done in the Java program, so when seen in the S3 Management Console there won't be any changes.

Modify the Java program not to do the S3 cleanup and the changes done by the Java program can be seen in the S3 Management Console.

1 comment:

  1. Hi Praveen. I am trying to do above steps and have Created basic maven package. But not able to replace the exec-maven-plugin (1) and aws-java-sdk (1) versions with the latest versions got from the maven repository in the pom.xml file. When i am running command "ls exec-maven-plugin" or "rm exec-maven-plugin" i am getting response- "cannot access exec-maven-plugin: No such file or directory "

    please advise