Sunday, 23 June 2019

Deploy Spring boot application on AWS EC2 instance



Today we will see how spring boot application would be deployed on AWS EC2 instance.

Assumptions:

I am assuming below points
  •    You already have Spring Boot application ready with you.
  •      You have already created S3 bucket and upload the Spring Boot application jar file.
  •      You have created IAM role for accessing the S3 bucket so that it would be assigned to EC2 instance while creating it.


Spring Boot Rest api.


There are number of ways you can deploy the spring boot application on EC2.

  •     Using Terraform
  •     CloudFormation
  •         Providing deployment script in Advance Section while creating the EC2 instance.
  •       Manually



In this post we will see how you will deploy that application on EC2 manually on Windows OS.
  1.  Login to AWS console and select EC2 service
  2. Now create Launch Instance button from the EC2 landing page






3. Then select the AMI and go to the next page

4. Now select the instance type (Choose only the free tire eligible, else charges will apply)



5.Now in instance details page we need to select two important things, rest of the things you can keep as default like VPC, subnet etc.

·  In this page first select the IAM role that we have created earlier, in my case I named that role as Admin_S3_Access



6.Go with default storage. 
7.Add tags if you want
.
8.Create new Security group and added other type of type

9. Finally choose one key pair and finish the instance creation step by clicking Launch Instance button and download it. It will be used to login in EC2 instance.

10. Go to bash command prompt where you saved you security private key.

11. You need to execute below command to generate public key
# ssh-keygen –y –f MyNewSpringBootKey.pem > MyNewSpringBootKey.pub

12. Rename the private key without .pem extension.

13. Grab the public IP of your EC2 instance.

14. If you don’t have secure shell app extension for your browser than download the extension.


Open the Secure shell app


15.  Make sure you provide the public IP in the required text box and Import both the private and public key.

16.  Once connected give it to sudo permission



17. Execute below command
# install updates
yum update –y


18. Install apache server
# install apache httpd
yum install httpd –y



19. Install java 8

# install java 8
     yum install java-1.8.0 –y


20.. Create below directory where you will copy jar of Spring boot application.
# create the working directory
mkdir /opt/spring-boot-ec2-demo

21. # create configuration specifying the used profile
echo "RUN_ARGS=--spring.profiles.active=ec2" > /opt/spring-boot-ec2-demo/spring-boot-ec2-demo.conf

22. # download the maven artifact from S3
aws s3 cp s3://myfirsts3bucketsumit/myproject.jar /opt/spring-boot-ec2-demo/


23. Execute below commands
# create a springboot user to run the app as a service
useradd springboot
chown springboot:springboot /opt/spring-boot-ec2-demo/myproject.jar

chmod 500 /opt/spring-boot-ec2-demo/ myproject.jar

24. # create a symbolic link

ln -s /opt/spring-boot-ec2-demo/myproject.jar /etc/init.d/spring-boot-ec2-demo

25. #forward port 80 to 8080
  echo "<VirtualHost *:80>
    ProxyRequests Off
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/
   </VirtualHost>" >> /etc/httpd/conf/httpd.conf


26. service httpd start
  java -jar /opt/spring-boot-ec2-demo/myproject.jar 



27. Copy the  IP/DNS of your EC2 instance and verify it on browser.






















No comments:

Post a Comment