Getting Started Spring Boot Application on AWS — S3 (Part 2)
Revise : In Part 1 — We did following things :
- S3 Bucket Creation via AWS Console
- Creating a Fresh Spring Boot Project and wrote some code with S3 API to Upload and Download File using AWS SDK
If you missed Part -1 Here it is https://vaibhavdesign.medium.com/getting-started-spring-boot-application-on-aws-s3-f5f42329f3f6
In this Part — 2, we are going to deploy our previously created Spring Boot App (Backend) to EC2 Instance,
Ok first of All, let me tell there are different ways to deploy our App on AWS which reduces steps and make work easier,
- ElasticBean Stalk
- Lambda as Serverless (Depends on Use case)
- (Also can automate workflow with CodeDeploy i.e CI/CD)
3. Or Container Way
But, In this we will follow Manual way, so we get to know What happens behind this automated services provided by AWS, not entirely
Lets Get Started :-)
Step 1 : Creating an EC2 Instance
To Run App/Service or Basically Code we need a Compute, and AWS Provides this under a resource named Elastic Compute Cloud (EC2), it allows users to rent virtual computers (with OS — Windows/Linux/Mac running) on which to run computer applications and accessible over Internet, at very cheap cost. (Check Pricing Model 😛)
- Login to AWS Console
- Search “EC2” in (Unified Search Box), Select first option
3. And Here goes, EC2 Screen
Instances (running) : Each Virtual Machine you create and in running state is counted here.
Key-Pairs : it consit of a private key and a public key, used as security credentials to prove your identity when connecting to an instance
(e.g using Putty), and assigned while creating instance. (Must Download)
Security-Groups : it act as Firewall, consisting of rule to control incoming and outgoing traffic (in our case for EC2, but if you have worked with services like RDS its required there as well).
- Taking it step ahead you need to undestand VPC inside AWS Account.
Elastic IP’s : Let’s understand why many services has name ‘Elastic’, Ok, Why we prefer Public Cloud Services because its feasible to start/stop or do anything with services we used, that helps reduce cost and increase performance. Now as we are using Free-Tier, everytime you stop and start EC2 Instance its Public Address will change everytime, What if I don’t want to change it as it might impact somewhere, then we have to get Elastic IP from AWS Address Pool and assign to Instance.
[As Elastic IP comes provided from AWS Address Pool (and are limited), Once you get one, if you don’t assign it to any running instance, you will be charged.]
Volumes : Its high performance storage required for EC2 Instance.
If you want to “terminate” an Instance, but store its state, then we Snapshot, in S3 and restore later, without loosing anything.
That’s enough of concepts, there are many things to explore :-)
4. Scroll down a bit, Click on “Launch Instance”
OR “Go to -> Instances and you will see same option there”
5. First Step is to Select Image (i.e OS) for our Instance, in this tutorial we will be using “Amazon Linux 2” as its eligible under Free-Tier :-)
6. Second Step to Select Configuration like CPU, Memory (RAM).
AWS Categorize this Common Configuration under Family like (t2, m3,p3) each are optimized for something and used as per application workload needs.
In this we will use “t2.micro”, Yes you guessed correctly its eligible free tier :-)
7. Instance Details and Configuration (Do Not Modify Anything Here)
8. Compute Instance Needs a Storage (EBS Volume)
We can configure but under free tier we can get upto 30GB, as we already selected instance type — free tier so it will assign storage as default, (Do not change anything here, you can change size, but 8 GB is OK for learning)
9. Tags for EC2 (Optional)
In AWS we can add tags for different Resources, we use tags to organize resources, and cost allocation tags to track AWS costs on a detailed level.
(Read More on Tagging Strategies)
10. Security Group Configuration (We will change this later) : To control incoming request (to custom port or protocols) and outgoing flow
11. Finally “Review And Launch” , Woaww!!! we created our first instance
Once EC2 is Created and Running
Select the Instance (Click on CheckBox), you will various information been show (Have blurred few info due to security reasons)
Now Click on “Connect” to enter into Instance SSH via Browser
(We can use other SSH Tools to connect, Ref https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html)
Once you Connect a New Tab will Open with UI
Try Linux Commands (To Update OS)
sudo suyum update -yyum install httpd -y# check if java is installed
java -version# if no version found, Install Java with following command
yum install java-1.8.0 -y# Make App Directory to Save Spring Boot Jar
Step 2: Deploying our Spring Boot App in EC2 Instance
- Upload Build Jar File on S3
- Copy JAR from S3 to EC2 Instance -(Storage/Volume)
# Make Sure to Change Bucket/Object Name and Region of S3 Bucketaws s3 cp s3://bucketnamedemo/app-0.0.1-SNAPSHOT.jar /app --region=us-east-1
3. Start Our App
# Make Sure to Change Build File Name as per your Projectjava -jar /app/app-0.0.1-SNAPSHOT.jar# To Stop App Press Crtl + C# Worst Case - As Our App will be using 8080 Port and is still running in background, then we cannot use same port, run below commands lsof -i:8080
kill -9 $(lsof -t -i:8080)
4. Lets Test Our App API (Keep Spring Boot App Running)
Copy Public Address and Try to Hit via Browser or Postman
(Add Port at Address : ec2……..com:8080/…)
5. Oh :-( It won’t work, because Security Group Config, Firewall will not allow incoming request with HTTP Method and to this Port.
Lets Allow That
a. Select Instance (Check Box) and From Info Go To “Security” Tab
b. Click on Security Group in Infomation Tab
c. Edit Inbound Rule (By Default we allowed only SSH)
d. Add Rules as Follow (Can Change Port as per your App Config)
- Also you can allow from particular IP / Resource
(Or Resources Belonging to Same Security Group, )
e. And Save Rules (Wait for few Seconds for Changes to Reflect)
Now Try to Hit URL, And It should be Working Now.
(In case you face any issue, Please Comment Below)
f. As Our App Interact with S3 Resource, If we try to hit related API’ request, it won’t work, for that we need to Assign a Permission to EC2 Instance to Allow S3 Related Operation,
For that we need to Make Change in Role/Assign , Lets Do That
- In Unified Search, Type “IAM”
IAM is service where we manage Permission, Role, Users etc.
Click On “Role”
We will be assigning for AWS Service, Select EC2, Click “Permission”
We will create custom Policy -> “Create Policy”
Select “Permission” for S3, as Below (As we will keep limited and Required Permissions only, which is first rule of Security)
We can Add ARN (Resource Name) , and Limit Permission to That Particular S3 Bucket Only (Let’s not do that here), (Tick All Resources/ ANY)
- In Java API, we are passing Bucket Name as well
Lets “Review Policy”, Name the Policy (Required), Description is Optional
and “Create Policy”,
Now lets return back to “Create Role” and assign Policy (If Our New Policy is not seen click on Refresh Icon)
Click on “Next Tags” (Lets Skip or you can Add Tags)
Click on “Next Review”
Fill the Details (Role Name) and “Create Role”
Now Lets Assign Role to Our Instance, Go Back to EC2 Console
Select Instance, then From “Action” Click “Modify IAM Role”
And Assign Role
Now Try API with S3 Operation, It should work like Charm.
Done, 😓 We came a Long Way to End of Article,
Hope you enjoyed this and learned, How things works on ElasticBeanStalk
(Not completely but somewhat similar)
Once Done Lets Cleanup !!!
- Terminate and Remove EC2 Instance
- Delete Policy & Roles (From IAM)
- Remove S3 Bucket.
Now I leave you with Many Question to Find Answer For
- VPC related Stuff (Including CIDR in Security Group)
- Roles & Permission
- Security Groups & Elastic IP
- Machine Images / EBS (Volume)
- Auto Scaling — Load Balancer
In Next Part, We will Containerize our Application and Deploy on EC2 By Enabling Docker and With CI/CD of AWS.
Till Then Take Care, Be Safe :-)
PS: Read /Listen/Watch about various Use Cases & Architecture of Different Applications using EC2
Follow Us on Instagram : www.instagram.com/awsdeveloper